I have a bash script that is calling an expect script in a for loop. This loop creates users in the bash script.
EXPECT SCRIPT:
# Define variables for arguments passed into the script
set user [lindex $argv 0]
set role [lindex $argv 1]
set email [lindex $argv 2]
set passwd [lindex $argv 3]
# Run the CLI command for users and expect the required output
spawn cli users add -username $user -role $role -email $email
expect "*assword:"
send "$passwd\r"
expect "*assword:"
send "$passwd\r"
expect {
default { send_user "\nERROR: $user was NOT created successfully.
Exiting script.\n"; exit 1 }
"*added to the system successfully*"
}
interact
BASH SCRIPT FOR LOOP:
for role in $user_roles
do
expect_scripts/users.exp $role"1" $role $user_email $password
done
Now, what I want to happen is if the user is not created in the expect script, exit the expect script with an error and fail in the FOR loop. I want the FOR loop to exit completely.
I cannot figure out how to do this, as it seems that my expect script is failing with the desired error but the FOR loop continues. Any help would be greatly appreciated.
A bash for loop will not fail if part of its body returns non-zero. You have to explicitly test for it, and handle it. For example:
for role in $user_roles
do
expect_scripts/users.exp $role"1" $role $user_email $password
if [ $? -ne 0 ]; then
exit 1;
fi
done
You can of course shorten this in a single line as so:
for role in $user_roles
do
expect_scripts/users.exp $role"1" $role $user_email $password || exit 1
done
Also, if you don't want to exit the script you can replace exit 1 with break, which will cause the for loop to terminate, but will not exit the script.
at the top use set -e in the expect script
Related
I'm trying to create an expect script to perform and scp command to copy a given file to a remote machine.
If I run the following script, the scp command fails.
#!/usr/bin/expect -f
set ip_addr [lindex $argv 0]
set in_fname [lindex $argv 1]
set out_fname [lindex $argv 2]
set user [lindex $argv 3]
set password [lindex $argv 4]
set timeout 2
if {[llength $argv] != 5} {
send_user "Usage: ./scp_copy.sh <ip_address> <in_fname> <out_fname> <user> <password>\n"
exit 1
}
spawn scp $in_fname $user#$ip_addr:$out_fname
expect {
(yes/no) {send "yes\r"}
timeout
}
expect {
timeout
{
send_user "FAILED TO PERFORM SCP CMD.\n";
exit 1
}
password:
{
send "$password\r"
}
eof
}
... however, if I remove the 'eof' at the end of the second expect clause and instead create a new expect clause at the end, the scp command works.
expect eof
Please can someone explain this. I'm completely new to expect (and bash) scripts and would be grateful for a simple explanation that helps me understand.
What does 'expect eof' do exactly? Does eof indicate that the spawned process has complete? If so, should I introduce another timeout e.g.
expect {
timeout { exit }
eof
}
What is the difference between the eof being inside the second expect clause and it's own separate expect clause? I was expecting the same effect.
Do I need to call 'close' at the end of an expect script?
Thanks.
I wanted to write up a small expect script and another bash script to save the effort of typing password in ssh connection.
Here goes the scripts:
// ssh.exp, the real workhorse
#!/usr/bin/expect -f
# usage: ./ssh.exp host user pass
set host [lrange $argv 0 0]
set user [lrange $argv 1 1]
set pass [lrange $argv 2 2]
spawn ssh $user#$host
match_max 100000
expect "*?assword:*"
send -- "$pass\r"
send -- "\r"
interact
// the bash script to call it
#!/bin/bash
host='my.host.com'
user='someuser'
pass='Qwerty389$'
./ssh.exp $host $user $pass
However, when the test scripts run, the ssh server always complains that the password is incorrect.
I tried escaping the dollar sign, like pass='Qwerty389\$', but to no avail.
Put the debug statement exp_internal 1 into the expect script, and it shows that the password sent is:
send: sending "{Qwerty389$}\r" to { exp6 } // without escaping $
send: sending "{Qwerty389\$}\r" to { exp6 } // escaping $ in password
Not sure why expect put the curly brackets around the password passed to it. I verified that if there is no dollar sign in the password, there would not be the brackets.
Any help?
The shell code needs to quote variables:
./ssh.exp "$host" "$user" "$pass"
The expect code should not treat lists like plain strings. Extract the arguments with
lassign $argv host user pass
or if your expect is too old to have lassign, do
foreach {host user pass} $argv break
or (less DRY)
set host [lindex $argv 0]
set user [lindex $argv 1]
set pass [lindex $argv 2]
Im executing the remote script and checking return status of the script but if I do in the following way its returning the status of the password but not the status of the called script.How can I get the return status of the called script. Please help thanks in advance.
#!/usr/bin/expect
proc auto { } {
global argv
set timeout 120
set ip XXXX.XXX.XX.XX
set user name
set password pass
set ssh_opts {-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no}
set script /path-to-script/test.sh
spawn ssh {*}$ssh_opts $user#$ip bash $script {*}$argv
expect "Password:"
send "$password\r"
send "echo $?\r"
expect {
"0\r" { puts "Test passed."; }
timeout { puts "Test failed."; }
}
expect eof
}
auto {*}$argv
You're automating ssh bash remote_script, so you're not going to get a shell prompt where you can echo $? -- ssh will launch your script and then exit.
What you need to do is get the exit status of the spawned process (ssh is supposed to exit with the remote command's exit status). expect's wait command gets you the exit status (among other info)
spawn ssh {*}$ssh_opts $user#$ip bash $script {*}$argv
expect {
"Password:" { send "$password\r"; exp_continue }
timeout { puts "Test failed." }
eof
}
# ssh command is now finished
exp_close
set info [wait]
# [lindex $info 0] is the PID of the ssh process
# [lindex $info 1] is the spawn id
# [lindex $info 2] is the success/failure indicator
if {[lindex $info 2] == 0} {
puts "exit status = [lindex $info 3]"
} else {
puts "error code = [lindex $info 3]"
}
So im trying to write a script that connects automatically via ssh
I have three files.
First is text file (file.txt) with my login credentials (now its only one but later there will be a few):
user1 abcd
Second file (connect.sh) is bash file that reads credentials and passes them to expect file Looks like this:
#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
count=0;
words[0]="";
for word in $line; do
words[$count]="$word"
count=$(($count + 1))
done
myDir="$(dirname "$0")"
"$myDir/find.sh" "${words[1]}"
done < "$1"
The third file (find.sh) is /expect file which looks like this:
#!/usr/bin/expect
set password [lindex $argv 0]
spawn ssh "test#host.com"
expect "Password:"
send "$password\r"
interact
However when I try to login it fails to deliver the password. As far as I checked the password is sent correctly to the expect script. Also I tried a set timeout function but it does not work either.
As msw points out, you can do
#!/usr/bin/expect
proc main {passfile} {
set fh [open $passfile r]
while {[gets $fh line] != -1} {
lassign [split $line] user pass
connect $user $pass
}
close $fh
}
proc connect {user password} {
spawn ssh $user#host.com
expect "Password:"
send "$password\r"
interact
}
main [lindex $argv 0]
Then invoke your expect script with the password file
./test.exp file.txt
BTW, ".sh" is not a great extension to use for an expect program.
I have written a shell script for double authentication.
This is giving an error while execution in if statement.
#!/usr/bin/Expect
#!/bin/bash
set cpPass "App12345"
set server [lindex $argv 0]
set pass "App12345#123"
set app "app1"
set appPass "app#1013"
spawn ssh user1#$server.corp.clll.com
expect {
"(yes/no)?" {
send "yes\r"
expect "password:"
send "$cpPass\r"
}
"password:" {
send "$pass\r"
}
}
expect "bash"
send "su - $app\r"
expect "Password:"
send "$appPass\r"
if [ "$server" == "server1" ]
then
send "cd /ngs/app/genevad/QA/site/distribution/servers1\r";
else
send "sqlplus gqa_owner/App#789#dbgeneva01d.corp.clll.com:1700/app1D\r";
fi
interact
The error is like this on execution of command
./script.sh server1
invalid command name "server1"
while executing
""$server" == "novello" "
invoked from within
"if [ "$server" == "novello" ]"
(file "./script.sh" line 28)
I have tried various combinations in if like :
if [ $server == "novello" ]
if [ $server -eq "novello" ]
if [ "$server" -eq "novello" ]
etc but still not working. Any suggestions/Solutions to it?
Problem is here:
#!/usr/bin/Expect
#!/bin/bash
You cannot have 2 shebangs in same Unix script. Only first will be in effect and second will be ignored. Hence your script must be fully expect compliant script (since that is first one here - should be lowercase expect though).
Moment you try to use a bash syntax in it, you will get errors since that bash specific script will be interpreted by expect.
Replace the code [$server == "server1"] with {$server == "server1"}. As anubhava pointed out, u don't need 2 executeables name in the shebang. You can use only bash. Include all your expect code with -c flag. To know more about how to use expect with -c flag, have a look at https://stackoverflow.com/a/26607110/974155