show_command_threaded.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:python-scripts 作者: Sandeepsr 项目源码 文件源码
def GetDeviceConfig(ip,command,delay_factor=2,max_loops=30):
    output = str()
    try:
        session = paramiko.SSHClient()
        session.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        session.connect(ip, username = username, password = password)
        connection = session.invoke_shell()
        connection.send("term len 0\n")
        connection.send(command)
        for i in xrange(max_loops):
            time.sleep(2 * delay_factor)
            if connection.recv_ready():
                output += connection.recv(MAX_BUFFER)
                # Check for Invalid command
                if 'invalid' in output.lower():
                    message = "Invalid Command sent: {}, error message: {}".format(command, output)
                    print 'Invalid comamnd found...', message
            else:
                print "recv_ready = False "
                print "\nDONE for device %s" % ip
                session.close()
                return output
    except paramiko.AuthenticationException:
        print "* Invalid username or password. \n* Please check the username/password file or the device configuration!", ip
        pass
    except Exception as e:
        print 'Error in connection {} , Device : {}'.format(e , ip)
        pass


#Open SSHv2 connection to devices
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号