python realizes the operation of remote server

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. In the past, we often used the xftp tool. Today, we will introduce a python library Paramiko, which can help us upload and download remote servers through code. You can also input operation commands to the remote server.

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

Paramiko library mainly includes two parts: SSHClient and SFTPClient

SSHClient: refers to 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
# Pair paramiko Under method SSHclient Instantiate
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 has also introduced tools similar to xftp, which can help us upload and download remote files. In fact, the method is the same. First instantiate, then log in to the server, create sftp tool, and then upload and download files

Upload file

Quiet here, first write a txt file, and then transfer this file to the server through code

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

import paramiko
# yes paramiko Under method SSHclient Instantiate
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')
# establish 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
# yes paramiko Under method SSHclient Instantiate
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')
# establish 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 changed the name

summary

Through a small example, this paper briefly introduces how paramiko uploads and downloads files and executes linux commands. For our testing, whether in daily testing or in writing automation, when we need server operation, we can try it. First, we can install a wave in front of the leaders, and second, we can improve our knowledge of python. Well, thank you for your reading. I hope it will help you.

 

Keywords: Python

Added by BobcatM on Tue, 01 Feb 2022 18:49:14 +0200