惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
博客园 - 三生石上(FineUI控件)
D
DataBreaches.Net
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
GbyAI
GbyAI
P
Proofpoint News Feed
Microsoft Security Blog
Microsoft Security Blog
月光博客
月光博客
I
InfoQ
V
Visual Studio Blog
罗磊的独立博客
Engineering at Meta
Engineering at Meta
Vercel News
Vercel News
Jina AI
Jina AI
L
LangChain Blog
Apple Machine Learning Research
Apple Machine Learning Research
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
The Cloudflare Blog
小众软件
小众软件
雷峰网
雷峰网
V
V2EX
人人都是产品经理
人人都是产品经理
Stack Overflow Blog
Stack Overflow Blog

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Server Autologin with Expect
2018-10-08 · via jdhao's digital space

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

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#