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
评论列表
文章目录