How to write expect snippet in bash script to run remote command? - bash

Currently I am running this script to print directories on a remote box but I am not sure this code is working.
#!/bin/bash
PWD="test123"
ip="10.9.8.38"
user=$1
/usr/bin/expect <<EOD
spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no $user#$ip df -h
expect "*assword:"
send "$PWD\n"
interact
EOD

expect spawns a new sub-shell, hence your local bash variables lose their scope, one way to achieve this is to export your variables to make it available for the sub-shell. Use the Tcl env built-in to import such variables in your script.
#!/bin/bash
export pwdir="test123"
export ip="10.9.8.38"
export user=$1
/usr/bin/expect <<EOD
spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no "$env(user)"#"$env(ip)" df -h
expect "*assword:"
send "$env(pwdir)\n"
interact
EOD
(Or) If you are not interested in using an expect script directly with a #!/usr/bin/expect she-bang, you can do something like
#!/usr/bin/expect
set pwdir [lindex $argv 0];
set ip [lindex $argv 1];
set user [lindex $argv 2];
spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no $user#$ip df -h
expect "*assword:"
send "$pwdir\n"
interact
and run the script as
./script.exp "test123" "10.9.8.38" "johndoe"

Related

Bash script with expect that executes commands locally and in sftp [duplicate]

I'm trying to use expect in a Bash script to provide the SSH password. Providing the password works, but I don't end up in the SSH session as I should. It goes back strait to Bash.
My script:
#!/bin/bash
read -s PWD
/usr/bin/expect <<EOD
spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr#$myhost.example.com'
expect "password"
send "$PWD\n"
EOD
echo "you're out"
The output of my script:
spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr#$myhost.example.com
usr#$myhost.example.com's password: you're out
I would like to have my SSH session and, only when I exit it, to go back to my Bash script.
The reason why I am using Bash before expect is because I have to use a menu. I can choose which unit/device to connect to.
To those who want to reply that I should use SSH keys, please abstain.
Mixing Bash and Expect is not a good way to achieve the desired effect. I'd try to use only Expect:
#!/usr/bin/expect
eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr#$myhost.example.com
# Use the correct prompt
set prompt ":|#|\\\$"
interact -o -nobuffer -re $prompt return
send "my_password\r"
interact -o -nobuffer -re $prompt return
send "my_command1\r"
interact -o -nobuffer -re $prompt return
send "my_command2\r"
interact
Sample solution for bash could be:
#!/bin/bash
/usr/bin/expect -c 'expect "\n" { eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr#$myhost.example.com; interact }'
This will wait for Enter and then return to (for a moment) the interactive session.
The easiest way is to use sshpass. This is available in Ubuntu/Debian repositories and you don't have to deal with integrating expect with Bash.
An example:
sshpass -p<password> ssh <arguments>
sshpass -ptest1324 ssh user#192.168.1.200 ls -l /tmp
The above command can be easily integrated with a Bash script.
Note: Please read the Security Considerations section in man sshpass for a full understanding of the security implications.
Add the 'interact' Expect command just before your EOD:
#!/bin/bash
read -s PWD
/usr/bin/expect <<EOD
spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr#$myhost.example.com
expect "password"
send -- "$PWD\r"
interact
EOD
echo "you're out"
This should let you interact with the remote machine until you log out. Then you'll be back in Bash.
After looking for an answer for the question for months, I finally find a really best solution: writing a simple script.
#!/usr/bin/expect
set timeout 20
set cmd [lrange $argv 1 end]
set password [lindex $argv 0]
eval spawn $cmd
expect "assword:" # matches both 'Password' and 'password'
send -- "$password\r"; # -- for passwords starting with -, see https://stackoverflow.com/a/21280372/4575793
interact
Put it to /usr/bin/exp, then you can use:
exp <password> ssh <anything>
exp <password> scp <anysrc> <anydst>
Done!
A simple Expect script:
File Remotelogin.exp
#!/usr/bin/expect
set user [lindex $argv 1]
set ip [lindex $argv 0]
set password [lindex $argv 2]
spawn ssh $user#$ip
expect "password"
send "$password\r"
interact
Example:
./Remotelogin.exp <ip> <user name> <password>
Also make sure to use
send -- "$PWD\r"
instead, as passwords starting with a dash (-) will fail otherwise.
The above won't interpret a string starting with a dash as an option to the send command.
Use the helper tool fd0ssh (from hxtools, source for ubuntu, source for openSUSE, not pmt). It works without having to expect a particular prompt from the ssh program.
It is also "much safer than passing the password on the command line as sshpass does" ( - comment by Charles Duffy).
Another way that I found useful to use a small Expect script from a Bash script is as follows.
...
Bash script start
Bash commands
...
expect - <<EOF
spawn your-command-here
expect "some-pattern"
send "some-command"
...
...
EOF
...
More Bash commands
...
This works because ...If the string "-" is supplied as a filename, standard input is read instead...
sshpass is broken if you try to use it inside a Sublime Text build target, inside a Makefile. Instead of sshpass, you can use passh
With sshpass you would do:
sshpass -p pa$$word ssh user#host
With passh you would do:
passh -p pa$$word ssh user#host
Note: Do not forget to use -o StrictHostKeyChecking=no. Otherwise, the connection will hang on the first time you use it. For example:
passh -p pa$$word ssh -o StrictHostKeyChecking=no user#host
References:
Send command for password doesn't work using Expect script in SSH connection
How can I disable strict host key checking in ssh?
How to disable SSH host key checking
scp without known_hosts check
pam_mount and sshfs with password authentication

multiple ssh to cisco routers using expect script

I am not a hardcore script guy,but trying to learn.I just started with expect scripts to automate tasks on cisco router.Please be gentle and nudge me in the right direction.I will do research accordingly thereafter.
Requirement: to spawn 2 ssh sessions to 2 different cisco routers and run unique commands on each of them in a single expect script.
Current Method : I call this expect script using a regular bash script.I can achieve the requirement using two expect scripts,But I want to do this using one expect script.
Example:
# Set variables
set router1 [lindex $argv 0]
set router2 [lindex $argv 1]
set username [lindex $argv 2]
set password [lindex $argv 3]
spawn ssh -o StrictHostKeyChecking=no $username\#$router1
expect "*assword"
send "$enablepassword\n"
expect "#"
send "command on router1"
expect "#"
close
#i want to close this ssh session and spawn ssh process to router2
spawn ssh -o StrictHostKeyChecking=no $username\#$router2
#i tried this simply in the same script and it doesn't work,mostly because #it is not correct
expect "*assword"
send "$enablepassword\n"
expect "#"
send "command on router2"
expect "#"
I think you should use spawn_id global variable, it helps to interact with multiple ssh or telnet sessions.
Your code should look something like this:
spawn ssh -o StrictHostKeyChecking=no $username\#$router1
set R1 $spawn_id
expect -i $R1 "*assword"
send -i $R1 "$enablepassword\n"
expect -i $R1 "#"
send -i $R1 "command on router1"
expect -i $R1 "#"
send -i $R11 "exit\r"
spawn ssh -o StrictHostKeyChecking=no $username\#$router2
set R2 $spawn_id
expect -i $R2 "*assword"
send -i $R2 "$enablepassword\n"
expect -i $R2 "#"
send -i $R2 "command on router2"
expect "#"

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

calling expect script inside a while loop of shell script

I am trying to automate password-less ssh setup from master account to other slave accounts.I have a script named AddSSH.ksh which does this setup.When this script is run manually,it asks for same password same times,it basically copied keys using scp. All the slave accounts are saved in a file env.txt.So Now, I have a shell script(run.ksh) which reads the accounts from this file(env.txt) one by one and then uses expect script auto_ssh.ksh to handle the interaction and it enters the password accordingly.
env.txt
account1#machine1
account2#machine2
account3#machine3
account4#machine4
run.ksh:
#!/usr/bin/ksh
while read env
do
username=`echo $env | cut -d"#" -f1`;
hostname=`echo $env | cut -d"#" -f2`;
password='Unix_11'
ssh -n -o PasswordAuthentication=no ${env} ' ' 2>/dev/null
if [ $? -eq 0 ]; then
printf "\nConnection OK for : $env \n"
else
expect auto_ssh.ksh $username $hostname $password
fi
done<env.txt
auto_ssh.ksh:
#!expect
set timeout 6
set user [lindex $argv 0]
set machine [lindex $argv 1]
set password [lindex $argv 2]
spawn AddSSH.ksh $user $machine
expect "password:"
send "$password\r";
expect "password:"
send "$password\r";
interact
If a run the script auto_ssh.ksh like
./auto_ssh.ksh account1 machine1 password
It runs fine but when I call it inside shell script,this expect script exits at the second password.when I ran the shell script in debug mode, I see that instead of sending the password the second time it moves to reading the next env from env.txt and exits.
This is the line of the output in debug mode where it fails.
account1#machine1's password: + read env
Add exp_internal 1 to the expect script for additional debugging. I suspect you might need to refine what you expect: expect -re {password:\s*$}
If you don't need to actually interact with addSSH.ksh, change interact to expect eof
Why does your expect script have a ".ksh" extension?

running expect scripts that are in gist.github.com

I know I can run bash scripts placed inside gist.github.com using the following:
bash -c "$(curl -fsSL https://raw.github.com/gist/5126919)" <arg0> <arg1>
How do I do the same for expect scripts?
I have a sample here:
https://gist.github.com/simkimsia/5126919
#!/usr/bin/expect -f
set CHANGESHELL_FOR_USER [lindex $argv 0];
set PASSWORD_OF_USER [lindex $argv 1];
########################################
## switch to another user
## read this http://stackoverflow.com/a/1988255/80353
########################################
sudo -u $CHANGESHELL_FOR_USER -H sh -c "chsh -s /bin/bash"
expect "*?assword:*" {send -- "$PASSWORD_OF_USER\r";}
expect eof
My purpose is to chsh for a particular user.
Bash and ksh support process substitution so you can try like this:
expect <(curl ...) arg1 arg2 ...
The only problem I can think of is that the value of $argv0 is not what we usually expect. You may consider hard coding it in the script.

Resources