SSH login expect shell script to supply username and password
Posted in Security
#!/usr/bin/expect -f # Expect script to supply root/admin password for remote ssh server # and execute command. # This script needs three argument to(s) connect to remote server: # password = Password of remote UNIX server, for root user. # ipaddr = IP Addreess of remote UNIX server, no hostname # scriptname = Path to remote script which will execute on remote server # For example: # ./sshlogin.exp password 192.168.1.11 who # ------------------------------------------------------------------------ # Copyright (c) 2004 nixCraft project <http://cyberciti.biz/fb/> # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ---------------------------------------------------------------------- # set Variables set password [lrange $argv 0 0] set ipaddr [lrange $argv 1 1] set scriptname [lrange $argv 2 2] set arg1 [lrange $argv 3 3] set timeout -1 # now connect to remote UNIX box (ipaddr) with given script to execute spawn ssh root@$ipaddr $scriptname $arg1 match_max 100000 # Look for passwod prompt expect "*?assword:*" # Send password aka $password send -- "$password\r" # send blank line (\r) to make sure we get back to gui send -- "\r" expect eof
Download - Email this to a friend - Printable version
Related Other Helpful Shell Scripts:
- Change password shell script
- Remote Server Rsync Backup Replication Shell Script
- Shell Script To Dump All MySQL Databases Every Hour To NAS Storage
- Shell Script to Start and Login via Netgear WG311 Marvell 88w8335 Wireless Card
- Shell script to remove whole ftp directory structure
Discussion on This Shell Script:
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: /usr/bin/expect, addreess, expect command, hostname, remote script, remote server, root admin, script collection, server password, set variables, shell script, ssh server, unix, unix box, unix server ~ Last updated on: April 10, 2008


Hello,
What happens if the server asks for a ‘RSA key fingerprint’?
In that the value to send is “yes”, right? Sending password will come only after this.
We can write a script to first expect “(yes/no)? ” and then expect “password:”
This will work only at the first time. Second time it expects “(yes/no)? ” and the script won’t get it.
Can you make it a single script to include both using an IF loop or something?
Regards,
Sarat
Why doesn’t this work for host names? Can the script be modified to support host names rather than just IP addresses?
Hi,
I can’t run this script.
Is anybody there to help me to show how to run it.
Thanks in advance.
Padmanabh
Sarat,
I’m having the same problem!
Try this:
send -- "ssh Manager@$ipaddr\r" expect "(yes/no)" { send "yes\r" } \ "Manager@$ipaddr's password:" { send "$pword\r" } expect "Manager@ipaddr's password:" { send "$pword\r" } \ "#*" { send "" }The weird box at the end is the same as “Cntl Y”
This WILL work after you have accepted this host. My problem is getting this to work 100% of the time, regardless.
I need a bash script which can login to an other machine via SSH and then run some commands and then return the result to my machine. There is a need for the sudo passwd on the remote machine.
[...] al sito, sezione apposita (Bash Shell Scripting Directory For Linux / UNIX) nonchè al post specifico da cui ho scopiazz… “tratto ispirazione”. [...]
I am not able to run this sript it says error is below
[root@rahil sam]# ./rssh
bash: ./rssh: /usr/bin/expect: bad interpreter: No such file or directory
Rahil,
You need to install expect tool.
Hi
I need to check uptime in multiple hosts
i have tried to write a script. Still it not working and stops at Password:
#!/bin/bash
#!/usr/bin/expect -f
set password “123″
SERVERS=”abc688 rgmgw1 abc173 abc30 abc101″
for host in $SERVERS
do
echo $host ; ssh -o StrictHostKeyChecking=no $host uptime
done
{
expect “Password:*”
send — “$password\r”
send — “\r”
expect eof
}
Any one any adea…
Madhusudan
here is the final code,
you can set the SSH port if it’s other than 22
——————————–
#!/usr/bin/expect -f # This script needs three argument to(s) connect to remote server: # password = Password of remote UNIX server, for root user. # ipaddr = IP Addreess of remote UNIX server, no hostname # scriptname = Path to remote script which will execute on remote server # For example: # ./sshlogin.exp password 192.168.1.11 who # set Variables set password [lrange $argv 0 0] set ipaddr [lrange $argv 1 1] set scriptname [lrange $argv 2 2] set arg1 [lrange $argv 3 3] set timeout -1 # now connect to remote UNIX box (ipaddr) with given script to execute spawn ssh -p 22 root@$ipaddr $scriptname $arg1 match_max 100000 expect { -re ".*Are.*.*yes.*no.*" { send "yes\r" exp_continue #look for the password prompt } "password:" { send -- "$password\r" #he expect command will now return } } sleep 2————————–
for the yes/no question, this ssh option does the trick: -o StrictHostKeyChecking=no
so sshpass + -o StrictHostKeyChecking=no option and no need for expect and this script.
Drink thanks a million for the addition of ssh -o StrictHostKeyChecking=no that saved me.