2

I need some help with paramiko(python). I have a server with non-standard SSH interface so I used interactive mode and I am running couple of commands under the same channel(exec_command doesn't work for me). Everything is fine however I would like to introduce some timeout per command because the application stays in the while loop when the data is not received. Channel.settimeout doesn't seem to work properly as the application timeouts after 1st command. I cannot use signals due to threading. When I used time() to count the time after data variable is empty I noticed is that the whole code execution hangs, most probably waiting for the channel data. Any advise how to solve this would be highly appreciated.

    ssh=paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        connect = ssh.connect(host,username=,password=,timeout=20)
        file=open("test.txt","w+")
        channel = ssh.invoke_shell()
        data = channel.recv(5000)
        set_of_commands = ["cmd\n","reset\n"]

        while set_of_commands is not None:

            file.write(data)
            # New command might be executed here if we have 'system:'
            if re.match("system:",data) is not None:
                try:
                    command=set_of_commands.pop()
                    channel.send(command)
                except:
                    break
            data=channel.recv(5000)
2
  • timeout used in ssh.connect API is for TCP connect timeout. Commented Nov 3, 2014 at 3:55
  • Have you tried using channel.recv_exit_status() ? Commented Nov 3, 2014 at 4:04

1 Answer 1

1

You could probably use below for having timeout for each command. I have taken reference from this post. Python Paramiko timeout with long execution, need full output

chan = ssh.get_transport().open_session()

cmd = "timeout {0} {1}\n".format(timeouttime, cmd)

chan.exec_command(cmd)
Sign up to request clarification or add additional context in comments.

2 Comments

This will not work for me because I don't have an option to run 'timeout'+ command. Don't have CoreUtils GNU here.
I think you should try for channel.recv_exit_status() API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.