Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parallel processing in Expect

Tags:

expect

tcl

The code below works well right now; however, it's serially executed. I'd like to be able to roll through the source_list file until I get a max number of sessions and let them all complete and feed the results back to this parent script. Is that possible or would it require me to change the script I'm calling to feed back the results? I've looked at the fork command but it somewhat eludes me.

set source_list [lindex $argv 0]

set device_list [open $source_list r]
while {[gets $device_list ipaddress] != -1} {
spawn "./ios-upgrade.exp" 0 $ipaddress username password image-file MD5hash ftp-server
expect eof
}
close $device_list
like image 545
rsaturns Avatar asked Jul 29 '26 05:07

rsaturns


2 Answers

All you really need is:

exec ./ios-upgrade.exp 0 $ipaddress username password image-file MD5hash ftp-server &
## No need for an expect statement here since you didn't spawn...

The & character at the end of exec backgrounds the script in the shell so your loop will not be blocked for the return code.

However, you are sending the username and password as CLI args, so they will also show up in the process table when someone does ps auxw or its ilk. I would store the username / passwords in the same file with your IP address, and use:

exec ./ios-upgrade.exp 0 $ipaddress image-file MD5hash ftp-server &
## No need for an expect statement here since you didn't spawn...

After ios-upgrade.exp is done, have it write a file named something like ip_4_1_12_18.out and iterate through the directory until you have received status for all the ip addresses.


OP's additional information:

Turns out the missing piece of information to the above answer was to eval the variable so it was passed properly.

exec ./ios-upgrade.exp 0 {*}$ipaddress image-file MD5hash ftp-server &

*Note that {*} only works with TCL 8.5 and above.

Found the answer in the following: How to add a variable amount of arguments to exec in tcl?

like image 61
Mike Pennington Avatar answered Aug 02 '26 07:08

Mike Pennington


Turns out the missing piece of information to the above answer was to eval the variable so it was passed properly.

exec ./ios-upgrade.exp 0 {*}$ipaddress image-file MD5hash ftp-server &

*Note that {*} only works with TCL 8.5 and above.

Found the answer in the following: How to add a variable amount of arguments to exec in tcl?

like image 43
rsaturns Avatar answered Aug 02 '26 05:08

rsaturns