• 计算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
    ')
        res = crt.Screen.WaitForString('Verification code:',3)
        if res == 0:
            return
        crt.Screen.Send(code+'
    ')
        res = crt.Screen.WaitForString("assword:", 3)
        if res == 0:
            return
        crt.Screen.Send(passwd + '
    ')
    
    
    gcode = GoogleCode('google_key')
    AutoLogin('hemingyuan','password',gcode)
    
    
  • 相关阅读:
    剑指offer-矩形覆盖
    剑指offer-变态跳台阶
    剑指offer-跳台阶
    剑指offer-斐波那契数列
    剑指offer-旋转数组的最小数字
    剑指offer-用俩个栈实现队列
    剑指offer-重建二叉树
    剑指offer-从尾到头打印链表
    http头
    mysql-8.0解压缩版安装配置完整过程
  • 原文地址:https://www.cnblogs.com/hemingyuan/p/7760505.html
Copyright © 2020-2023  润新知