schizo
schizo

Reputation: 93

Paramiko exec_command, timeout not working

Setup: paramiko 1.15.1 According to the documentation a timeout was created for exec_command since 1.10.0 but for some reason it's not working for me. Do I have an error in my code that I've missed or is it in fact a bug?

I have this code

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

That I'm executing as follows:

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

What is wrong here?

Upvotes: 2

Views: 20429

Answers (1)

schizo
schizo

Reputation: 93

It was of course a simple typo.

When calling exec_command I sent the value of timeout instead of timeout=value.

self.ssh.exec_command(command, timeout)

should have been

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

Upvotes: 5

Related Questions