Windows environment, using Python to implement SSH operations

Source: Internet
Author: User

Work needs, often use Xshell to connect remote Linux machines, view some of the above document information, and then constantly switch directories, cd.....cd ..., heart tired.

Why not use the script to implement it. Go online to read some information, there is a solution, thank you for sharing the results of the IT people, here to share my experience it.

1, the installation of related support library:

Install Pycrypto:http://www.voidspace.org.uk/python/modules.shtml#pycrypto
Install paramiko,https://pypi.python.org/pypi/paramiko/1.7.7.1
Extract to C disk, cmd switch to the corresponding directory execution: Python setup.py build, then execute Python setup.py install

2, Paramiko use:

def get_search_text (self):

# SSH Remote Connection
SSH = Paramiko. Sshclient ()
Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
Ssh.connect (HOST_IP, host_port, username, password)

# orders to be executed
Swithch_path = "CD docker/conf/"
search_contents = "grep ' Game ' Config.xml"

# Note: When you execute multiple commands sequentially, separate the commands with semicolons
Command = swithch_path+ ";" +search_contents

# Execute Command
stdin, stdout, stderr = ssh.exec_command (command)
Err = Stderr.readlines ()
out = Stdout.readlines ()
If Len (err)!= 0:
Print "==execute command[", Command, "] failed:", err
Else
Print "==execute command[", Command, "] success:", out

Ssh.close ()

Return out

3, the definition of this method, directly with robot to call the good, and then directly run the case of robot, you can get to the Dongdong.


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.