| ||||||||||||||
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:
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:
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:
Here is what the "Bash" code looks like. Code:
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'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:
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.
|
| |||||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |