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))
Problem with Expect in shell script

Posted by johny (johny), 6 March 2008
Hai

I have a shell script from where i call an expect to ssh to different host and execute the command..

expect script:-
#!/usr/bin/expect -f
set ip [lindex $argv 0]
spawn ssh -X janab@$ip
stty -echo
expect {"connecting (yes/no)?" { send  "yes\r" ; exp_continue }
       "password:" { send  "pbaxsf\r"; exp_continue  }
       "]$"{ send "df -k | grep \mnt\r" }
}

Shell script:-
#!/bin/sh
for i in falcon skylark bulbul mynah
do
expect janta $i
#echo $i
done

when i run the shell script the password is not been sent and looks as below..
[janab@skylark ~]$ sh banta
spawn ssh -X janab@falcon
janab@falcon's password: spawn ssh -X janab@skylark
janab@skylark's password: spawn ssh -X janab@bulbul

What am i doing wrong here ...

Please help me as this urgent..


Posted by admin (Graham Ellis), 6 March 2008
Try taking out the stty -echo so you can see what's happening

Standard text:
Please do not ask for urgent or a.s.a.p. assistance here - we're not set up to provide a rapid response service, and in any case we answer questions as soon as we practically can.  Customers of Well House Consultants (who host this site) are very welcome to email me if there are any truely urgent / critical issues and I will do my very best to help as quickly as I can.  Freeloaders who ask me to prioritise giving them support without charge over and above my "day job" are being very cheaky!

Posted by johny (johny), 7 March 2008
Hi

Thank you and sorry for posting it to be urgent..

As advised have removed the line stty -echo
and could get it working.

But it seems to be sending the df -k command n number of times to a single host when pressed cntrl+c this gets out...but when i remove the exp_continue in the below line this will not work

"*]$*" { send "df -k | grep /mnt\r"; exp_continue }

Code :-
#!/usr/bin/expect -f
set ip [lindex $argv 0]
spawn ssh -X janab@$ip
expect {
"*yes/no*" { send "yes\r"; exp_continue }
sleep 1
"*assword:" { send "XV_4gTdo\r"; exp_continue }
sleep 1
"*]$*" { send "df -k | grep /mnt\r"; exp_continue }
"*]$*" { send "exit \r" }
}


O/p is as below...
Last login: Thu Mar  6 20:49:04 2008 from falcon.kwi.intra.kyocera-wireless.com
[janab@falcon ~]$ df -k | grep /mnt
/dev/sda7             87722116  34512916  48681284  42% /mnt/local
[janab@falcon ~]$ df -k | grep /mnt
/dev/sda7             87722116  34512916  48681284  42% /mnt/local
[janab@falcon ~]$ df -k | grep /mnt
/dev/sda7             87722116  34512916  48681284  42% /mnt/local
[janab@falcon ~]$ df -k | grep /mnt
/dev/sda7             87722116  34512916  48681284  42% /mnt/local
[janab@falcon ~]$ df -k | grep /mnt
/dev/sda7             87722116  34512916  48681284  42% /mnt/local

Please help me...


Posted by johny (johny), 7 March 2008
Hi sorry for my previous post...

I could get that working by the below code...

#!/usr/bin/expect -f
set ip [lindex $argv 0]
spawn ssh -X janab@$ip
expect {
"*yes/no*" { send "yes\r"; exp_continue }
sleep 1
"*assword:" { send "XV_4gTdo\r"; exp_continue }
}
expect "*]$*"
send "df -k | grep \mnt\r"
expect "*]$*"
send "exit \r"
expect eof

But now what all i need is to capture the output of the df -k and write that into a file in shell script...

Is this possible...? if yes please guide me through....

Regards
Johny

Posted by admin (Graham Ellis), 7 March 2008
The $expect_out(string) variable holds the output you require after each expect; you can open a file with open and write the contents of that variable there with puts



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