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 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Tcl & Expect

Posted by pelon (pelon), 24 February 2005
Seeking help with "expect" & Tcl.

We are creating a simple script that "Login" from one server to another server, runs a simple comand that returns either 0 (yes) or 127 (no) answer.

We would like to pass the result back via a variable to first server to be used in an "if statement".

Is it possible..?  If yes, how?  If no, options?

Posted by admin (Graham Ellis), 25 February 2005
I think this is the sort of thing you're looking for:

Code:
log_user 0
spawn ssh -l graham 192.168.200.66
expect "sword: "
exp_send "abc123\r"
expect {$ }
exp_send "grep -s yawn atnight\r"
expect {$ }
exp_send "echo \$?\r"
expect {$ }

puts "You got"
puts $expect_out(buffer)
puts "Completed"


On the machine you're running from, I've spawned an ssh session onto the remote server I'm interested in, logged in within that session, run a command, echoed its return status which I've then printed out on the machine that invoked the connection.  You can put $expect_out(buffer) through any of the normal tcl commands to test it ... but note that it will contain the response you're looking for wrapped up in the various echoes back of the ssh exchange.

P.S. This is JUST an example. It's going to need changing to reflect your machine details and passwords, which shell the remote machine is running, what prompt the remote machine issues, etc.   I suggest you remove the log_user line while testing so that you can spot where any hangups occur. See Don Libes' book on Expect for a lot more details.

Posted by pelon (pelon), 25 February 2005
Thank you for the example.  Yes, I have the book and it been very helpful.

The only other area I'm a little confuessed on is passing what's in the $expect_out(buffer), back to our Bash script.

Any suggestion?

Posted by admin (Graham Ellis), 26 February 2005
on 02/25/05 at 21:53:06, pelon wrote:
The only other area I'm a little confuessed on is passing what's in the $expect_out(buffer), back to our Bash script.

Any suggestion?


Run your expect program from within your bash script using a command like
        remoteanswer=`expect wotsit`
(those are back quote characters) where wotsit is the name of your expect script and remoteanswer is the name of the shell variable that you're setting in your bash shell.

use puts within your expect script to output whatever it i syou want to appear in the shell variable.

Posted by pelon (pelon), 28 February 2005
We have it working to a point, but the result is mixed.

The result needs to be either "0 or 100", which we are getting.  However the bash script errors with [: Too many arguments.  

When we look at the captured output below we are returning everything expect captured.  We are not sure if its because string which we believe that all expect returns.  Not sure

[myname@node1 myname]$ 100
' Kernel 2.4.21-4.ELsmp on an $'i686\r' login: $'myname\r' Password: $'\r' Last login: Mon Feb 28 15:18:12 from $'node2\r' '[' $'100\r' '[myname@node1' 'myname]$' 100 == 0 ']''[myname@node1' 'myname]$' echo '$?
test_ssh: line 15: [: too many arguments


Here is what our "Expect" code looks like.

Code:
set timeout -1
# log_user 0
spawn telnet xxx.xxx.xxx.xxx
expect "login: "
exp_send "myname\r"
expect "Password: "
exp_send "123456\r"
expect {$ }
exp_send "d3 -q > /dev/null\r"
expect {$ }
send "echo \$?\r"
expect -re "\r\n(.*)\r\n"
puts $expect_out(1,string)
exit



Here is what the "Bash" code looks like.

Code:
d3Status=
                                                                                   
if [ $HOSTNAME != "node1.peacock.net" ]; then
       d3Status=`expect /root/test2.exp`
       if [ $d3Status == 0 ]; then
               echo "D3 is running"
               sleep 8
               exit 1
       fi
       #
else
       d3Status=`expect /root/test2.exp`
       if [ $d3Status == 0 ]; then
               echo "D3 is running"
               sleep 8
               exit 1
       fi
fi
sleep 5
exit 0



Posted by admin (Graham Ellis), 1 March 2005
I think you've correctly identified your problem - you're substituting more than a simple numeric result into the bash script - note my comment of earlier:

on 02/25/05 at 04:04:23, Graham Ellis wrote:
You can put $expect_out(buffer) through any of the normal tcl commands to test it ... but note that it will contain the response you're looking for wrapped up in the various echoes back of the ssh exchange.

P.S. This is JUST an example. It's going to need changing to reflect ...


You've two choices.  

1. You could modify your bash script so that it copes with the whole return string (my first thought at this early hour would be to put the output for a file and "grep -c" it find out how many times "100" occurs

2. You could use a regexp command in your tcl to establish if the string "100" occurs withing the buffer of text returned.   If you've not come across regexp before, here's an example - and I've put a square bracket in the text string in this example!

Code:
% set rs1 {This is a great [ fun test of 100}
This is a great [ fun test of 100
% regexp 100 $rs1
1
% regexp 200 rs1
0
%


More on regular expressions at http://www.regularexpression.info/ which is a sister site of ours.



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., 2021: 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