nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

interacting with expect

This is a discussion on interacting with expect within the Shell scripting forums, part of the Development/Scripting category; hello, I'm calling expect from a shell script and am having trouble interacting once connected. The below script autmates the ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 03-04-2008, 12:31 AM
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 02:19 AM. Reason: correcting code, removing function not called in this script.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-04-2008, 04:42 PM
Junior Member
User
 
Join Date: Mar 2008
My distro: Debian
Posts: 2
Rep Power: 0
monteo7 is on a distinguished road
Default

OK, is my code too long that nobody is answering it or is it that noone knows the answer? In short here's my problem: I don't know how to interact when calling expect through shell script. Please note in this shortened version you will need to change 'someHost' to an actual ip address or host name in order to establish an ssh connection and optionally $LOGNAME to a valid user name (if not on unix).

I'm trying to automate the login only and when a prompt is reached exit login loop and interact with user.

Quote:
#!/bin/ksh
set prompt "(%|#|\\\$) $" # default prompt

expect_output=$(expect -c "
set timeout 10
match_max 100000
spawn ssh $LOGNAME@someHost
expect {
stty -echo
"*yes/no*" { send \"yes\r\"; exp_continue }
"*assword:" { send \"$password\r\"; exp_continue }
$prompt { send \"r\" }
}
stty echo
expect -re $prompt { send \"uname\r\"
interact
exit
}
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
expect about helps pls samuel Shell scripting 0 03-05-2008 12:05 PM
Expect and tcl - manymaint.exp script Intelligen Shell scripting 11 11-21-2007 02:48 PM
Interacting with a Linux Process through Scripts manikandakumar Shell scripting 1 08-17-2007 06:49 PM


All times are GMT +5.5. The time now is 04:34 AM.


Powered by vBulletin® Version 3.7.3 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36