SSH login expect shell script to supply username and password

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
Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our email newsletter to make sure you don't miss a single tip/tricks.

{ 1 trackback }

Automatizzare i trasferimenti di file via SSH… « JP’s Web Place
July 25, 2008 at 7:33 pm

{ 21 comments… read them below or add one }

Sarat June 3, 2008 at 7:19 pm

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

Reply

Z June 4, 2008 at 1:44 pm

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

Reply

Padmanabh June 5, 2008 at 12:18 pm

Hi,

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

Thanks in advance.

Padmanabh

Reply

Mark June 16, 2008 at 7:47 pm

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.

Reply

Viven Rajendra June 18, 2008 at 9:36 am

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.

Reply

rahil August 7, 2008 at 4:43 pm

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

Reply

vivek August 9, 2008 at 9:50 am

Rahil,

You need to install expect tool.

Reply

Madhusudan August 11, 2008 at 4:04 am

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

Reply

Mohammed August 29, 2008 at 2:12 pm

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

————————–

Reply

Drink September 9, 2008 at 12:41 pm

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

Reply

Drink September 9, 2008 at 12:43 pm

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

Reply

SeeFor September 10, 2008 at 5:05 pm

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

Reply

ram November 17, 2008 at 9:07 am

while in an interactive program work as first it will ask for user_name if it is valid it will promt for passqd then ecucution will progress. in case we r sending wrong user_name it will prompt again for user_name. how we are going to solve this condition with help of expect. how we can determine that previous input was not currect so program is asking for the same input again…

please help me
thanks.

Reply

siddu March 5, 2009 at 6:43 am

i’m getting partial output of the command and more over if the command is more than a word,which is give in double quotes, is taken as { cmd }. which error out saying { cmd } not found.

Please help

Reply

frank May 5, 2009 at 11:35 pm

what if you want to log into an device which can’t use expect, like a cisco IOS device?

Reply

syed May 18, 2009 at 3:38 pm

i want to login to a normal using shell script anyone have idea for the steps to do

thanks in advance

Reply

ahmed May 18, 2009 at 3:43 pm

i want to login to a user using shell script anyone have idea for the steps to do
forexample:
username=oracle
password=xyzzz

Reply

cragman513 June 1, 2009 at 4:37 pm

How do you pass a variable from a shell script to an expect script? It seems that I can either call the expect script properly with no variable or I can pass the variable to the expect script, but the expect script will not run.

Reply

RobsterLPL June 11, 2009 at 1:01 am

Something strange is happening. So I’m able to a root prompt BUT when I try to type anything in it just hangs then dumps me back to my original host I SSH’ed from.
Can anyone help?

#!/usr/bin/expect -f
set timeout -1
spawn ssh root@172.17.27.70
match_max 100000
expect “*?assword:*”
send — “password\r”
send — “\r”
sleep 2
expect eof
~

Reply

pradeep June 22, 2009 at 12:20 pm

I ran the script. it worked for me. But after logging in when i type the command it logging out immediately. Tell me a solution..

Reply

Santosh June 24, 2009 at 5:39 am

I want to telnet to a remote m/c and enable events on that m/c.I used expect script to telnet and enable the events, but i dont see any events coming on the screen.Pls help.

#!/usr/local/bin/expect –

set host1 “10.1.2.3″
set login “user”
set passwd “passwd”
spawn telnet $host1
expect “login:”
send “$login\r”
expect “Password:”
send “$passwd\r”
expect “prompt>”
expect “TeMIP>”
send “enable events\r”
expect “*” ### wht shud I give here to see all events ??
sleep 3600

Reply

Leave a Comment

Previous post: Shell script for search for no password entries and lock all accounts

Next post: Change password shell script