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

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.

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