Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-05-2012, 03:13 PM   #1
LQ Newbie
Registered: Aug 2011
Posts: 7

Rep: Reputation: Disabled
Expect: Handling multiple spawned processes from a single script

Hi All,

I am trying to work with Expect scripts and iam trying to write a script to do the following (based on the info that we can handle multiple processes within a single expect script):

1. From the mainpc (running the expect script), spawn 2 SSH connections/processes - one to pc1 and another to gw1 - the ssh connections are opened and supposed to run simultaneously from mainpc

2. On PC3, a udp-echo server is started and running already. This is done manually and no script is involved

3. Now on pc1, after ssh login, start a udp-echo client session to continuosly send traffic to remote udp-echo-server thru the Gw1. This traffic is to be sent continuously. Hence this ssh session from mainpc is not supposed to be disconnected till it is explicitly exited by the script from mainpc.

4. Simultaneously, on GW1, after ssh login, while the udp traffic between pc1 and pc2 is passing thru the Gw, do some action such as say, start reading the conntrack connections file using the command "cat /proc/net/ip_conntrack" and check for the pattern, say, for example "udp...ESTABLISHED..." for verifying the udp connection establishment for the traffic passing thru the GW1 and Also some more verification steps on GW1. This ssh session from mainpc should remain open

5. Now come back to PC1, stop the udp-echo-client traffic and now start a tcp connection to pc2, such as a ssh connection, or a ftp connection, etc.

6. So now going back to GW1 again, check using the already opened ssh connection, the ip_conntrack file for the "tcp...ESTABLISHED..." pattern and other verification steps

7. Now Going back to PC1 ssh connection, stop the traffic being sent, logout of the ssh session, and close the spawned ssh-connection process to pc1 in the script

8. Next exit/logout of the ssh connection to GW1, and close this spawned process too

9. write a PASS result for this test in a file and then start a similar iteration once again for a different test-result

linx-fedora-pc1----xxx.1[openwrt-gw1]yy.1-----lnx-fedora-pc3 | (
#On the lnx-fdr-mainpc

#!/usr/bin/expect -f
set timeout -1
# for debugging only
#exp_internal 1
# first spawn the 2 processes
spawn $env(SHELL)
set pc1id $spawn_id
spawn $env(SHELL)
SET gw1id $spawn_id
#start the ssh connections
send -i $pc1id "ssh\r"
send -i $gw1id "ssh\r"
#process the initial ssh setup on pc1
expect -i $pc1id "password: "
send -i $pc1id "config123\r"
expect -i $pc1id ".*root*"
# process the initial ssh setup on gw1
expect -i $gw1id "password: "
send -i $gw1id "config123\r"
expect -i $gw1id ".*root*"
# now start executing the commands on pc1
....<send a command for starting the udp-echo client traffic on pc1>.....
....<expect some traffic pattern output for above command on PC1 in the ssh connection session
# simulataneously, check for the successfull connection
# establishement on GW1
....<send a command to read the ip_conntrack file using "cat /proc/net/ip_conntrack" command
----< expect for ESTABLISHED ...pattern in the file...>
# again go back to pc1, do something else

# back to gw1 and check the next result in some action

# exit out of the pc1 ssh session and close the spawned #process

send -i $pc1id "exit\r"
close -i $pc1id

# exit out of gw1 ssh session and close the spawned process

send -i $gw1id "exit\r"
close -i $gw1id

puts "This is the end of this test iteration\n"

Now i have started of with a simple steps, and iam observing the following issues:

1. The script seems to stop/hang after a ssh connection is opened to pc1 and i am only observing the "pc1@root#" prompt

2. It is not moving ahead with the other required operation of openning another ssh connection to GW1...and the other subsequent operations required for this test

3. Where am i going wrong with spawning and handling multiple processes?

4. My mainpc and other pc's are running linux-fedora-13 (and i installed expect and tcl using "yum install expect*" and "yum install tcl*" from the internet)

5. Or am i getting too ambitious in wanting to do everything thru a single script. But that' the idea behind automation right?

i would be very greatful for all your help. Please do forgive me for such a lengthy post.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding child processes spawned from screen worm5252 Linux - Server 2 08-25-2010 07:50 AM
Can expect return control of a spawned process to a shell script? duffrecords Programming 4 04-28-2010 02:33 PM
Expect: Prevent output from spawned process to appear on stdout jlinkels Programming 3 08-12-2009 09:20 PM
expect says it cant send the string to the spawned command exceed1 Programming 0 01-17-2009 06:51 AM
vpnc spawned from "expect" killed upon exit saravkrish Linux - General 1 01-29-2007 08:17 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:07 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration