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
expect, automate ssh login

Posted by monteo7 (monteo7), 4 March 2008
Hello, I'm trying to automate the login of ssh and then interact when a prompt is reached.  I don't know what I'm expecting while logging in (password prompt or ssh key) so loop through these.  This script prompts you for a username, password and host, then runs expect.  I want to exit the login loop once a prompt is reached,  run the "uname" command and then (hopefully) interact.

code button not working for me, will these code tags work?

Code:
#!/bin/ksh

#get username, password and host information from user
username=
password=
host=

clear  #clear screen

until [[ "$username"  != "" ]];do
 print -n "Enter username: "
 read username
done

until [[ "$password" != "" ]];do
 print -n "Enter your password: "
 stty -echo
 read password
done

#return echo to terminal
stty echo

until [[ "$host" != "" ]];do
 echo ''
 print -n "Enter remote host: "
 read host
done

#initiate ssh connection and loop through possible prompts (accept key or password)
# once prompt is reached run uname command and then interact
expect_output=$(expect -c "
 set prompt "(%|#|\\\$) $"      ;# default prompt
 set timeout 10
 match_max 100000
 spawn ssh $username@$host
 expect {
   stty -echo
   "*yes/no*" { send \"yes\r\"; exp_continue }
   "*assword:" { send \"$password\r\"; exp_continue }
   stty echo  
   $prompt { send \"uname\r\" }
 }

 interact
 exit
}


#display output from expect
echo ""
echo $expect_output

#clean up
username=
password=
host=
expect_output=

#exit script
exit0



Posted by admin (Graham Ellis), 4 March 2008
You might find it easier to put your expect script into a separate file and test it on its own rather than embedding it within a Korn shell script.  Tcl quoting is bad enough without adding a ksh level too!

Posted by monteo7 (monteo7), 5 March 2008
I have put my code in a separte expect script and it still fails because the issue is the expect loop, not ksh.  If I put the following into expect it still fails, again, not sure how to exit expect loop and initiate interaction.  Are there for or while loops in expect?  I could possbile do something like this:
for (i=0; i<2;i++):  then loop through twice and automatically exit...??

Code:
set timeout 10
match_max 100000
spawn ssh username@hostname
       stty -echo
       expect {
         "*yes/no*" { send \"yes\r\"; exp_continue }
         "*assword:" { send \"$password\r\"; exp_continue }
         "*$" {interact}
         }
       stty echo
       exit
     ")


Posted by admin (Graham Ellis), 5 March 2008
exp_continue tells it to loop.  If you remove the exp_continue, it will break out of the loop.  And if you want to keep expecting until you have some counter up to (say) 2, write of the form

set counter 0
while {$counter < 2 } {
expect ..... to include incr counter were appropriate
}



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., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho