View Single Post

  #1 (permalink)  
Old 03-04-2008, 01:31 AM
monteo7 monteo7 is offline
Junior Member
User
 
Join Date: Mar 2008
My distro: Debian
Posts: 2
Rep Power: 0
monteo7 is on a distinguished road
Default interacting with expect

hello, I'm calling expect from a shell script and am having trouble interacting once connected. The below script autmates the login and after running a command (through ssh), I want to interact and am unable to. Any thoughts would be appreciated. Thanks. And sorry for the long code snippet, but I thought full code would be appreciated.

Quote:
#!/bin/ksh

#int main()
clear
#variables
username=
password=
host=
asRoot=
interactive=
cmd=

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
until [[ "$asRoot" = [y/Y/n/N] ]];do
print -n "Do you want to run command as root? [y/n]: "
read asRoot
done
until [[ "$interactive" = [y/Y/n/N] ]];do
print -n "Do you want interact after command completion? [y/n]: "
read interactive
done
until [[ "$cmd" != "" ]];do
print -n "Enter command to execute: "
read cmd
#remove leading/trailing whitespace
cmd=$(echo $cmd|sed -e 's/^ *//g;s/ *$//g')
cmd="\"$cmd\""
done
#display what were doing:
echo "you are running: "
echo ""
echo "Command: $cmd"
echo "As: `if [[ "$asRoot" = [y/Y] ]]; then echo root; else echo $username;fi`"
if [[ "$interactive" = [y/Y] ]]; then echo Interactively;fi
if [[ -e "$serverFile" ]]; then echo "On host(: ";cat $serverFile; else echo "On host: $host";fi
echo ""
print -n "Press enter to continue or ctrl c to exit"
read
prompt="[>/%/#/$]"
echo ""
host=$(echo $host|sed -e 's/^ *//g;s/ *$//g')
if [[ "$asRoot" = [y/Y] ]];then
if [[ "$interactive" != [y/Y] ]]; then
sshcmd="ssh $LOGNAME@$host {sudo su - root -c $cmd}"
exp=$(expect -c "
set timeout 10
match_max 100000
spawn $sshcmd $LOGNAME@$host {sudo su - root -c $cmd}
expect {
stty -echo
"*yes/no*" { send \"yes\r\"; exp_continue }
"*assword:" { send \"$password\r\"; exp_continue }
}
stty echo
exit
")
else
sshcmd="ssh $LOGNAME@$host {sudo su - root -c $cmd}"
exp=$(expect -c "
set timeout 10
match_max 100000
spawn $sshcmd $LOGNAME@$host {sudo su - root -c $cmd}
expect {
stty -echo
"*yes/no*" { send \"yes\r\"; exp_continue }
"*assword:" { send \"$password\r\"; exp_continue }
stty echo
interact
}
stty echo
exit
")
fi #end interactive
else
#running as user, not root
sleep 2
if [[ "$interactive" != [y/Y] ]]; then
exp=$(expect -c "
set timeout 10
match_max 100000
spawn ssh $username@$host $cmd
expect {
stty -echo
"*yes/no*" { send \"yes\r\"; exp_continue }
"*assword:" { send \"$password\r\"; exp_continue }
}
stty echo
exit
")
else
exp=$(expect -c "
set timeout 10
match_max 100000
spawn ssh $username@$host $cmd
expect {
stty -echo
"*yes/no*" { send \"yes\r\"; exp_continue }
"*assword:" { send \"$password\r\"; exp_continue }
}
stty echo
#interact
exit
")
fi #end interactive
fi #end as root
#display to user and log any output except that which expect produces
if [[ "`echo "$exp"|grep -v spawn|grep -v password|grep -v Password|grep -v RSA|grep -v rsa`" = "" ]]; then
echo " $host: unable to connect"
echo "####$host.`date +"%H%M%m%d%y"`####" >> autossh.log
echo " $host: unable to connect" >> autossh.log
else
echo " $host: `echo "$exp"|grep -v spawn|grep -v password|grep -v Password|grep -v RSA|grep -v rsa`"
echo "####$host.`date +"%H%M%m%d%y"`####" >> autossh.log
echo " `echo "$exp"|grep -v spawn|grep -v password|grep -v Password|grep -v RSA|grep -v rsa`" >> autossh.log
fi

Last edited by monteo7; 03-04-2008 at 03:19 AM.. Reason: correcting code, removing function not called in this script.
Reply With Quote