当前位置:网站首页>Operate the server remotely more gracefully: the practice of paramiko Library
Operate the server remotely more gracefully: the practice of paramiko Library
2022-07-04 04:37:00 【Software quality assurance】
Continue to adhere to the original output , Click on the blue word to follow me
author : Software quality assurance
You know :https://www.zhihu.com/people/iloverain1024
As a test , If you ask what tools you deal with more in your work , Presumably most people will not hesitate to say server .
Test environment construction 、 Code deployment 、 The problem location log query is inseparable from the operation server . We usually log in to the server to operate the service , Then switch to the corresponding log directory , adopt grep/tail Way to query the logs we want . Of course, there are also many clients on the market to assist us in operating the server , for example xshell/xftp, But even with the client , It is still impossible to minimize our manual pipeline operation . Therefore, this paper introduces an efficient Python library Paramiko, Help you develop your own log query tool .
Paramiko What can be done
paramiko yes Python A library written in language , follow SSH2 agreement , Support the connection of remote server in the way of encryption and authentication , utilize paramiko, It can be done conveniently SSH Connect the server and file transfer between servers .
Paramiko Some basic nouns in :
1. Channel: It's kind of safe SSH Transmission channel ;
2. Transport: When used, an encrypted Tunnels( passageway ), This Tunnels be called Channel;
3. Session: yes Client And Server Keep connected objects , use connect()/start_client()/start_server() Open a session .
How to use Paramiko
Paramiko Provide a wealth of API For our use , This section mainly introduces several commonly used API And how to use it .
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
pip install paramiko
1. Establishing a connection
paramiko.connect Detailed explanation of method parameters :
connect Common parameters
hostname Connected target host
port=SSH_PORT Designated port
username=None The verified user name
password=None Authenticated user password
pkey=None Private key mode is used for authentication
key_filename=None A file name or list of files , Specify private key file
timeout=None Optional tcp Connection timeout
allow_agent=True, Allow connection to ssh agent , The default is True allow
look_for_keys=True Whether in ~/.ssh Search for private key file , The default is True allow
compress=False, Whether to turn on compression
Method 1 、 Password connection server
import paramiko
from paramiko import SSHClient
def connect_with_password(host, username, password):
ssh = paramiko.SSHClient()
# Auto add policy , Save the host name and key information of the server , If you do not add , So it's no longer local know_hosts The host recorded in the file will not be able to connect
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_system_host_keys()
ssh.connect(host, username=username,
password=password)
session = ssh.get_transport().open_session()
AgentRequestHandler(session)
ssh_stdin, ssh_stdout, ssh_stderr = session.exec_command("ls -l")
content = ssh_stdout.read()
return session
Method 2 、 Key connection server
def connect_with_RSAKey(host, username, file):
# Configure private key file location
private = paramiko.RSAKey.from_private_key_file(file)
# private = paramiko.RSAKey.from_private_key_file('/Users/qa/.ssh/id_rsa')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect SSH Server side , Authenticate with user name and password
ssh.connect(hostname=host, port=22, username=username, pkey=private)
session = ssh.get_transport().open_session()
AgentRequestHandler(session)
ssh_stdin, ssh_stdout, ssh_stderr = session.exec_command("ls -l")
content = ssh_stdout.read()
return session
2. Carry out orders
Use exec_command Executing the command will return three messages :
1、 Standard inputs ( Used to implement interactive commands )--stdin
2、 standard output ( Save the normal execution result of the command )--stdout
3、 Standard error output ( Save the error message of the command )--stderr
# Defined function ssh, Write the operation contents into the function
def ssh_exe_cmd(host, username, password, content):
session = connect_with_password(host, username, password)
# Use exec_command Method to execute the command , And use variables to receive the return value of the command and use print Output
stdin, stdout, stderr = session.exec_command(content)
return stdout.read()
3. Upload and download
Like we use xshell Executing instructions on the server is the same as querying logs , We can be like xftp Upload and download files on the server .
# File download
def download_file_ftp(host, username, password, local_path, remote_path):
# Create with the server ssh Connect ,transport Methods establish channels , Write server information in tuples
ssh_ftp = paramiko.Transport((host, 60317))
ssh_ftp.connect(username=username, password=password)
# After creating a connection , Use sftpclient Classes and from_transport( In parentheses, write the one created above Transport passageway ) Based on the above ssh Create a connection sftp Connect , Defined as ftp_client Variables are easy to reference
ftp_client = paramiko.SFTPClient.from_transport(ssh_ftp)
# Download the file
#ftp_client.get(" Target file ", r" Save the location , Write file name ")
ftp_client.get(remote_path, local_path)
# close ssh Connect
ssh_ftp.close()
# Upload files
def upload_file_ftp(host, username, password, local_path, remote_path):
# Create with the server ssh Connect ,transport Methods establish channels , Pause server information in tuples
ssh_ftp = paramiko.Transport((host, 60317))
ssh_ftp.connect(username=username, password=password)
# After creating a connection , Use sftpclient Classes and from_transport( In parentheses, write the one created above Transport passageway ) Based on the above ssh Create a connection sftp Connect , Defined as ftp_client Variables are easy to reference
ftp_client = paramiko.SFTPClient.from_transport(ssh_ftp)
# Upload files
ftp_client.put(local_path, remote_path)
# close ssh Connect
ssh_ftp.close()
be based on Paramiko Develop log query tools
The implementation principle is very simple , Is to automatically disconnect the server ( logon server ) Part of , The execution tool automatically connects to the server , Users only need to enter the log query instruction . The complete code is as follows :
# -*- coding: utf-8 -*-
# @Author : qualityassurance
# @Email : [email protected]
# @File : ParamikoUtil.py
# @Time : 2022/7/3 11:13
import paramiko
from paramiko.agent import AgentRequestHandler
host = ''
port = ''
username = ''
password = ''
def connect_with_password(host, port, username, password):
""" Connect to server ,Client = paramiko.SSHClient()
"""
ssh = paramiko.SSHClient()
try:
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_system_host_keys()
ssh.connect(host, port=port, username=username,
password=password)
except Exception as e:
print(e)
return ssh
def ssh_exe_cmd(client, cmd):
""" Return the command result executed remotely :stdin, stdout, stderr (stdout) and decode Output ...
"""
stdin, stdout, stderr = client.exec_command(cmd)
print(stdout.read().decode())
return stdout
def main():
""" Set remote execution Linux Operation command input ..."""
client = connect_with_password(host, port, username, password)
while True:
cmd = input(" Enter the command to be executed ( Empty to exit ):").strip()
if cmd == "":
return False
else:
ssh_exe_cmd(client, cmd)
if __name__ == "__main__":
main()
Like it , Just click on it. Zanhe is looking at Let's go
- END -
Scan the code below to pay attention to Software quality assurance , Learn and grow with quality gentleman 、 Common progress , Being a professional is the most expensive Tester!
Previous recommendation
Talk to self-management at work
Experience sharing | Test Engineer transformation test development process
Chat UI Automated PageObject Design patterns
边栏推荐
- Modstartblog modern personal blog system v5.2.0 source code download
- Self sharing of a graduate
- Keysight N9320B射频频谱分析仪解决轮胎压力监测方案
- C language one-way linked list exercise
- 更优雅地远程操作服务器:Paramiko库的实践
- 资深开发人员告诉你,怎样编写出优秀的代码?
- Leetcode skimming: binary tree 04 (sequence traversal of binary tree)
- [microservices openfeign] two degradation methods of feign | fallback | fallbackfactory
- leetcode:1314. Matrix area and [2D prefix and template]
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
猜你喜欢
Longest increasing subsequence problem (do you really know it)
架构实战营 - 第 6 期 模块九之毕业设计
【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装
Balloon punching and Boolean operation problems (extremely difficult)
Graduation project
[security attack and Defense] how much do you know about serialization and deserialization?
Intersection traffic priority, illustration of intersection traffic rules
GUI 应用:socket 网络聊天室
Exploration and practice of eventbridge in the field of SaaS enterprise integration
仿《游戏鸟》源码 手游发号评测开服开测合集专区游戏下载网站模板
随机推荐
"Don't care too much about salary when looking for a job", this is the biggest lie I've ever heard
[wechat applet] good looking carousel map component
两万字带你掌握多线程
Redis:哈希hash类型数据操作命令
Leader: who uses redis expired monitoring to close orders and get out of here!
最长递增子序列问题(你真的会了吗)
Cmake compilation option setting in ros2
C language one-way linked list exercise
Asynchronous development process - touch your hand and lead you to realize a promise
rac删除损坏的磁盘组
Touch your hand and bring you a commonjs specification
Instructions for LPC interrupt binding under ft2000+
C语言单向链表练习
Dry goods | detailed explanation of webshell Foundation
Kivy教程之 自定义字体(教程含源码)
LeetCode136+128+152+148
Kivy教程之 更改背景颜色(教程含源码)
What should a novice pay attention to when looking for an escort
First knowledge of batch processing
[cloud native] those lines of code that look awesome but have a very simple principle