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?
I think that what you're looking for is something like this:
spawn ssh name@host
set pid [exp_pid]
puts "PID: $pid"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With