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?
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