美文网首页程序员
交互式shell——编写基于TOTP验证的远程登陆脚本

交互式shell——编写基于TOTP验证的远程登陆脚本

作者: __Jo | 来源:发表于2019-10-11 14:09 被阅读0次

环境

Macbook Pro Go zsh shell expect shell

交互式登陆

用户输入ssh user@remote_host
会提示输入基于TOTP的验证码,
确认后再次提示输入密码。
所以主要解决两个问题:

  1. 交互处理
  2. 脚本中获取TOTP验证码

第一步:编写 expect shell 脚本 relay.exp

#!/usr/bin/expect
# 取得命令第一个参数作为变量totp的值
set totp [lindex $argv 0]
# 同理,取得pass的值
set pass [lindex $argv 1]
# 发起执行ssh
spawn ssh user@remote_host
# expect语法是 expect shell 内置语法用于检测输出
expect {
# 服务端返回 "亲爱的" 表示远程登陆成功
# 由于ssh可以设置session复用,已经登陆过,再次登陆无需密码
# 所以区分下是否已经直接登陆成功
    "亲爱的" {
        interact
        exit
    }
# 当服务器返回输入TOTP验证码时,将变量totp 发送给服务端
    "Verification code:" {
        send "$totp\r"
# 同理发送密码
        expect "Password:"
        send "$pass\r"
# 保持交互状态,把控制权交给控制台
        interact
# 正常退出
        exit
    }
}

解决自动交互的雏形基本完成

./relay.exp  [TOTP验证码] [密码]

第二步:通过命令行直接获取TOTP验证码

常见的TOTP客户端 GOOGLE Authenticator 浏览器插件
而我们需要的是直接通过命令行获取,
这里推荐开源的基于Go的项目https://github.com/arcanericky/totp
编译完成后,使用效果:

./totp-darwin-amd64 my-totp-item
640938

最后组合效果,直接登陆

./relay.exp `totp-darwin-amd64 my-totp-item`  [密码]

相关文章

网友评论

    本文标题:交互式shell——编写基于TOTP验证的远程登陆脚本

    本文链接:https://www.haomeiwen.com/subject/egptmctx.html