Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to execute a unix statement in Expect script and get an output without having to include the interact statement.The unix statement outputs rsize value for a process. I haven't programmed in Expect before. This is my code:

 set some_host "some host"
 set Mycmd "top -l 1 -stats pid,rsize,command | grep Process_Name| awk '{print \$2};'"
 spawn telnet localhost $some_host
 expect "login:"
 send "myDevice\r"
 expect "Password:"
 send "$password\r"
 expect "\$"
 send "$Mycmd\r"   

If I don't include the interact statement, I don't get any output. How do I get this to work so that I get the correct rsize value as the output?

share|improve this question
Telnet. shudders. – EEAA Feb 24 '12 at 21:59
For one thing I certainly would not use telnet....maybe a keyed login version of ssh to send the command. – mdpc Feb 24 '12 at 21:59
Feels like 1992 in here all of a sudden. – Kyle Smith Feb 24 '12 at 23:53
up vote 3 down vote accepted

Why not just use the output of ps?

$ ps -p <pid> -o rss | egrep '[0-9]'

Remotely, you can do this over ssh:

$ ssh user@host ps -p <pid> -o rss | egrep '[0-9]'
share|improve this answer
Thanks @ErikA: I ended up using ssh. Had to set up ssh keys for password-less SSH login. – smokinguns Feb 25 '12 at 1:43

You can use any one of these methods, each has slightly different results.

expect -re "*\n"
expect "%"

You should also look at the meaning of match_max which controls how much will be matched.

after the results are caught, you will want to look the results

puts "$expect_out(buffer)"

see for some great examples.

share|improve this answer
Will definitely try this.. – smokinguns Feb 25 '12 at 1:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.