SSH login expect shell script to supply username and password

#!/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

Featured Articles:

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

{ 29 comments… read them below or add one }

1 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

2 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

3 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

4 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

5 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

6 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

7 vivek August 9, 2008 at 9:50 am

Rahil,

You need to install expect tool.

Reply

8 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

9 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

10 Drink September 9, 2008 at 12:41 pm

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

Reply

11 Drink September 9, 2008 at 12:43 pm

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

Reply

12 SeeFor September 10, 2008 at 5:05 pm

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

Reply

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 Nilantha July 22, 2009 at 12:45 pm

Pradeep
put “interact” at the end of expect script. Without “” ok

Reply

23 Nilantha July 22, 2009 at 12:46 pm

does anyone know how to pass a variable from bash to expect

Reply

24 Alexandru September 16, 2009 at 8:23 am

Does anybody know how to modify this script so that it can run a script on a remote host so that it returns a message from the script? many thanks

Reply

25 Alexandru September 16, 2009 at 8:30 am

My bad, figured it out that it returns by default.

Reply

26 Dewes October 19, 2009 at 1:29 pm

Changing set scriptname [lrange $argv 2 2] to set scriptname [lindex $argv 2] you should be able to run ssh user@host ‘ls -a’ and others commands within ‘ ‘ with no need to another variable.

Reply

27 Pradeep November 8, 2009 at 1:11 am

Thanks Dewes ! I was looking for that !

Reply

28 Sidh November 13, 2009 at 7:28 pm

Create a file named “execute.sh” with below contents

#!/bin/bash

HOST="remote-hostname"
USER="remote-user"
PASS="remore-user-password"
CMD=$@

VAR=$(expect -c "
spawn ssh -o StrictHostKeyChecking=no $USER@$HOST $CMD
match_max 100000
expect \"*?assword:*\"
send -- \"$PASS\r\"
send -- \"\r\"
expect eof
")
echo "==============="
echo "$VAR"

Later make it executable:

chmod +x execute.sh  

Try this as below:

./execute.sh "ls -l"

or

./execute.sh 'ls -l'

Reply

29 hozifa January 23, 2010 at 2:08 pm

I have a question, who can I List displays system memory utilization (free) every 10 minutes.

thank u for ur help

Reply

Previous post:

Next post: