About

Script Categories

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:

Discussion on This Shell Script:

  1. Sarat Says:

    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

  2. Z Says:

    Why doesn’t this work for host names? Can the script be modified to support host names rather than just IP addresses?

  3. Padmanabh Says:

    Hi,

    I can’t run this script.
    Is anybody there to help me to show how to run it.

    Thanks in advance.

    Padmanabh

  4. Mark Says:

    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.

  5. Viven Rajendra Says:

    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.

  6. Automatizzare i trasferimenti di file via SSH… « JP’s Web Place Says:

    [...] al sito, sezione apposita (Bash Shell Scripting Directory For Linux / UNIX) nonchè al post specifico da cui ho scopiazz… “tratto ispirazione”. [...]

  7. rahil Says:

    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

  8. vivek Says:

    Rahil,

    You need to install expect tool.

  9. Madhusudan Says:

    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

  10. Mohammed Says:

    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

    ————————–

  11. Drink Says:

    for the yes/no question, this ssh option does the trick: -o StrictHostKeyChecking=no

  12. Drink Says:

    so sshpass + -o StrictHostKeyChecking=no option and no need for expect and this script.

  13. SeeFor Says:

    Drink thanks a million for the addition of ssh -o StrictHostKeyChecking=no that saved me.

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!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tags: , , , , , , , , , , , , , , ~ Last updated on: April 10, 2008