gpt4 book ai didi

python - Paramiko exec_command,超时不工作

转载 作者:太空宇宙 更新时间:2023-11-04 10:30:07 26 4
gpt4 key购买 nike

设置:paramiko 1.15.1根据文档,从 1.10.0 开始为 exec_command 创建了超时,但由于某种原因它对我不起作用。我的代码中是否有我遗漏的错误,或者它实际上是一个错误?

我有这个代码

class CustomSSH(object):

def __init__(self, node):
self.node = node
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
self.privkey = paramiko.RSAKey.from_private_key_file('./secret.key')
except:
self.use_password = True

def connect_ssh(self, timeout=60):
try:
if self.use_password:
self.ssh.connect(self.node,
timeout=60,
username='xxx',
password='xxx',
look_for_keys=False)
else:
"""Using SSH Key"""
self.ssh.connect(self.node,
timeout=60,
username='xxx',
pkey=self.privkey,
look_for_keys=False)
self.using_ssh = True
return True

except paramiko.AuthenticationException, e:
if self.use_password:
raise paramiko.SSHException
print "Private key {}".format(e)
print "Trying using username/password instead"
self.use_password = True
self.connect_ssh()

except paramiko.SSHException, e:
print "some other error ", e
self.close()
raise
except Exception:
print "exception"
raise

def close(self):
print "Closing connection"
try:
self.ssh.close()
except:
print "Error closing connection"

def send_cmd(self, command, regexmatch='', timeout=60):
"""Issue Commands using SSH
returns a Tuple of (True/False, results)"""
try:
stdin, stdout, stderr = self.ssh.exec_command(command, timeout)
xresults = stdout.readlines()
results = ''.join(xresults)
re_status = re.compile(regexmatch, re.IGNORECASE)
if re_status.search(results):
return (True, xresults)
else:
return (False, xresults)
except Exception as e:
raise KeyError, e

我正在执行如下:

ssh = CustomSSH(node)
ssh.connect_ssh()
ssh.send_cmd(abort_cmd)

这里有什么问题吗?

最佳答案

这当然是一个简单的错字。

调用 exec_command 时,我发送了超时值而不是 timeout=value。

self.ssh.exec_command(command, timeout)

应该是

self.ssh.exec_command(command, timeout=60)

关于python - Paramiko exec_command,超时不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27292439/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com