#!/usr/bin/expect set machine [lindex $argv 0] set kdauth [ exec oathtool --totp -b <这里写你的Google秘钥>] #solve the window size bug trap { set rows [stty rows] set cols [stty columns] stty rows $rows columns $cols < $spawn_out(slave,name) } WINCH spawn ssh <这里写你的登录账号>@<这里写你的跳板机IP> expect { "Verification*" { send "$kdauth\r"; exp_continue } "Pass*" { send "<这里写你的登录密码>\r"; exp_continue } "*choice*" { send "$machine\r"; interact } }
1. 复制以上代码到 .exp 格式文件,修改对应的配置,需要安装一下 oath-toolkit
brew install oath-toolkit* 如果你的 Mac 还没有安装 Homebrew,参考文档:https://brew.sh/index_zh-cn.html
完成以上步骤后,执行
expect <你的脚本名称>.exp <这里是一个参数,登录跳板机后执行的第一条命令,可以为空>
为提高效率可以配置一下简易命令
echo "alias serv='expect <你的脚本名称>.exp <这里是一个参数,登录跳板机后执行的第一条命令,可以为空>'" >> ~/.bash_profile && source ~/.bash_profile
之后每次登录时使用简易命令就可以了
serv