expect interact with <<EOF not work properly - shell

i want a auto su script but
expect interact with <
this script work properly
#!/usr/bin/expect
set timeout 5
spawn su
expect "Password:"
send "123456\r"
interact
#expect "# "
#send "whoami"
but this not work
#!/bin/bash
su_user=$1
su_pwd=$2
/usr/bin/expect <<EOF
set time 30
spawn su $su_user
expect "assword"
send "$su_pwd\r"
send "whoami\r"
interact
EOF
after run the script the user do not change
why dose interact not work with <

I don't know for certain that this is your problem but I think interact is getting the EOF from stdin and aborting.
I'm having a similar issue trying to do
cmd | expect_script.
As my very simple testcase I'm trying to do 'read x; echo $x' on a remote server and "echo FRED" as my cmd but my script is aborting like this:
running read x; echo this is $x
send: sending "read x; echo this is $x\r" to { exp5 }
spawn id exp0 sent <FRED\n>
interact: received eof from spawn_id exp0
interact: spawn id exp0 not open
(You might find exp_internal 1 useful for debugging)

Related

how to user expect and send with a request and sleep

When the password appeare
I want to use expect to autoinput the result of a request after 5s
macOS shell expect
expect << !
set timeout -1
spawn bundle exec fastlane fastlane-credentials add --username sakura
expect "*Password*"
sleep 5
send `curl http://localhost/a.txt`
interact
!
the sleep can't effect
the curl http://localhost/a.txt used before the Password,I want to after
In tcl language, send command interpret at every first time, so the sleep will not work for interact with your process. Put sleep before expect should work, but U need save password in temp file as a storage. More detail, example codes show as:
#!/usr/bin/expect
set timeout -1
spawn bundle exec fastlane fastlane-credentials add --username sakura
sleep 5
send_user "[exec curl -s http://localhost/a.txt -o /tmp/_passwd.txt]"
set fp [open "/tmp/_passwd.txt" r]
set passwd [read $fp]
close $fp
send_user "[exec rm -f /tmp/tmp/_passwd.txt]"
puts $passwd
expect "*Password*"
send '$passwd\r'
interact

How to make expect nested in the bash script properly?

Here is the expect file which can run succesfully.
#!/usr/bin/expect -f
set host vps_ip
set user test
set loginpwd passwd
set adminpwd passwd
set timeout 300
set prompt "#|>|\\\$"
spawn scp /home/wpdatabase_backup.sql $user#$host:/tmp
expect -nocase "password:"
send "$loginpwd\r"
expect eof
spawn ssh $user#$host
expect -nocase "password:"
send "$loginpwd\r"
expect -re $prompt
send "su\r"
expect "assword:"
send "$adminpwd\r"
expect -re $prompt
send "mysql -u root -pxxxx wpdatabase < /tmp/wpdatabase_backup.sql\r"
expect eof
Here is the bash file:
mysqlword="xxxx"
mysqldump -u root -p$mysqlword wpdatabase > /home/wpdatabase_backup.sql
Now i want to make the expect file nested in the bash file because the bash file is more complicated than expect file ,most lines in the bash file were omitted.
code:
#!/usr/bin/bash
mysqlword="xxxx"
mysqldump -u root -p$mysqlword wpdatabase > /home/wpdatabase_backup.sql
/usr/bin/expect <<EOD
set host vps_ip
set user test
set loginpwd passwd
set adminpwd passwd
set timeout 300
set prompt "#|>|\\\$"
spawn scp /home/wpdatabase_backup.sql $user#$host:/tmp
expect -nocase "password:"
send "$loginpwd\r"
expect eof
spawn ssh $user#$host
expect -nocase "password:"
send "$loginpwd\r"
expect -re $prompt
send "su\r"
expect "assword:"
send "$adminpwd\r"
expect -re $prompt
send "mysql -u root -pxxxx wpdatabase < /tmp/wpdatabase_backup.sql\r"
expect eof
<<EOD
An error occur :
spawn scp /home/wpdatabase_backup.sql #:/tmp
ssh: Could not resolve hostname : Name or service not known
lost connection
send: spawn id exp6 not open
while executing
"send "\r""
Quote the terminator:
cat<<'EOD'
set host vps_ip
set user test
spawn scp /home/wpdatabase_backup.sql $user#$host:/tmp
EOD
Result:
set host vps_ip
set user test
spawn scp /home/wpdatabase_backup.sql $user#$host:/tmp
But how to make the output of result run in bash?
/usr/bin/expect <<cat<<'EOD'
set host vps_ip
set user test
spawn scp /home/wpdatabase_backup.sql $user#$host:/tmp
EOD
The code can't run!
This is because in a here-document with an unquoted terminator like EOD, parameter substitution is performed by the shell. You can see that if you replace /usr/bin/expect with cat:
$ cat<<EOD
set host vps_ip
set user test
spawn scp /home/wpdatabase_backup.sql $user#$host:/tmp
EOD
Result:
set host vps_ip
set user test
spawn scp /home/wpdatabase_backup.sql #:/tmp
So, how to avoid parameter substitution? Quote the terminator:
$ cat<<'EOD'
set host vps_ip
set user test
spawn scp /home/wpdatabase_backup.sql $user#$host:/tmp
EOD
Result:
set host vps_ip
set user test
spawn scp /home/wpdatabase_backup.sql $user#$host:/tmp

perl program not running thorough automated script until it is executed manually first

i am writing code to automate some steps . First it is required to switch user and then run a perl script. Here is my code
if [ -a /try/Test ]
then
su trial -c ". /try/.profile Test"
expect -c 'spawn try1;
send "3\r";
send "1\r";
send "show\r";
interact';
fi
try1 is my perl program which i am trying to call.This script throws this error
couldn't execute "try1": no such file or directory
while executing
"spawn try1"
but once i do this step manually and then run this script then this script runs without nay error.
I think you've already asked about it (and I did answer, didn't I)?
Here's the basic skeleton (make sure to add error/timeout/unexpected output handling):
# collect password
stty -echo
send_user -- "Password: "
expect_user -re "(.*)\n"
send_user "\n"
stty echo
set pass $expect_out(1,string)
spawn sudo sh;
expect -re ": *$";
send -- "$pass\r"
expect -re "\$ *$";
send "echo SETTING PARAMS\r";
expect -re "\$ *$";
send "echo RUNNING MY COMMAND\r";
expect -re "\$ *$";
interact

Trimming output of expect script

I am new to Expect scripts. I have Expect script written in a bash script. The script returns me the statistics I require but along with a lot of other stuff from the terminal. "Is there any way I can get precisely the output of the command only?"
I have spent a day searching various forums but didn't had any luck.
Any sort of help will be appreciated.
Stats = $(expect -c "
spawn ssh $Username#$Host
expect \"password:\"
send \"$Password\r\"
expect \"\\\\$\"
send \"ps -A | grep java\r\"
expect -re \"$USER.*\"
send \"logout\"
")
echo $Stats > someFile.txt
You can turn of logging except for the command output:
expect -c "
log_user 0
spawn ssh $Username#$Host
expect \"password:\"
send \"$Password\r\"
expect \"\\\\$\"
log_user 1
send \"ps -A | grep java\r\"
expect -re \"$USER.*\"
send \"logout\"
"

expect in bash script × 16172

I try to set a expect script in bash.
#!/bin/bash
/usr/bin/expect <<- EOD
set router 192.168.0.251
set user admin
set pass test
set timeout 1000
set filesave [exec date +%m-%d-%Y]
spawn telnet $router
send "\n"
expect "Username:"
send "$user\n"
expect "Password:"
send "$pass\n"
expect ">"
send "en\n"
expect "Password:"
send "$pass\n"
send "term len 0\n"
log_file $router--$filesave.cfg
send "show running-config\n"
expect "end\r"
send "\n"
log_file
send "exit\n"
EOD
cat /Users/test/Desktop/python/$router--$filesave.cfg | grep end
exit 0
I just got this output
./script2
spawn telnet
telnet> telnet>
Try changing your shebang from:
#!/bin/bash
to
#!/bin/expect
and remove:
/usr/bin/expect <<- EOD
And see if that works.
Update: if you need to have expect run as part of your bash script, either encapsulate the expect code in a separate script with a expect shebang and source it from your bash script, or encode it as in the following example:
expect_sh=$(expect -c "
spawn ssh $login#$IP
expect \"password:\"
send \"$password\r\"
expect \"#\"
send \"cd $dest_dir\r\"
expect \"#\"
send \"chmod +x $server_side_script $other_script\r\"
expect \"#\"
send \"./$device_side_script\r\"
expect \"#\"
send \"cat $deploy_count\r\"
expect \"#\"
send \"exit\r\"
")
echo "$expect_sh"
The problem is that bash is interpreting all the $variables before expect sees the script. Thus you're simply spawning telnet with no hostname given. Change:
/usr/bin/expect <<- EOD
to:
/usr/bin/expect <<- 'EOD'
This has the effect of single-quoting the entire here-document.
http://www.gnu.org/software/bash/manual/bashref.html#Here-Documents
Note also that the next bash command (cat /Users/...) relies on variables defined in Expect -- they are not defined in bash. Try this
#!/bin/bash
export router=192.168.0.251
export filesave=$(date +%m-%d-%Y)
/usr/bin/expect <<- 'EOD'
set router $env(router)
set filesave $env(filesave)
# the rest stays the same

Resources