计算GoogleAuthenticatorCode使用CRT执行python脚本登录堡垒机

使用python计算出GoogleAuthenticatorCode,使用CRT脚本自动连接登录堡垒机。

此脚本是使用CRT内置crt.Screen.WaitForString 捕捉屏幕中出现的字符串,无法与弹出的对话框进行交互。因此此脚本在使用时,需要先登录一台拥有SSH功能的服务器。

如果想要直接通过CRT连接服务器,需要捕捉弹出框并自动填入数据,需要使用win32gui模块,CRT执行python脚本时使用CRT安装目录下自带的py2,不使用系统python。导致win32gui模块无法使用

如果SSH工具为xshell,将脚本中所有的crt修改为xsh即可

import hmac, hashlib, time, base64, struct


def GoogleCode(secretKey, interval=30):
    input = int(time.time()) // interval    # 时间间隔30s
    secretKey += (8 - (len(secretKey) % 8)) * '='       
    # py3中base64模块要求字符串必须为8的倍数,不足部分使用 = 补全
    
    secret = base64.b32decode(secretKey, True) 
    i_struct = struct.pack('>Q', input)
    h = hmac.new(secret, i_struct, hashlib.sha1).digest()
    index = ord(h[19]) & 15     # 对15做位与运算,得出的结果范围为0-15
    code = str((struct.unpack('>I', h[index:index + 4])[0] & 0x7fffffff) % 1000000)
    while len(code) < 6:    # 计算出的code不足6位时, 对code前面补0
        code = '0' + code
    return code

def AutoLogin(user, passwd, code):

    crt.Screen.Send('ssh '+ user + '@IP\r')
    res = crt.Screen.WaitForString('Verification code:',3)
    if res == 0:
        return
    crt.Screen.Send(code+'\r')
    res = crt.Screen.WaitForString("assword:", 3)
    if res == 0:
        return
    crt.Screen.Send(passwd + '\r')


gcode = GoogleCode('google_key')
AutoLogin('hemingyuan','password',gcode)

转载于:https://www.cnblogs.com/hemingyuan/p/7760505.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值