Tutorials  /  Linux Basics

Automating SSH Logins with Expect Script: A Comprehensive Guide

Ccentron Redaktion · November 2024 ·3 min read ·Linux Basics, Tutorial

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.

VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

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:

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

Console
$ /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:

Code
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.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren