Python SSH module logon, remote machine execution of shell Command instance parsing, pythonssh

Source: Internet
Author: User

Python SSH module logon, remote machine execution of shell Command instance parsing, pythonssh

Use the python SSH module to log on and execute shell commands on a remote machine.

(The Redhat series should be compatible if the test is successful in CentOS 7 .)

Install required modules first

# yum install python-dev# yum install python-devel# pip install pycrypto# pip install paramiko# pip install ssh

After all these are successful, write a Python script

# Vim remote_run.pyimport ssh # create an ssh client object myclient = ssh. SSHClient () # set to automatically accept the key myclient by default. set_missing_host_key_policy (ssh. autoAddPolicy () # connect to the remote host myclient. connect ("xxx.coder4.com", port = 22, username = "xxxx", password = "xxxx") # execute shell commands stdin, stdout, stderr = client.exe c_command ("ls-l") # Read the returned result print stdout. read () # execute the python script command stdin, stdout, stderr = client.exe c_command ("python/home/test. py ")

After creating an SSHClient object, in addition to executing commands, you can also enable an sftp session for transferring files and creating folders.

# Create sftp sessionsftp = client. open_sftp () # create the directory sftp. mkdir ('abc') # download files from the remote host. If the download fails, an exception may be thrown. Sftp. get ('test. sh', '/home/testl. sh ') # uploading a file to a remote host may also throw an exception sftp. put ('/home/test. sh', 'test. sh ')

Summary

The above is all the content about the python SSH module logon and remote Machine shell Command instance parsing, I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.