This is the third day of my participation in Gwen Challenge

I believe you should be familiar with the Linux operating system (Ubuntu, Centos, etc.), so the Linux operating system needs to use some remote SSH tools, especially the public network server.

Common SSH tools include Xshell, MobaXterm, and Termius

Upload files to the remote Linux server, such as Xftp or Filezilla

Some of these tools are free and some are charged. And it’s a little expensive. How to use Python to connect to a remote Linux server, execute commands and upload files. The Python library used here is Paramiko

Core Knowledge points (highlights) :

Python connects to a remote Linux server

2. Run the command and return the result

3. Upload files to the remote Linux server

4. Download files from remote Linux

01 Paramiko library

1. Install the Paramiko library

Before you start writing the code, install the Python library to connect to the Linux server using the following command:

#1. Install dependencies
pip install ecdsa
pip install Crypto
pip install Twisted
#2. Install Paramiko
pip install paramiko
Copy the code

Install the dependency libraries first and then install Paramiko. If you install Paramiko directly, an error will be reported.

2. Paramiko Core components

SSH component: SSHClient. Similar to the Linux SSH command, it is used to run remote commands.

SFTP component: SFTPClient. Similar to the SFTP command in Linux, SFTPClient is used to upload and download files, and modify file permissions.

02 SSHClient

1. Introduction to common methods of SSHClient

Here Chen Elder brother to his remote server as an example to show you, first connect to the server

Import paramiko # IP, username, password IP = "remote Linux IP address" port = 22 user = "root" password = "password Set_missing_host_key_policy (paramik.autoaddPolicy ())) # Connect to the remote machine, address, port, User name Password ssh.connect(IP, port, user, password, timeout=10)Copy the code

Run the Linux command, for example, ls

Command1 = "ls" ssh.exec_command(command1) # stdout Exec_command (command2) # exec_command result = stdout.read() print(result)Copy the code

Xshell returns the result:

Python execution code result:

2. Run multiple Linux commands

In the preceding example, only one Linux command is executed. In practice, two or more Linux commands need to be executed. The following example shows how to execute multiple commands

The wrong way:

Command1 = "CD/WWW /wwwroot" command2 = "ls" SSH. Exec_command (command1) stdin, stdout, Stderr = ssh.exec_command(command2) # print(result)Copy the code

Xshell xshell xshell xshell xshell xshell xshell xshell xshell xshell xshell xshell xshell xshell xshell xshell xshell xshell

Python code execution results:

You can see that the result is the same as before, so this method is wrong.

The right way:

Command = "CD/WWW /wwwroot; ls" stdin, stdout, Stderr = ssh.exec_command(command) # output command execution result result = stdout.read() ## bytes transform STR result = STR (result) result = result.split('\\n') for i in result: print(i)Copy the code

In direct use; After converting multiple commands to STR, divide the result by **\\n** and output it in line

03 SFTPClient

1. Common methods of SFTPClient are introduced

2. Upload files to Linux

Here first in the local new text file: Chen ge. TXT, and input some content inside

Connecting to a Linux Server

Tran = paramik. Transport((' remote Linux IP ', 22)) SFTP = Paramiko. Sftpclient.from_transport (tran)Copy the code

Upload a file

Remotepath = "/ WWW /wwwroot// chinggo.txt" Remotepath) # Close connection tran.close()Copy the code

The effect is as follows:

3. Download the file from the Linux server

Create a new text file: Python researcher.txt on the Linux server and write the content

The download file

Remotepath2 = "/ WWW /wwwroot/Python researcher.txt" Localpath2) # Close connection tran.close()Copy the code

Effect:

04 summary

This article introduces the Paramiko library and the two components of SSH and SFTP.

Python connects to a remote Linux server

2. Run the command and return the result

3. Upload files to the remote Linux server

4. Download files from remote Linux