0

with help of google , script that measures multicast streams bandwidth.

Here it is tcpdump -tnn -i eth1 -f multicast | sort | uniq -c | awk '{print $3,"> "$5,$1*$NF/2*8/1024 " Kbps"}' | awk '$1 > 10' & sleep 2s && pkill -HUP -f tcpdump

It turns out fine, except I need to hit enter to get back to prompt. I know it is && problem, but dont know how to fix it.

How can I trascribe it to shell script. And maybe add $var for number of seconds.

1
  • You have to press enter? If I put that in a script and run it, I have to press CTRL C to exit which returns me straight to the prompt. You need to explain your situation better. Commented Apr 29, 2013 at 6:18

2 Answers 2

2

Try using timeout to kill the tcpdump after a duration:

(timeout -s HUP 2 tcpdump -tnn -i eth1 -f multicast || true) | sort | uniq -c | awk '{print $3,"> "$5,$1*$NF/2*8/1024 " Kbps"}' | awk '$1 > 10' 

To script it:

#!/bin/bash if ! [[ "$1" =~ ^[0-9]+$ ]]; then echo "Usage: $0 <number of seconds>" exit 1 fi (timeout -s HUP $1 tcpdump -tnn -i eth1 -f multicast || true) | sort | uniq -c | awk '{print $3,"> "$5,$1*$NF/2*8/1024 " Kbps"}' | awk '$1 > 10' 
1

you don't need to press enter to exit. it's just a cosmetical issue here. that is because your output "overwrites" your terminal (you can still enter any command and press enter - it will be executed).

To "fix" this layout issue you can try to append "; sleep 1; echo" to your line so it will look like:

tcpdump -tnn -i eth1 -f multicast | sort | uniq -c | awk '{print $3,"> "$5,$1*$NF/2*8/1024 " Kbps"}' | awk '$1 > 10' & sleep 2s && pkill -HUP -f tcpdump; sleep 1; echo 

Hope that helps.

And here is your bash script with variable:

#!/bin/bash SLEEPTIME=5 tcpdump -tnn -i eth0 -f multicast | sort | uniq -c | awk -v a=$SLEEPTIME '{print $3,"> "$5,$1*$NF/a*8/1024 " Kbps"}' | awk '$1 > 10' & sleep $SLEEPTIME && pkill -HUP -f tcpdump; sleep 1; echo 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.