Skip to main content
  1. Posts/

Server Autologin with Expect

··404 words·2 mins·
Note Expect Cygwin Ssh Windows
Table of Contents

I use a Windows 10 machine and need to connect to Linux servers. After using several ssh clients for connecting to the server, I finally settled for Cygwin and Wsltty, which is open-sourced and use the well-known mintty terminal.

But one feature I miss from the SSH tools such as MobaXterm or Xshell is that these tools can save the server IP, port, user name and even password, so that you can log into the server with just a click.

With mintty terminal, I have to manually ssh to my server each time, which is tedious and time consuming. Fortunately, I have found the Expect scripting language which is pretty powerful for this task.

Install
#

If you are using Cygwin, you can install Expect package via its setup program. If you are using Ubuntu on Windows, you can install expect with apt-get:

sudo apt-get install -y expect

Usage
#

Simple case
#

Expect can execute command, receive input and send output automatically for you. Suppose you want to connect to a server, you can use the following script:

#!/usr/bin/expect

set timeout 10
spawn ssh -p 22 USER@SERVER_IP
expect "Password: "
send "PASSWORD\r"

You should change USER, SERVER_IP and PASSWORD properly. In the above script, we first use spawn to execute the ssh command for us. Then we use expect to capture the output of the terminal to see if it matches the predefined string. Finally, we use send command to send the right password and a newline character: it is just like you have typed your password and press Enter manually in terminal.

You can save this script as auto_login.exp and execute it with

expect auto_login.exp

Accept command line parameters
#

In the above script, the user name and password is fixed in the script. If you have multiple user account in the server, it would be better that you can input the user name and password dynamically from the command line. You can use the following script for that:

#!/usr/bin/expect

set timeout 10
set user [lindex $argv 0]
set password [lindex $argv 1]

spawn ssh -p 22 $user@SERVER_IP
expect "Password: "
send "$password\r"

In the above script, lindex command is used to get the parameters supplied in the command line. To use a variable, add a $ before it to access its value. In the command line, you can invoke the script like this:

expect auto_login.exp user_name some_password

References
#

Related

Execellent Development Experience with Cygwin and Mintty Terminal on Windows 10
··556 words·3 mins
Tools Windows Ssh Mintty Cygwin
Display Image with Pillow inside Ubuntu on Windows
··268 words·2 mins
Note PIL Windows Python X11 WSL Ubuntu
Deoplete Failed to Load at Startup after Updating Python neovim Package
··473 words·3 mins
Note Nvim Windows CentOS