#!/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:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- 10 Greatest Open Source Software Of 2009
- My 10 UNIX Command Line Mistakes
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
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.
- Download Script
- Email this to a friend
- Rss Feed
- Last Updated: 04/10/08
{ 1 trackback }
{ 29 comments… read them below or add one }
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.
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.
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.
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
what if you want to log into an device which can’t use expect, like a cisco IOS device?
i want to login to a normal using shell script anyone have idea for the steps to do
thanks in advance
i want to login to a user using shell script anyone have idea for the steps to do
forexample:
username=oracle
password=xyzzz
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.
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
~
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..
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
Pradeep
put “interact” at the end of expect script. Without “” ok
does anyone know how to pass a variable from bash to expect
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
My bad, figured it out that it returns by default.
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.
Thanks Dewes ! I was looking for that !
Create a file named “execute.sh” with below contents
Later make it executable:
Try this as below:
or
I have a question, who can I List displays system memory utilization (free) every 10 minutes.
thank u for ur help