Advanced and elegant remote operation of the server: understanding the Python module Paramiko

Posted by aka_bigred on Tue, 11 Jan 2022 12:07:07 +0100

preface

During the test process, we often encounter the need to upload local files to the remote server, or pull the files on the server to the local for operation. In the past, we often used the xftp tool.

Today, let's introduce a Python library Paramiko, which can help us upload and download remote servers through code, or input operation commands to remote servers.

Paramiko

Paramiko is a third-party library of Python. It can remotely connect to the Linux server and operate Linux through python. It can download and upload files to the remote server.

install

Since it is a third-party library, we can install it through pip:

pip install paramiko

Basic use

The Paramiko library mainly includes two parts: SSHClient and SFTPClient.

SSHClient: indicates ssh commands similar to Linux. We can perform some command operations on the remote server through the SSHClient module (Linux).

SFTPClient: similar to SFTP tool, it can upload and download files from remote server.

SSHClient

Here, connect to the remote server through sshclient and execute Linux commands. First, instantiate the sshclient under Paramiko, connect using connect under sshclient, and then operate some commands:

import paramiko
# Instantiate the SSHclient under paramiko method
ssh = paramiko.SSHClient()
# Save server key
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())  
# Enter the server address, account name and password
ssh.connect(hostname='xxxx', port=22,username='root',password='xxxxxx')
# Three data are returned, the first is the input command, the second is the result returned by the command, and the third is the result returned when the command is wrong
stdin, stdout, stderr = ssh.exec_command('pwd;lll')
# The current path result is returned. If there is an error, it is null
print(stdout.read().decode('utf-8'))
# Returns an incorrect execution result. If it is correct, it returns null
print(stderr.read().decode('utf-8'))

After executing the code operation, we can clearly see that we have completed the input of Linux commands and returned the correct information and error information.

SFTPClient

SFTPClient also introduced tools similar to xftp, which can help us upload and download remote files.

In fact, the same method is used. First instantiate, then log in to the server, create an sftp tool, and then upload and download files.

Upload file

Here, first write a txt file, and then transfer this file to the server through code.

The upload method here is put (server path, local path):

import paramiko
# Instantiate the SSHclient under paramiko method
ssh = paramiko.SSHClient()
# Save server key
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Enter the server address, account name and password
ssh.connect(hostname='xxxxx', port=22, username='root',password='xxxxxx')
# Create sftp Client
sftp = paramiko.SFTPClient.from_transport(ssh.get_transport())
# Local path
aaa = "anjing.txt"
# Remote path
bbb = "/home/anjing/222/anjing.txt"
sftp.put(aaa, bbb)

After executing the code, we find that the file just uploaded already exists on the server.

File download

We modify the text content, then transfer the file to our local file and edit it with vi command:

The download method used here is get (server path, local path):

import paramiko
# Instantiate the SSHclient under paramiko method
ssh = paramiko.SSHClient()
# Save server key
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Enter the server address, account name and password
ssh.connect(hostname='xxxxx', port=22, username='root',password='xxxxxx')
# Create sftp Client
sftp = paramiko.SFTPClient.from_transport(ssh.get_transport())
# Remote path
bbb = "/home/anjing/2
# Download File
sftp.get(bbb, r'E:\web\anjing_01.txt')

After executing the code, we found that we have successfully downloaded the modified file on the server to the local and modified the name.

summary

Through a small example, this paper briefly introduces how Paramiko uploads, downloads files and executes Linux commands. For our testing, whether in daily testing or in writing automation, we can try when we need server operation. First, we can install a wave stably in front of the leaders, and second, we can improve our Python knowledge.

Well, thank you for reading. I hope it will help you.

Now there is such an opportunity. I invite you to join our software testing learning exchange group: 914172719. Note "csdn". You can discuss and exchange software testing together, learn software testing technology, interview and other aspects of software testing together, and have free live classes to gain more testing skills. Let's advance Python automated testing / test development together, The road to high pay.

Let's give you a word of encouragement: when our ability is insufficient, the first thing to do is internal practice! When our ability is strong enough, we can look outside!

Finally, we have prepared a matching learning resource for you. You can get a 216 page Software Test Engineer Interview document information without official account: heartbroken spicy bar. And the corresponding video learning tutorials for free!, The materials include basic knowledge, Linux essentials, Shell, Internet program principles, Mysql database, special topics of packet capture tools, interface test tools, test advanced Python programming, Web automation test, APP automation test, interface automation test, advanced test continuation, test architecture, development test framework, performance test, security test, etc.

Haowen recommendation

Job transfer interview, job hopping interview, these interview skills that software testers must know!

Interview experience: move bricks in first tier cities! Another software testing post, 5000 will be satisfied

Interviewer: I've worked for three years and have a preliminary test? I'm afraid your title of software test engineer should be enclosed in double quotation marks

What kind of person is suitable for software testing?

The man who left work on time was promoted before me

The test post changed jobs repeatedly and disappeared

As a test engineer with 1 year working experience, my suggestions before the interview are as follows

"After a year in office, the automation software test hired by high salary was persuaded to quit."

4 months of self-study software testing into Ali! How to move from functional testing to automation... What have I experienced

6000 yuan applied for the training course. Three months later, I successfully "cheated" into Tencent's big factory with a monthly salary of 15000 yuan

Topics: Python Linux Programmer software testing server