Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  fukawi2 Apr 29 '13 at 6:18
add comment

2 Answers

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'
share|improve this answer
add comment

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.