公司服务器使用了两层跳板机,外面的一台我们管它叫 server A
, 另外一台 叫它 server B
。
虽然我不知道这种双保险给公司带来了多少安全感,但是我知道我的运维效率降低了差不多90%吧 :>。
server A
被直接暴露在公网上, 我们不能使用 ssh key
只能使用 password
认证ssh
。
这还不算完,server A
每3小时改一次自己的root
密码。
后面的server B
跳板机器的自我安全感就强多了,server B
可以直接免密使用ssh key
登陆所有的内网服务器,而且允许server A
免密登陆到自己。
我决得这样两次登录很浪费时间,于是写了个脚本从外网一次性登陆到server B
服务器上。
expect 脚本
|
|
基本语法
简单说下基本流程如下:
- set: 设置变量;
- spawn: 给对象一个进程空间,让对象可以运行起来
- expect: 模拟用户等待,期待对象输出字符串;
- send: 模拟用户输入,给对象发送字符串;
- send_user: 提示用户;
- interact: 用户直接和对象通信,expect不再在中间传话了。
因为expect
语法继承自Tcl
,所以变量赋值/初始化的方式和 Bash
不同,需要使用set
关键字。
注意第5点send_user
命令是为了更好的用户交互,主要是给用户一下提示信息/反馈信息。
最后注意:send "$pwd\r"
语句的转以符是 \r
而不是\r\n
。
详细语法
expect
详细的语法和参数解释参考Expect examples and tips
Expect is an automation and testing tool used to automate a process that receives interactive commands. If you can connect to any machine using ssh, telnet, ftp, etc then you can automate the process with an expect script. This works even with local programs where you would have to interact with a script or program on the command line.