基于 shell 使用 expect 实现 SSH 自动登录远程服务器

1. 效果预览

2. 相关知识点

2.1. expect

expect 是一个免费的编程工具,用来实现自动的交互式任务,而无需人为干预。

说白了,expect 就是一套用来实现自动交互功能的软件。

在实际工作中,我们运行命令、脚本或程序时,这些命令、脚本或程序都需要从终端输入某些继续运行的指令,而这些输入都需要人为的手工进行。而利用expect,则可以根据程序的提示,模拟标准输入提供给程序,从而实现自动化交互执行。

这就是 expect!!!

2.2. IFS

IFS 是存储定界符的环境变量,是Shell环境中的默认定界符字符串,默认值为空白字符(换行符、制表符、空格)

3. 主要文件

源码地址:https://github.com/whorusq/ssh-autologin

  • goto.sh 入口脚本
  • goto.ex expect 执行脚本
  • goto.conf 服务器列表参数配置文件

4. 如何使用

  1. git clone https://github.com/whorusq/shell-ssh-autologin.git

  2. cd shell-ssh-autologin

  3. 修改 goto.conf ,追加服务器列表

  4. 赋予脚本可执行权限 sudo chmod u+x goto.sh goto.ex

  5. 使用

    • 方式一:./goto.sh

    • 方式二:将 goto 加入当前用户全局使用

      ➜  ~ echo "alias goto=\"$PWD/goto.sh\"" >> ~/.zshrc
      ➜ ~ source ~/.zshrc
      ➜ ~ goto

5. 常见问题

5.1. 提示没有 expect 或 spawn 命令

当前脚本主要基于 expect 使用 spawn 实现。

Expect 是一个用来处理交互的工具,通常用于需要手动输入数据的场景,可在脚本中使用 Expect 来实现自动化。

首先使用以下命令检查 expect 是否已安装:

➜  whereis expect
/usr/bin/expect

如果没有安装,请按照以下步骤:

  1. 安装 expect 的依赖 tcl

    ➜  wget https://sourceforge.net/projects/tcl/files/Tcl/8.4.19/tcl8.4.19-src.tar.gz
    ➜ tar zxvf tcl8.4.19-src.tar.gz
    cd tcl8.4.19/unix && ./configure
    ➜ make
    ➜ make install
  2. 安装 expect

    ➜  wget http://sourceforge.net/projects/expect/files/Expect/5.45/expect5.45.tar.gz
    ➜ tar zxvf expect5.45.tar.gz
    cd expect5.45
    ➜ ./configure --with-tcl=/usr/local/lib --with-tclinclude=../tcl8.4.19/generic
    ➜ make
    ➜ make install
    ➜ ln -s /usr/local/bin/expect /usr/bin/expect

5.2. 特殊字符转义

如果密码中有特殊字符,需要做转义处理,否则使用 expect 的 send 语法是无法发送成功的,具体需要转义的字符如下:

\ ===> \\\
} ===> \}
[ ===> \[
$ ===> \\\$
` ===> \`
" ===> \\\"
~ ===> \\~