Python implements SSH remote login and command execution (Sharing), pythonssh

Source: Internet
Author: User

Python implements SSH remote login and command execution (Sharing), pythonssh

In AutomationTestIn the process, the common operation is to operate the remote host. How can this operation be performed? Remotely log on to the host using SSH, and then execute the corresponding command.

UsePythonTo achieve these operations is quite simple. Below isTestCode.

The code is as follows: (code runtime environment: python27 + eclipse + pydev)

import paramiko  def sshclient_execmd(hostname, port, username, password, execmd):   paramiko.util.log_to_file("paramiko.log")      s = paramiko.SSHClient()   s.set_missing_host_key_policy(paramiko.AutoAddPolicy())      s.connect(hostname=hostname, port=port, username=username, password=password)   stdin, stdout, stderr = s.exec_command (execmd)   stdin.write("Y") # Generally speaking, the first connection, need a simple interaction.      print stdout.read()      s.close()          def main():      hostname = '10.***.***.**'   port = 22   username = 'root'   password = '******'   execmd = "free"      sshclient_execmd(hostname, port, username, password, execmd)       if __name__ == "__main__":   main() 

The above Python method for implementing SSH remote login and executing commands (Sharing) is all the content shared by Alibaba Cloud. I hope you can give us a reference and support for the customer's house.

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.