“一些使用 Python 进行自动化运维的示例脚本”的版本间的差异

来自linux中国网wiki
跳到导航 跳到搜索
 
(未显示同一用户的1个中间版本)
第40行: 第40行:
 
# 关闭连接
 
# 关闭连接
 
ssh.close()
 
ssh.close()
 +
 +
 +
#using priky
 +
➜  py cat pp.py
 +
import paramiko
 +
client =  paramiko.SSHClient()
 +
#client = paramiko.SSHClient()
 +
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 +
client.connect('192.168.10.5',22,username='evan', key_filename='/home/evan/.ssh/id_rsa')
 +
stdin, stdout, stderr = client.exec_command('ls -l')
 +
print(stdout.read().decode())
 +
client.close()
 +
 +
 +
➜  py py3 pp.py
 +
total 24704
 +
-rw-r--r--  1 evan evan      125 Dec 30  2018 1
 +
drwxr-xr-x  2 evan evan      4096 Jul  1  2019 11
 +
-rw-r--r--  1 evan evan      1143 Jan 26  2021 #1.txt#
 +
-rw-r--r--  1 evan evan      1133 Jan 26  2021 1.txt~
 +
-rw-r--r--  1 evan evan        84 Feb 13  2019 du
 +
drwxr-xr-x  3 evan evan      4096 Oct 15  2018 github
 +
-rwxr-xr-x  1 evan evan  12517376 Aug 28  2023 gost-linux-armv6
 +
 +
 +
 +
# 上传文件到远程服务器
 +
import paramiko
 +
 +
def depoly_m(ip):
 +
    with paramiko.SSHClient() as client:
 +
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 +
        client.connect(ip,22,username='evan', key_filename='/home/evan/.ssh/id_rsa')
 +
 +
        stdin, stdout, stderr = client.exec_command('ls -l')
 +
        print(stdout.readlines())
 +
 +
        with client.open_sftp() as sftp:
 +
            sftp.put('/home/evan/data/tmp/py/pscp.py', '/tmp/pscp.py')
 +
            # //sftp.chmod(path, mode, *, dir_fd=None, follow_symlinks=True)
 +
 +
 +
 +
 +
def main():
 +
    with  open('hosts') as f:
 +
        for line in f:
 +
            depoly_m(line.strip())
 +
 +
 +
 +
if __name__ == '__main__':
 +
    main()
 +
  
 
</pre>
 
</pre>

2024年10月19日 (六) 09:39的最新版本


一、文件备份


python
复制

import shutil
import datetime

source_directory = '/path/to/source'
backup_directory = '/path/to/backup'

timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
backup_filename = f'backup_{timestamp}.zip'

shutil.make_archive(f'{backup_directory}/{backup_filename}', 'zip', source_directory)

print(f'Backup completed: {backup_filename}')

远程服务器管理(使用 Paramiko 库)

import paramiko

# 创建 SSH 对象
ssh = paramiko.SSHClient()

# 允许连接不在 know_hosts 文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# 连接服务器
ssh.connect(hostname='remote_server_ip', port=22, username='your_username', password='your_password')

# 执行命令
stdin, stdout, stderr = ssh.exec_command('ls -l')
print(stdout.read().decode())

# 关闭连接
ssh.close()


#using priky 
➜  py cat pp.py 
import paramiko
client =  paramiko.SSHClient()
#client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('192.168.10.5',22,username='evan', key_filename='/home/evan/.ssh/id_rsa')
stdin, stdout, stderr = client.exec_command('ls -l')
print(stdout.read().decode())
client.close()


➜  py py3 pp.py
total 24704
-rw-r--r--  1 evan evan       125 Dec 30  2018 1
drwxr-xr-x  2 evan evan      4096 Jul  1  2019 11
-rw-r--r--  1 evan evan      1143 Jan 26  2021 #1.txt#
-rw-r--r--  1 evan evan      1133 Jan 26  2021 1.txt~
-rw-r--r--  1 evan evan        84 Feb 13  2019 du
drwxr-xr-x  3 evan evan      4096 Oct 15  2018 github
-rwxr-xr-x  1 evan evan  12517376 Aug 28  2023 gost-linux-armv6



# 上传文件到远程服务器
import paramiko

def depoly_m(ip):
    with paramiko.SSHClient() as client:
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(ip,22,username='evan', key_filename='/home/evan/.ssh/id_rsa')

        stdin, stdout, stderr = client.exec_command('ls -l')
        print(stdout.readlines())

        with client.open_sftp() as sftp:
            sftp.put('/home/evan/data/tmp/py/pscp.py', '/tmp/pscp.py')
            # //sftp.chmod(path, mode, *, dir_fd=None, follow_symlinks=True)




def main():
    with  open('hosts') as f:
        for line in f:
            depoly_m(line.strip())



if __name__ == '__main__':
    main()


trouble

➜  py py3  paramiko.py
Traceback (most recent call last):
  File "/home/evan/data/tmp/py/paramiko.py", line 1, in <module>
    import paramiko
  File "/home/evan/data/tmp/py/paramiko.py", line 2, in <module>
    client = paramiko.SSHClient()
             ^^^^^^^^^^^^^^^^^^
AttributeError: partially initialized module 'paramiko' has no attribute 'SSHClient' (most likely due to a circular import)


哈哈 笑死了 名字问题
If you have nay file in your local system by the name of "paramiko". The code is going to import that paramiko instead of the python paramiko. You need to rename the file simply and the error will be gone.

https://stackoverflow.com/questions/25409977/paramiko-module-object-has-no-attribute-error-sshclient

监控服务器资源(使用 psutil 库)

import psutil

# 获取 CPU 使用率
cpu_percent = psutil.cpu_percent(interval=1)
print(f'CPU usage: {cpu_percent}%')

# 获取内存使用情况
memory = psutil.virtual_memory()
print(f'Memory usage: {memory.used / memory.total * 100}%')

# 获取磁盘空间
disk = psutil.disk_usage('/')
print(f'Disk usage: {disk.used / disk.total * 100}%')

自动部署应用(假设使用 Flask 应用)

import os
import shutil

# 源应用目录
app_source_directory = '/path/to/app_source'

# 目标服务器目录
server_directory = '/path/to/server'

# 复制应用文件到服务器目录
shutil.copytree(app_source_directory, f'{server_directory}/app')

# 在服务器上安装依赖
os.system(f'cd {server_directory}/app && pip install -r requirements.txt')

# 启动应用
os.system(f'cd {server_directory}/app && python app.py &')

批量文件重命名

import os

directory = '/path/to/directory'

for filename in os.listdir(directory):
    if filename.startswith('old_prefix_'):
        new_filename = filename.replace('old_prefix_', 'new_prefix_')
        os.rename(os.path.join(directory, filename), os.path.join(directory, new_filename))

1. 远程服务器登录和命令执行 (使用 Paramiko)

Paramiko 是一个用于通过 SSH 协议与远程服务器进行交互的库。
import paramiko

def execute_remote_command(hostname, username, password, command):
    try:
        # 创建 SSH 客户端
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        
        # 连接到远程主机
        ssh.connect(hostname, username=username, password=password)
        
        # 执行命令
        stdin, stdout, stderr = ssh.exec_command(command)
        print(f"Output: {stdout.read().decode()}")
        print(f"Error: {stderr.read().decode()}")
        
        ssh.close()
    except Exception as e:
        print(f"An error occurred: {e}")

# 示例调用
execute_remote_command('192.168.1.100', 'admin', 'password123', 'ls -l')

2. 监控磁盘空间 (使用 psutil 库)

psutil 是一个跨平台库,用于系统和进程监控,特别适用于磁盘、CPU、内存等资源的监控。
import psutil

def check_disk_usage(threshold=80):
    disk_usage = psutil.disk_usage('/')
    usage_percent = disk_usage.percent
    if usage_percent > threshold:
        print(f"警告:磁盘使用率已达到 {usage_percent}%")
    else:
        print(f"磁盘使用正常:使用率为 {usage_percent}%")

# 示例调用
check_disk_usage()

3. 自动化日志轮替

此脚本可以自动将日志文件备份并清空原文件,用于日志轮替的简单管理。
import os
import shutil
import time

def rotate_logs(log_file):
    if os.path.exists(log_file):
        # 生成备份文件名
        backup_file = log_file + '.' + time.strftime('%Y%m%d%H%M%S') + '.bak'
        shutil.copy(log_file, backup_file)
        
        # 清空原日志文件
        open(log_file, 'w').close()
        print(f"日志文件已备份至 {backup_file},并已清空原文件。")
    else:
        print("日志文件不存在!")

# 示例调用
rotate_logs('/var/log/my_app.log')

网络监控:检测主机是否在线 (使用 ping)

这个脚本用来检测一系列主机是否在线,并记录结果。
import os

def ping_host(hostname):
    response = os.system(f"ping -c 1 {hostname}")
    if response == 0:
        print(f"{hostname} is up")
    else:
        print(f"{hostname} is down")

# 示例调用
hosts = ['192.168.1.1', '192.168.1.2', 'google.com']
for host in hosts:
    ping_host(host)

5. 自动化备份数据库 (MySQL)

import os
import time

def backup_mysql_db(user, password, db_name, backup_dir):
    if not os.path.exists(backup_dir):
        os.makedirs(backup_dir)

    backup_file = os.path.join(backup_dir, f"{db_name}_{time.strftime('%Y%m%d%H%M%S')}.sql")
    dump_command = f"mysqldump -u {user} -p{password} {db_name} > {backup_file}"

    os.system(dump_command)
    print(f"数据库备份完成:{backup_file}")

# 示例调用
backup_mysql_db('root', 'password123', 'my_database', '/backup')

6. 重启服务 (使用 subprocess 库)

import subprocess

def restart_service(service_name):
    try:
        subprocess.run(['systemctl', 'restart', service_name], check=True)
        print(f"{service_name} 服务已重启")
    except subprocess.CalledProcessError as e:
        print(f"重启 {service_name} 失败: {e}")

# 示例调用
restart_service('nginx')

7. 自动化服务器健康检查

这个脚本综合了 CPU 使用率、内存使用情况和磁盘使用情况,检查服务器的健康状态。
import psutil

def check_system_health(cpu_threshold=85, memory_threshold=80, disk_threshold=80):
    # 检查 CPU 使用率
    cpu_usage = psutil.cpu_percent(interval=1)
    if cpu_usage > cpu_threshold:
        print(f"CPU 使用率过高:{cpu_usage}%")
    
    # 检查内存使用情况
    memory_info = psutil.virtual_memory()
    if memory_info.percent > memory_threshold:
        print(f"内存使用率过高:{memory_info.percent}%")
    
    # 检查磁盘使用情况
    disk_info = psutil.disk_usage('/')
    if disk_info.percent > disk_threshold:
        print(f"磁盘使用率过高:{disk_info.percent}%")
    
    if cpu_usage <= cpu_threshold and memory_info.percent <= memory_threshold and disk_info.percent <= disk_threshold:
        print("系统状态正常")

# 示例调用
check_system_health()


分享一些使用 Python 进行系统配置管理的示例脚本