Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

expect ssh login help

Status
Not open for further replies.

monteo7

Programmer
Mar 4, 2008
6
US
Hello, I'm trying to automate the login of ssh and then interact when a prompt is reached. I don't know if I'll be getting an "accept key" prompt or a password prompt, so I want to loop through these and when any unix prompt is reached, then exit the loop, execute a command and then interact with the user. 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.
Any help would be greatly appreciated, thank you.

I didn't see any code tags, will this 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
 
correction, the "accept key" from above should read "key fingerprint", which is stored in known_hosts file.
 
I can help you with the expect portion of this but I'm not much of a ksh guy.

Have you taken a look at the comp.lang.tcl archives? There may already be an answer to your issue.
 
macd68, thanks for your help and yes I have looked at comp.lang.tcl and only found things like the following, however I'm not entering my loop in that way.
Code:
for {set i 1} {i<10} {incr i} { 
         send a commanda 
         expect { 
         1 {puts 1} 
         2 {puts 2} 
         3 {........} 
         } 
         send a commandb 
}

Not a ksh guy...well me neither but I'm restricted to it. However, this isn't a ksh question, I can't exit the loop even through an expect script (#!/bin/expect):
Code:
expect { 
    stty -echo 
    "*yes/no*" { send \"yes\r\"; exp_continue } 
    "*assword:" { send \"$password\r\"; exp_continue } 
    stty echo   
    $prompt { send \"uname\r\" } 
  } 
  interact
  ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top