Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the sub-process pid in the expect shell script, after spawn

Part of the script a.exp:

#!/usr/bin/expect
# ..... 
spawn ssh -qTfnN -D $port $user@$host
expect "*assword:*"
# .....

How can I get the pid of sub-process "ssh".

If I execute these in bash shell, not in a script file, the result is

expect1.1> spawn ssh name@host
spawn ssh name@host
2188
expect1.2> 

2188 is the sub-process pid.

And how to use exp_pid command in the expect shell?

like image 926
Yanjiong Wang Avatar asked Feb 13 '12 12:02

Yanjiong Wang


2 Answers

I think that what you're looking for is something like this:

spawn ssh name@host
set pid [exp_pid]
puts "PID: $pid"
like image 178
jcollado Avatar answered Nov 15 '22 03:11

jcollado


This man page says:

spawn returns the UNIX process id. If no process is spawned, 0 is returned.

I.e. you can do:

set pid [spawn ssh -qTfnN -D $port $user@$host]

You can also use exp_pid as jcollado demonstrated. The same man page explains that:

exp_pid [-i spawn_id]
returns the process id corresponding to the currently spawned process. If the -i flag is used, the pid returned corresponds to that of the given spawn id.

like image 37
Oren Milman Avatar answered Nov 15 '22 04:11

Oren Milman