# 实例化SSHClient
client = paramiko.SSHClient()
# 自动添加策略,保存服务器的主机名和密钥信息
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
loger.debug("hostname=%s username=%s password=%s" %(equ_ip_s,username_s,passwd_s))
# 连接SSH服务端,以用户名和密码进行认证
client.connect(hostname=equ_ip_s, username=username_s, password=passwd_s)
chan = client.invoke_shell()
chan.settimeout(9000)
# 设置结束条件
p = re.compile(r'xxx')
loger.debug("client is %s",client)
loger.debug("cmd is '%s'",cmd)
chan.send(cmd+'\n')
# chan.send(cmd+'\n')
result = ''
result = chan.recv(4096)
# 循环获取数据
while True:
chan.send(" ")
results = chan.recv(1024000)
result += results
if p.search(results):
print len(result)
print type(result)
chan.send('q')
break
# print result
# 拆分获取每行的数据
result2 = re.split(r'\r\n', result)
网友评论