Automating SSH Logins with Expect Script: A Comprehensive Guide

Managing multiple Linux/Unix servers often involves juggling various SSH credentials, including user accounts and superuser (SU) passwords. Remembering and entering these credentials manually can quickly become a tedious task, especially as the number of servers grows. Fortunately, the Expect scripting utility offers a streamlined solution to automate this process. This guide explains how you can leverage Expect scripts to log in to SSH servers, switch to superuser mode, and execute commands seamlessly.

What is Expect Script?

Expect is a powerful tool that automates interactions with terminal-based applications. It works by parsing the terminal output, matching specified patterns (regular expressions), and responding with predefined inputs. As the name suggests, it “expects” certain outputs and takes action accordingly.

Sample Expect Script

Below is a sample Expect script to log in to an SSH server, switch to a superuser account, and run a command:

  
#!/usr/bin/expect

# Usage: sshsudologin.expect     

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  

Understanding the Script

The script begins with the #!/usr/bin/expect shebang, which indicates the use of the Expect interpreter. By default, Expect scripts timeout after 10 seconds. Setting the timeout to 60 seconds ensures smooth operation, even if server prompts are delayed. The expect command matches terminal outputs like “yes/no” prompts or password requests. The script adjusts dynamically for such scenarios.

Server prompts may vary (“#”, “$”, etc.), so ensure the script’s regular expressions match your server configuration. Expect scripts can also send commands post-login. Here, it executes ls -ltr after gaining superuser access.

Example Usage

Here’s an example of running the script with appropriate arguments:

  
$ /path/to/sshloginsudo.expect 'yourserver.com' 'ssh_user' 'ssh_password' 'su_user' 'su_password'  

The script automates the SSH login, switching to the specified SU user, and running the ls -ltr command to list directory contents.

Tips for Efficient Usage

To simplify repetitive logins, you can create aliases for specific commands. For example:

  
alias myserver="/path/to/sshloginsudo.expect 'yourserver.com' 'ssh_user' 'ssh_password' 'su_user' 'su_password'"  

It is always advisable to use single quotes to pass arguments, especially passwords, as they may contain special characters that can cause unexpected behavior.

Conclusion

Expect scripts are a simple yet effective tool for automating SSH logins and other terminal-based tasks. By integrating regular expressions and automated responses, these scripts can save you valuable time and effort. Whether managing one server or many, Expect helps ensure a seamless workflow.

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: