iterm2自动输入堡垒机TOTP验证码

  堡垒机强制启用了 MFA 双因子认证,每次都需要打开 APP 输入验证码,对于登录频繁的人来说非常不方便,于是利用 expectoath-toolkit 完成了自动登录。(友情提示:本处堡垒机为仅内网可访问,且为边缘测试系统环境,出于安全考虑请不要在重要环境下使用)。

安装依赖

1
2
3
4
5
6
7
brew install oath-toolkit

# 此处secret为绑定 MFA 时的二维码扫描出来后的结果
oathtool --totp -b [secret]
# 执行上一行命令会产生一个动态验证码,对比手机APP上的验证码

brew install expect

脚本

保存为 jump.sh,并 chmod +x jump.sh 给予执行权限即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/expect

catch {spawn ssh -p 2222 alliot@xx.com}
expect "*password:*"
send "密码\r"


expect "*OTP*" {
set code [exec sh -c {oathtool --totp -b 上面扫描出来的密钥}]
send "$code\r"}


interact

脚本非常简单,捕捉 ssh -p xx 的输出,匹配到 password 时发送密码,匹配到 OTP 时,发送 oathtool 生成的 TOTP 验证码。 之后直接执行 jump.sh 脚本即可登录堡垒机。