跳板机+谷歌验证码 ssh 自动登录

搞了2,3个小时记录一下吧。
本博客实现

  1. ssh 自动登录(包括自动输入 IP, 密码和谷歌验证码)
  2. (win os) 复制密码+验证码到剪贴板

谷歌验证码计算

这里用的 python
googleCode_base.py

# -*- coding:utf-8 -* 
import hmac, base64, struct, hashlib, time
import platform

def get_hotp_token(secret, intervals_no):
    key = base64.b32decode(secret, True)
    msg = struct.pack(">Q", intervals_no)
    h = hmac.new(key, msg, hashlib.sha1).digest()
    # 很多网上的代码不可用,就在于这儿,没有chr字符串
    o = ord(chr(h[19])) & 15
    h = (struct.unpack(">I", h[o:o+4])[0] & 0x7fffffff) % 1000000
    return h

def get_totp_token(secret, bias):
    return get_hotp_token(secret, intervals_no=int(time.time()+bias)//30)

def get_google_code():
    secret="XXXXX" # 这里是谷歌双因子认证:Google Authenticator 的 Secret
    googlecode = get_totp_token(secret, 0) # CHJ_WARN 这个参数是试出来的
    return '%06d' % googlecode

注意上面 CHJ_WARN 这里那个数字需要自己试, 主要可能电脑时间不一致,需要你盯着手机的验证码,然后不断运行程序打印出来自己估计出差的时间

也可以通过下面的程序试出来

for i in range(-3000, 3000, 30):
        googlecode = get_totp_token(secret, i) 
        print(googlecode, i)

复制到剪贴板

这个主要是我希望使用openvpn的时候方便输入
googleCode.py

#  把googleCode_base.py的代码拷过来
xxxxxx

import win32con
import win32clipboard

def set_text(string):
    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    win32clipboard.SetClipboardData(win32con.CF_UNICODETEXT, string)
    win32clipboard.CloseClipboard()

#pyperclip.copy(get_google_code())
set_text( "passward"+get_google_code() )

如果想编译成exe程序锁定到任务栏就

# pip install pyinstaller
pyinstaller -F -w googleCode.py

当然这个是手动复制,也可以通过自动化程序捕捉程序输入框然后自动输入,这里不做了。

为ssh 自动登录做准备

googleCode.py

#  把googleCode_base.py的代码拷过来
xxxxxx
print( get_google_code() )

ssh 自动登录

大致思想是执行python脚本获得其输出
ssh_auto.sh

#!/usr/bin/expect -f
# 登录到服务器之后不这样设置 vim 有问题
trap {
 set rows [stty rows]
 set cols [stty columns]
 stty rows $rows columns $cols < $spawn_out(slave,name)
} WINCH

set user xxxxx
set host x.x.x.x
set jump x.x.x.x
set password xxxx
set timeout 30

spawn ssh $user@$jump
#expect {
#    "yes/no" {send "yes\r"}
#    "*assword:" {send "$password\r"}
#}

expect {
    "Verification code*" {
        send_user "\rEnter verification code: "
        #expect_user -re "(.*)\n"
        #set veri_code $expect_out(1,string)
        # 上面那两行是手动输入,下面这个则是依靠自动计算
        set veri_code [ exec python {googleCode.py} ]

        send "${veri_code}\n"
    }
}

expect {
    "yes/no" {send "yes\r"}
    "*assword:" {send "$password\r"}
}

expect {
    "* Please enter your Login Ip" {send "$host\r"}
}

参考资料

expect 交互输入,执行bash命令

https://www.cnblogs.com/zhengbin/p/8872836.html
https://zhuanlan.zhihu.com/p/64897121
https://zoomadmin.com/HowToInstall/UbuntuPackage/oathtool
https://blog.csdn.net/u013992330/article/details/78035040

python计算谷歌验证码

https://stackoverflow.com/questions/8529265/google-authenticator-implementation-in-python
https://www.jianshu.com/p/be4a460117d5

主要遇到的问题

  1. 验证码可能有固定的时间偏差需要自己调
  2. 验证码那里网上一般是 py2,py3的话会有一些问题
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值