Background
I am running BusyBox in the remote server.
I have a bash script that does two things:
1. via ssh, starts a sub process to monitor tcp traffic using tcpdump command. Save results to a file - either on remote machine or local machine. Tried both.
2. starts a second sub process to generate tcp traffic.
Code Snippet:
#html_tcpdumpfile="$(ssh remotemachine.mydomain.net \"mktemp\")"
html_tcpdumpfile=$(mktemp)
test_steps=(
#"{ ssh remotemachine.mydomain.net \"timeout -t 20 tcpdump -nvi eth0 port 5060 > "$html_tcpdumpfile" \" ; }"
"{ ssh remotemachine.mydomain.net \"timeout -t 20 tcpdump -i eth0 port 5060 \"> $html_tcpdumpfile; }"
"{ ssh remotemachine.mydomain.net \"timeout -t 15 cat /tmp/htmlemail.txt | /etc/postfix/process_email.py \"; }"
)
pids=()
for index in ${!test_steps[@]}; do
(echo "${test_steps[$index]}" | bash) &
pids[${index}]=$!
echo "$pids[${index}] is the pid"
done
#shouldn't really need this because of the timers but... just in case...
for pid in ${pids[*]};
do
wait $pid;
done;
# ============ ANALYZE TEST RESULTS
echo "========== html_tcpdumpfile CONTENTS ============="
cat $html_tcpdumpfile
echo "========== html_tcpdumpfile CONTENTS ============="
Problem
Sometimes, the tcpdump command doesn't capture anything, and at other times it does. There are no error messages when it fails to capture.
What I've tried So far
As you can see, I've tried to change the location of the dump file between the remote machine and the local one. That doesn't seem to make a difference.
I've proven that TCP traffic is ALWAYS generated... each time I run the script because I have another ssh session open and i can see the traffic being generated. It's just that my script intermittently fails to capture it.
I've tried to increase the timeout value on the tcp session to something huge to make sure I give it enough time. But I don't think that's the problem.
Any suggestions would be appreciated. Thanks.
EDIT 1
I tried to introduce a sleep in between launching each subprocess:
pids=()
for index in ${!test_steps[@]}; do
(echo "${test_steps[$index]}" | bash) &
sleep 5
pids[${index}]=$!
echo "$pids[${index}] is the pid"
done
But that doesn't make a difference either.
EDIT 2
I changed the tcpdump command to look like this:
test_steps=(
"{ ssh remotemachine.mydomain.net \"timeout -t 30 tcpdump -nlc 100 -i eth0 port 5060 \"> $rtf_tcpdumpfile; }"
"{ ssh remotemachine.mydomain.net \"timeout -t 20 tail -f /var/log/messages \" > $syslog; }"
"{ ssh remotemachine.mydomain.net \"timeout -t 15 cat /tmp/htmlemail.txt | /etc/postfix/process_email.py \"; }"
)
The tcpdump still fails to capture intermittently, but ... what's interesting is that the syslog is always successfully captured. (the python script actually writes to the syslog when it's invoked and so I can see /prove that the script is working)
remotemachine.mydomain.net\"timeout
where I'd expect there should be one (betweennet
and\"
).-l
(lowercase ell) option, to make it line buffered. Otherwise you'll only get output every 4KB or so.