Using Python for Operations Automation: Remote Execution, Log Parsing, Monitoring, Deployment, and Backup
This article demonstrates how Python can automate common operations tasks such as remote command execution, log file parsing, system monitoring with alerts, batch software deployment, and file backup and recovery, providing code examples using libraries like paramiko, regex, psutil, fabric, and shutil.
Python is a versatile language with rich third‑party libraries that can greatly improve operational efficiency by automating routine tasks and reducing human error.
1. Connect to remote servers and execute commands – The paramiko library enables SSH connections and command execution.
import paramiko
# 创建 SSH 客户端
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='remote.test.com', username='user', password='password')
stdin, stdout, stderr = ssh.exec_command('ls -l /tmp')2. Parse log files and extract useful information – The regex library can be used to match patterns in log files.
import regex
# 读取日志文件
with open('log.txt', 'r') as f:
log = f.read()
# 使用正则表达式匹配错误信息
errors = regex.findall(r'ERROR:\s+(.*)', log)
# 打印出所有匹配到的错误信息
for error in errors:
print(error)3. Monitor system status and send alerts – The psutil library provides system metrics, and smtplib can send email alerts.
import psutil
import smtplib
# 获取 CPU 使用率
cpu_percent = psutil.cpu_percent()
# 判断 CPU 使用率是否超过阈值
if cpu_percent > 80:
server = smtplib.SMTP('smtp.example.com')
server.login('user', 'password')
message = 'CPU 使用率超过 80%:当前使用率为 {}%'.format(cpu_percent)
subject = '警报:高 CPU 使用率'
server.sendmail('[email protected]', '[email protected]', subject, message)
server.quit()4. Batch deploy software or update systems – The fabric library allows executing commands on multiple hosts.
from fabric import task
@task
def update_system(c):
c.run('apt-get update')5. Perform backup and recovery tasks – The shutil library can copy files and directories.
import shutil
# 备份文件
shutil.copy('/path/to/file', '/path/to/backup/file')
# 备份目录
shutil.copytree('/path/to/dir', '/path/to)')Beyond these examples, Python can also be used for automated testing with pytest or selenium, further extending its usefulness in operations.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
