Python paramiko implements ssh remote access

Source: Internet
Author: User

After paramiko is installed, see the following example:

Copy codeThe 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:
Copy codeThe Code is as follows:
Str = chan. recv (recv_buffer)
While not str. endswith ('#'):
Str = chan. recv (recv_buffer)

Related Article

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.