Training, Open Source computer languages

This is page http://www.wellho.net/forum/The-Tcl- ... guage/expect-automate-ssh-login.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
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))
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.

© 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