Take a common example in work , There is such a need : Need to use mac client , Connect remotely to Linux The server , Check the file size above , The methods commonly used are as follows :
If the demand adds another , To download a file from the server , What should I do ? The usual method may be :
You will find a commonality , Common solutions , Necessary configuration for remote server . If you need dozens or hundreds more Linux The server performs the same operation ? This kind of execution efficiency is bound to be low .
paramiko The birth of can solve the above problems .paramiko You only need to install... Locally python as well as PyCrypto, For connecting multiple servers , Operations that perform complex and repetitive operations are particularly helpful .
paramiko Yes, it is python Language to write a third-party library , Support encryption authentication , follow SSH2 agreement , You can connect to a remote server .
Languages that can run across platforms , Support for multiple platforms , Such as Linux、MacOS、Windows etc. . therefore , If needed SSH Connecting from one platform to another , When performing a series of operations ,paramiko It's a good choice .
Install third party libraries , You usually use pip Command line installation . Because of the Internet , Using the domestic image source will improve the download and installation speed .
pycrypto, because paramiko Module internal dependency pycrypto, So download and install first pycrypto.
The installation command is as follows :
pip install -i https://pypi.doubanio.com/simple/ --trusted-host pypi.doubanio.com pycrypto
pip install -i https://pypi.doubanio.com/simple/ --trusted-host pypi.doubanio.com paramiko
Here are three uses paramiko Connect to Linux The code of the server .
Connect by password
code snippet :
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("123.56.xx.xx",22,"root", " password ")
# The second line above is to allow the connection to be absent know_hosts Hosts in files .
print(ssh.exec_command("ps"))
Using the account + Password connection
code snippet :
import paramiko
t = paramiko.Transport(("127.0.0.1",22))
t.connect(username = "xxxx", password = "xxxxx")
print(t.sys.platform)
Use the secret key to connect , Two machines need to trust each other in advance .
Client's "id_rsa.pub" Files are added to the connected server "authorized_keys" In file .
If not , Manual creation required , Put it in ".ssh" File directory .
code snippet :
import paramiko
SSH_PRIVATE_KEY = '/Users/xinxi/.ssh/id_rsa' # Local key file path
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
key = paramiko.RSAKey.from_private_key_file(SSH_PRIVATE_KEY)
ssh.connect(hostname='123.56.xx.xx', port=22, username='root', pkey=key)
I recommend the third way , Because now the general company level test environment server , The operation and maintenance department does not provide the password of the server , Will be used uniformly " Springboard machine " Login server , Therefore, only secret key connection can be used .
You can print out the current directory file , exec_command You can put "Linux command ".
stdin, stdout, stderr = ssh.exec_command("ls")
# Get command results
result = stdout.read()
# Printout
print(result.decode())
On a daily basis , Will switch to a directory , Carry out multiple continuous commands .
paramiko For executing multiple instructions , Need to use ; Division .
stdin, stdout, stderr = ssh.exec_command("cd /data;pwd;ls")
# Get command results
result = stdout.read()
# Printout
print(result.decode())
obtain SFTP example , call put Method , Local and remote files .
# obtain SFTP example
sftp = paramiko.SFTPClient.from_transport(tran)
# Set upload local / Remote file path
localpath = "/Users/xinxi/Desktop/scp_test.py"
remotepath = "/data/test1.py"
# Perform upload actions
sftp.put(localpath, remotepath)
# Close links
tran.close()
obtain SFTP example , call get Method , Remote and local files .
# obtain SFTP example
sftp = paramiko.SFTPClient.from_transport(tran)
# Set upload local / Remote file path
localpath = "/Users/xinxi/Desktop/scp_test.py"
remotepath = "/data/test1.py"
# Perform the download action
sftp.get(remotepath, localpath)
# Close links
tran.close()
paramiko For and multiple servers shell Interactive commands , It's a good solution . In addition, we often encounter the need to upload the local automated test report to the remote server during the test process , Or you need to pull the automated test report on the server to the local for operation .
paramiko Provides ease of use 、 convenience , Greatly improve work efficiency ~