SSH Example Tutorial for Expect Script

The Expect script is a valuable utility for Linux/Unix systems. In my daily life, whether it is at work or on my hosting server, I frequently interact with numerous Unix servers. Due to the large number of servers involved, it becomes challenging to remember all the SSH user credentials along with the SU user credentials. To simplify this situation, I decided to create a script that would automatically log me into the servers. Initially, I faced a roadblock when attempting to enter the SSH password because standard Unix shells do not provide a means to send the password when prompted for login. However, I discovered the expect script, which allows for automation of interactions with programs that require terminal input.

Anticipate Script

Expect Script is highly intuitive to pick up, living up to its name by analyzing the command’s output and executing the specified instruction whenever it detects a match with the designated regular expression.

An example of an SSH script using Expect.

I have developed a script named sshsudologin.expect that enables automatic SSH server login, authentication as a super user, and execution of a basic command.

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

Key considerations regarding Expect Script

    Please take note of the initial line which specifies that the expect script will serve as the interpreter. To avoid any timeout issues in case the login prompt takes longer to appear, I have adjusted the timeout from the default 10 seconds to 60 seconds. Also, observe the use of the expect command with the associated regular expression and the desired response to send. The inclusion of the initial Yes/No choice ensures that the script does not fail if the remote server key has not been imported yet. The expect regular expressions may differ depending on the servers – for my server, it ends with “#” but for others, it could be “$” – so please modify it accordingly. The important thing is to ensure that the regular expression matches the expect command, enabling it to send the appropriate command or password. Lastly, the final expect command demonstrates that we can send commands once logged into the server.

This is what happens when I execute the expect script above using the right parameters.

scdev@Pankajs-MacBook-Pro:~$/Users/scdev/scripts/sshloginsudo.expect 'scdev.com' 'scdev' 'ssh_pwd' 'su_user' 'su_pwd'
spawn ssh scdev@scdev.com
scdev@scdev.com's password: 
Last login: Sun Jun  9 19:54:17 2013 from c-67-161-57-160.hsd1.ca.comcast.net
scdev@journal [~]# su - su_user
Password: 
su_user@journal [~]# ls -ltr
total 708
...

Additional suggestions for enhancing your experience with the expect script.

    You can reuse the expect script by passing all the information as arguments. To save time typing all the parameters, it is recommended to create aliases for each one of them for quick login. For instance, you could create an alias like this: alias journal=”/Users/scdev/scripts/sshloginsudo.expect ‘scdev.com’ ‘scdev’ ‘ssh_pwd’ ‘su_user’ ‘su_pwd'”. When passing arguments, it’s advisable to use single quotes, especially for passwords that may contain special characters, as not quoting them can lead to unexpected results.

Please be aware that the script mentioned above has been tested on both Mac OS and Linux operating systems. For more information, you can refer to the SourceForge Page.

 

 

More tutorials

EasyMock void method to have its last call.(Opens in a new browser tab)

Server Configurations Frequently Used for Your Web Application(Opens in a new browser tab)

Basics of Graph Plotting – Comprehending the plot() Function in R(Opens in a new browser tab)

Using Telnet Commands in Linux/Unix(Opens in a new browser tab)

JSch library to execute shell commands on a Unix(Opens in a new browser tab)

Leave a Reply 0

Your email address will not be published. Required fields are marked *