Expect script is a great linux/unix utility. I have to deal with a lot of unix servers in my daily life, whether it’s at work or it’s my hosting server. So I need to remember a lot of SSH users, their passwords and then SU users and passwords. It’s kind of messy when the number of servers are huge. So I thought of writing a script that will automatically login me to the server. When I first started working on this, I got stuck as how to enter the SSH password because normal unix shells don’t have any way to send the password when it gets prompted for login. Then I come to know about expect script
that allows to automate the interaction with programs that opens a terminal for input.
Expect Script is very easy to learn and as the name suggests it works by parsing the output of the command and when it matches the specified regular expression, it processes the specified instruction.
Here is the script I created for automatically login to the SSH server and then login with super user and then run a simple command. sshsudologin.expect
#!/usr/bin/expect
#Usage sshsudologin.expect <host> <ssh user> <ssh password> <su user> <su password>
set timeout 60
spawn ssh [lindex $argv 1]@[lindex $argv 0]
expect "yes/no" {
send "yes\r"
expect "*?assword" { send "[lindex $argv 2]\r" }
} "*?assword" { send "[lindex $argv 2]\r" }
expect "# " { send "su - [lindex $argv 3]\r" }
expect ": " { send "[lindex $argv 4]\r" }
expect "# " { send "ls -ltr\r" }
interact
expect
command followed by the regular expression and then what should be send as a response. The first Yes/No choice is added to make sure that it doesn’t fail if remote server key is not already imported.Here is the output when I run the above expect script with correct parameters.
pankaj@Pankajs-MacBook-Pro:~$/Users/pankaj/scripts/sshloginsudo.expect 'journaldev.com' 'pankaj' 'ssh_pwd' 'su_user' 'su_pwd'
spawn ssh pankaj@journaldev.com
pankaj@journaldev.com's password:
Last login: Sun Jun 9 19:54:17 2013 from c-67-161-57-160.hsd1.ca.comcast.net
pankaj@journal [~]# su - su_user
Password:
su_user@journal [~]# ls -ltr
total 708
...
alias journal="/Users/pankaj/scripts/sshloginsudo.expect 'journaldev.com' 'pankaj' 'ssh_pwd' 'su_user' 'su_pwd'"
NOTE: The above script is tested on Mac OS and Linux systems. Reference: SourceForge Page
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
take a look at https://github.com/clarkwang/sexpect . you can write expect scripts with shells only.
- clarkw
I want an expect script to connect to a website, fill out a form and get back the response of server. Is it possible?
- Pramod
27.000000\t\tC1[175] i want to mach this expression , this is the value i get while printing it in command line , i am usng expect “27.000000\t\tC1\[175]”; but it not matching and giving timed out as output . its inerpretating the words correclty as i am able to see by issuing exp_internal 1 command… Any solutions
- shyam
Hi Pankaj, my requirement is file transfer from Windows server(Cygwin exe is installed for SFTP) to Unix Server and vice versa. I need java code for this. do you have any idea about that ?
- Adhiran
Hi Pankaj, I was trying almost the same example. I am trying to execute a script in server B from server A. My problem is the script is not exiting from SSH session.How can I get control back to script in server A and print some message saying the script is done. #Shell script in Server A #!/usr/bin/expect -f set timeout -1 set Username “” set password “” set ipaddress “” set script “” spawn $env(SHELL) send “ssh $Username@$ipaddress $script\r && exit” expect { “(yes/no)?” {send “yes\r”;exp_continue} “password:” {send “$password\r”;interact;} }
- Riya
How can I ssh to one more session through an existing ssh session?
- Vamshi
Awesome :) now I can have up to six consoles open on a remote machine and monitor its logs with a single key combination Ctrl-Alt-m, want to learn how I did ? https://ychaouche.informatick.net/kde
- yassine
Hi there, I have a problem when trying to ssh to a solaris box using groovy script and expect To give you some background, I can ssh to the solaris box with no problem from command line using putty, I can also execute my groovy and expect script from Eclipse and I am able to connect. However when I try to do the same from a web server that is deployed on weblogic I get the following error: Error: java.lang. IllegalArgumentException: No Configuration was registered that can handle the configuration named com.sun.security.jgss.krb5.initiate This happens when I use def shell = ex.spawn(‘solarisBox’, 22, user,password) ex is an instantiated object of ExpectJ… a Java wrapper for expect if I try to use def shell = ex.spawn(“ssh user@solarisBox”) in my script shell.expect(prompt) shell.send(pass) I get Java.IOException broken pipe using groovy 1.5.5 and I believe the expect version is 2.0.7 Please help, thank you so much in advance.
- Samir
Could you please share the packages for send and intreact commands, as while running command getting command not found error. The operating system am using was RHEL 6.5 .
- MOHAN
Hi Pankaj I have gone through this web page. It was very helpful to learn about expect scripts. I need one help from you. I wrote same script which you have mentioned in this page to login to remote server. After login I am going to particular path and call shell script inside expect script. Its is executing properly in terminal( I am using Linux server). When I am calling that script on jenkins. Its not excuting properly. Please help me out its very urgent. If you need some information please let me know. Thanks pawar
- Gowtham