Expect - assigning UNIX command output to a variable

Hi,

I'm writing a script that connects through ssh (using "expect") and then is supposed to find whether a process on that remote machine is running or not. Here's my code (user, host and password are obviously replaced with real values in actual script):

#!/usr/bin/expect
set timeout 1
spawn ssh user@host
expect "*assword"
send "password\r"

expect "$"
#now I would do something like : 
# if [ -z "\$(pidof some_process)"]
# or set found_pid $(pidof some_process)

I'm at loss how to set a variable using the things that pidof would output and make a conditional statement with it...

I've googled it for quite a some time but couldnt find an answer... Thanks in advance!

When you use keys for passwordless authentication, feeding a script into ssh becomes as easy as just feeding the script into it:

ssh user@host exec /bin/sh <<EOF
if [ -z "\$(pidof some_process)"]
then
...
fi
EOF

This is because ssh is designed to work this way. It's not designed to have passwords forcefed into it -- it's designed to prevent you from doing that -- which is why you needed to install the expect brute-forcing utility to do so.

Well, I know about keys, but I really need to know how to get variables like $pidof work in expect scripts

Don't really know what you intend to do with the variable once you have it but this example might get you started:

#!/usr/bin/expect -f

set timeout 2
spawn -noecho ssh user@host
log_user 0
expect "*assword"
send "password\r"

expect {\$}
send "pidof some_process\r"
expect -re {\r\n(.*)\r\n(.*)\$}
set pidlist $expect_out(1,string)

if {[string length $pidlist] > 1} {
   puts "Found PID(s): $pidlist"
} else {
   puts "No PIDs found"
}

send "exit\r"
expect eof

Here we are matching on a $ symbol for the command prompt. Remove log_user 0 to see actual output while debugging.