设置: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)
这里有什么问题?
答案 0 :(得分:4)
这当然是一个简单的错字。
调用exec_command时,我发送的是timeout值而不是timeout = value。
self.ssh.exec_command(command, timeout)
应该是
self.ssh.exec_command(command, timeout=60)