Pythonparamiko implements ssh remote access

Source: Internet
Author: User
This article mainly introduces how to implement ssh remote access through the pythonparamiko module. for details, refer to the following example after installing paramiko:

The code is as follows:


Import paramiko

# Set the remote host address and port for ssh connection
T = paramiko. Transport (ip, port ))
# Set logon name and password
T. connect (username = username, password = password)
# Open a channel after successful connection
Chan = t. open_session ()
# Set Session Timeout
Chan. settimeout (session_timeout)
# Enable remote terminal
Chan. get_pty ()
# Activating terminal
Chan. invoke_shell ()
Then, you can remotely execute commands and obtain local feedback through chan. send ('command ') and chan. recv (recv_buffer.
For example:
Chan. send ('pwd ')
Print chan. recv (65535)

The point is that some commands are executed for a long time, and the desired result may not be obtained when the returned result is inappropriate. you can use time. sleep () to wait, or use some Conditional loops.
For example:

The code is as follows:


Str = chan. recv (recv_buffer)
While not str. endswith ('#'):
Str = chan. recv (recv_buffer)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.