Shell – Expect script: How to handle two processes

expectprocessesshell-script

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:user2@20.0.2.10\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?

Best Answer

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).

Related Question