Search code examples
shellexpectspawn

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?


Solution

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

    spawn ssh name@host
    set pid [exp_pid]
    puts "PID: $pid"