程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python uses paramiko to manage multiple servers

編輯:Python

Pain points

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 :

  • use telnet
  • use PUTTY
  • use SCP
  • use XManager etc.

If the demand adds another , To download a file from the server , What should I do ? The usual method may be :

  • Linux Installation on FTP And configuration
  • Linux Installation on Sambe And configuration, etc

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

Introduce

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

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

Use

How to connect

Here are three uses paramiko Connect to Linux The code of the server .

Mode one

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"))

Mode two

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)

Mode three

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 .

Basic operation

Single command

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())

Multiple commands

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())

Upload files

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()

Download the file

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()

Summary

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 ~


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved