Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connection.recv() timeout #2061

Open
stvnkem opened this issue May 18, 2022 · 2 comments
Open

connection.recv() timeout #2061

stvnkem opened this issue May 18, 2022 · 2 comments

Comments

@stvnkem
Copy link

stvnkem commented May 18, 2022

Hello!

I am trying to connect to a console via SSH terminal server
When the server port does not have any console connected or the device is powered off the

connection.send(b"\n")
connection.recv(4096)

does not return any output, so the scrip gets stuck. How can I set a short timeout value?

 ssh.connect(term_serv_ip_address, port=term_serv_ssh_port, username=term_serv_user,
                        password=term_serv_passw)
            connection = ssh.invoke_shell()
            connection.send(b"\n")
            time.sleep(.5)

            # login
            output = connection.recv(4096).decode('utf-8')
            hostn = re.search(r'<(ipi-\w{3,4}\d{3,4}-r-al-\d{2})>', output)
            print(output)
@ghost
Copy link

ghost commented May 24, 2022

Hi @stvnkem,

I think this example code might help with this issue. You can add a timeout argument when calling the connect() method to specify the timeout in seconds.

import socket
import paramiko

def main():

    #arbitrary connection settings for the purposes of demonstration
    ip = "10.0.99.1"
    username = "example username"
    password = "example password"
    port = 22

    client = paramiko.SSHClient()
    try:
        client.connect(ip, port, username, password, timeout=5)
    except socket.timeout: 
        print("Connection Timed out")
    print("complete")

main()

@zhangzzgit
Copy link

zhangzzgit commented Jun 2, 2022

you can use '''.recv_ready() '''

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants