第二篇:ssh.invoke_shell

发布时间:2019-03-01 10:36:10编辑:auto阅读(4580)

    接上一篇:按照上一篇的方式,在没有对ssh.invoke_shell()执行后的登录提示符进行判断的话,那边有部分机器就回因为返回为空导致程序卡死。

    正常机器  ssh.recv(9999)  命令返回内容:

    b'Last login: Sat Aug 18 22:06:17 2018 from 172.37.100.111\r\r\n[cattsoft@ZB_KT_MAS2 ~]$ '
    b'export LANG=en_US.UTF-8 \r\n[cattsoft@ZB_KT_MAS2 ~]$ export LANGUAGE=en \r\n[cattsoft@ZB_KT_MAS2 ~]$ su - \r\nPassword: '

          程序的模拟登陆过程如下(以下图片内容为ssh.recv(9999) 命令接收返回值解码后的结果):

         

    异常机器ssh.recv(9999)  命令返回内容:

    b'export LANG=en_US.UTF-8 \r\n'
    b'export LANGUAGE=en \r\nsu - \r\nLast login: Sat Aug 18 21:42:09 from 172.16.112.2\r\n[cattsoft@trancache01 ~]$ '

          程序的模拟登陆过程如下(以下图片内容为ssh.recv(9999) 命令接收返回值解码后的结果)

         

     

    如上,按照原来的循环方式,循环无法判断Password:位置,所以异常机器此时就回出现卡死现象,解决此问题的做法:在执行命令前,先判断一次登陆符:“$”,然后在执行命令。

    def verification_ssh(host,username,password,port,root_pwd,cmd):
        s=paramiko.SSHClient()
        s.load_system_host_keys()
        s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        s.connect(hostname = host,port=int(port),username=username, password=password)
    
        if username != 'root':
            ssh = s.invoke_shell()
            time.sleep(0.1)
    
    #先判断提示符,然后下一步在开始发送命令,这样大部分机器就都不会出现问题 buff
    = '' while not buff.endswith('$ '): resp = ssh.recv(9999) # print(resp) buff += resp.decode('utf8') time.sleep(0.1) print('获取登录后的提示符:%s' %buff) ssh.send(' export LANG=en_US.UTF-8 \n') #解决错误的关键,编码问题 ssh.send('export LANGUAGE=en \n') ssh.send('su - \n') buff = "" while not buff.endswith('Password: '): #true resp = ssh.recv(9999) print(resp) buff +=resp.decode('utf8') print('hhhhh') print(buff) ssh.send(root_pwd) ssh.send('\n') buff = "" # n = 0 while not buff.endswith('# '): # n += 1 resp = ssh.recv(9999) print(resp) buff +=resp.decode('utf8') # print(n) # if n >=3: # break # print(buff) ssh.send('sh /tmp/check/101.sh') #放入要执行的命令 ssh.send('\n') buff = '' # m = 0 while not buff.endswith('# '): resp = ssh.recv(9999).decode() buff +=resp # m += 1 # print(m) result = buff # print(type(result)) # print(result) s.close() if __name__ == "__main__": verification_ssh('测试IP地址', '普通账号', '普通账号的密码', '52222', 'root密码', 'id')

     上一篇:https://www.cnblogs.com/apff/p/9484939.html (python如何实现普通用户登录服务器后切换到root用户再执行命令遇到的错误解决 )

     

关键字

上一篇: Django(三):HttpReques

下一篇: python基础学习1