3

I am using Expect to automate VoIP calls for quality measurements.

My script is calling another VoIP client for a given amount of times. Before the calls should be handled, tcpdump should sniff all the packets. While tcpdump occupies the terminal, the VoIP client can't be spawned afterwards. My script essentially looks like this:

set count [lindex $argv 0]   //amount of calls that the VoIP should do
spawn tcpdump -i eth2 -s 0 -w dump1.pcap &
for {set i 1} {$i <= $count} {incr i 1} {
   spawn ./pjsua --config-file=config.txt   //starting VoIP client
   expect "Make call: "
   send "sip:[email protected]\r"   //starting the VoIP call
   sleep 30
   send "h\r"   //stopping the call
   send "q\r"   //closing the VoIP client
   close        //closing the spawned process
}
interact

I thought the & operator behind the tcpdump spawn spawns it in the background. However I get the error message:

send: spawn id exp7 not open
while executing
"send "\r""
invoked from within
"for {set i 1} {$i <= $count} {incr i 1} {
   spawn ./pjsua --config-file=config.txt"

How can I capture the packets in the background with tcpdump and at the same time start the other process and do the VoIP calls?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
timolemow
  • 63
  • 1
  • 5

2 Answers2

4

You may remove the ampersand (&): spawn always operates that way. There's an identifier for each spawned pipeline stored in $spawn_id global. You need to save it to a separate variable after each spawn to be able to reference each with -i flag in the following expect and send operators. See the relevant example under description of these operators in expect(1).

yrk
  • 226
  • 1
  • 6
  • derTimo: does this answer your question? – yrk Oct 15 '14 at 18:25
  • Thank you. I had some other errors in my script, but basically - yes this answers my question. I removed the & from the first spawned process and referenced to it with $spawn_id. The shortened version looks like this: spawn sudo tcpdump -i eth2 -s 0 -w $date/$dumpname set tcpID $spawn_id [...] spawn ./pjsua --config-file=config [...] set pjID $spawn_id send -i $pjID "\r" [...] close close -i $tcpID – timolemow Oct 17 '14 at 09:40
3

I solved my problem by referencing to the process with the $spawn_id variable. My code looks like this in the relevant lines:

spawn sudo tcpdump -i eth2 -s 0 -w $date/$dumpname
set tcpID $spawn_id
[...]
spawn ./pjsua --config-file=config
[...]
set pjID $spawn_id
send -i $pjID "\r"
[...]
close
close -i $tcpID
timolemow
  • 63
  • 1
  • 5