Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Assigning Expect script results to a variable???

Posted by shafa.fahad (shafa.fahad), 22 August 2006
Hi,

I am new to Expect and TCL...

I am developing an automated program which logs into remote machine using SSH and executes PS command to find status of process running on the remote machine...

for this purpose i have written a expect script in the following way

send_user "Enter Username@System to be logged in: "
expect_user -re "(.*)\n" {set USRSYS $expect_out(1,string)}
spawn ssh $USRSYS
expect Password: {
       stty -echo
       expect_user -re "(.*)\n"
       send_user "\n"
       send "$expect_out(1,string)\r"
       stty echo
          }
expect -re " $"
send "ps -awwwx|head\r"
expect -re " $"
set val $expect_out(buffer)
send_user $val
send "\n"

expect -re " $"
send "echo $?\r"
expect -re " $"
set retval $expect_out(buffer)
send_user $retval
send "\n"

expect -re " $"
send "exit\r"

when i execute the script.... the values which i get for VAL and RETVAL are

----------------------------------------------
ps -awwwx|head
 PID  TT  STAT      TIME COMMAND
   1  ??  S<s    0:13.25 /sbin/launchd
  23  ??  Ss     0:00.02 /sbin/dynamic_pager -F /private/var/vm/swapfile
  27  ??  Ss     0:01.99 kextd
  31  ??  Ss    66:05.18 /usr/sbin/configd
  32  ??  Ss     0:30.90 /usr/sbin/coreaudiod
  33  ??  Ss     0:07.38 /usr/sbin/diskarbitrationd
  34  ??  Ss     0:00.22 /usr/sbin/memberd -x
  35  ??  Ss     0:09.07 /usr/sbin/securityd
  36  ??  Ss     0:17.50 /usr/sbin/notifyd
JoseGs-iMac-G5:~ jose$
-------------------------------------------
echo $?
0
JoseGs-iMac-G5:~ jose$
-------------------------------------------

The problem is....
the $expect_out(buffer) echos the command and the prompt values which gets appended to the results which are assigned to the Variable...

The expected result is that VAL and RETVAL should have only the results of the command.. and NOT the command and prompt appended to it...

Please help me out in this case...


Posted by admin (Graham Ellis), 22 August 2006
Your expect_out(buffer) will include everything that's received including the echo of the commands you send.   That's the correct behaviour.  

You have several things you can do to isolate the response string.   The neatest way is to expect your echoed input text before you expect the response - so you would have the sequence send - expect - expect. An alternaive is to expect everything back and then use string handling commands to extract the sections that you want.

Posted by shafa.fahad (shafa.fahad), 23 August 2006
Thanks for the earliest reply Graham..

BTW I tried with send - expect - expect scenario in the following way...

expect -re " $"
send "ps -awwwx|head\r" --> 1
expect "ps -awwwx|head\r" --> 2
expect -re " $"
set val $expect_out(buffer)
send_user $val
send "\n"

but when i run, it seems that the script stops after the "send" command without proceeding futher...

ps -awwwx|head
 PID  TT  STAT TIME COMMAND
   1  ??  S<s    0:13.25 /sbin/launchd
  23  ??  Ss     0:00.02 /sbin/dynamic_pager -F
  27  ??  Ss     0:01.99 kextd
  31  ??  Ss    66:05.18 /usr/sbin/configd
  32  ??  Ss     0:30.90 /usr/sbin/coreaudiod
  33  ??  Ss     0:07.38 /usr/sbin/diskarbitrationd
  34  ??  Ss     0:00.22 /usr/sbin/memberd -x
  35  ??  Ss     0:09.07 /usr/sbin/securityd
  36  ??  Ss     0:17.50 /usr/sbin/notifyd
JoseGs-iMac-G5:~ jose$   ----> stops here

because of this the script does not logs out from the remote machine...

Please let me know if i have to add something to the above script...
Thanks in Advance

Posted by admin (Graham Ellis), 23 August 2006
Although you send \r on the end of your command, I suspect you need to expect \n, as that's often changed in the echo.

Posted by shafa.fahad (shafa.fahad), 23 August 2006
Thanks Graham...

I tried it and am getting unexpected results....

Could u tell when exactly i shoud expect \n? I am not very much clear with it....

That would be of great help, Thanks in advance

Posted by admin (Graham Ellis), 23 August 2006
\n is a new line character - you'll find on the output that you get both a \r and a \n at the end of each line (I think the \n first) even though your inputs only include a \r.

I do note that your expect scripts are looking for the " $" regular expression, and this is liable to be unstable - i.e. I would anticipate it would match once one way, then differently the next time. That's because there will sometimes be a space at the end of the expect buffer when it'c checked by expect even at intermediate times - something can end with a space, but then have other data appended to it and a condition that did match will no longer match.   The standard that's suggested is that you look for a unique striing.

You say you are getting unexpected results.  I'm nor suprised, but I can't really offer much more advise that what I've written above unless you describe what those unexpected results are.  Techincal support folks dread getting past a report like yours that just says does not work on it ... it somewhat lacks the evidence needed to provide the help that's being asked for.

Posted by shafa.fahad (shafa.fahad), 23 August 2006
Thanks for the help Graham.... I appriciate your patience.

I understand that what i sent wont be suffcient to provide necessary help.... I shall try with what you suggested....

Regards,
Fahad



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho