运维必备!用Python管理多个Linux服务器的连接与命令执行
引言:
代码实现多个Linux服务器的SSH连接,支持添加、删除连接,执行命令,并返回标准输出。可以在需要时批量处理多个服务器,避免了手动逐个登录和执行命令的繁琐过程。
代码封装如下:
# -*- coding: utf-8 -*-
import paramiko # 导入paramiko库,主要用于SSH连接到远程Linux服务器
class MultiLinuxConnection:
def __init__(self):
"""
初始化MultiLinuxConnection类,设置连接的列表。
"""
self.connections = [] # 初始化一个空的列表,用于存储所有的SSH连接对象
def add_connection(self, host, port, username, password):
"""
添加Linux服务器连接,通过SSH进行连接,并将连接对象保存到列表中。
:param host: 目标服务器的IP地址
:param port: SSH端口,默认为22
:param username: 登录的用户名
:param password: 登录的密码
"""
connection = paramiko.SSHClient() # 创建SSHClient对象,用于建立SSH连接
connection.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 忽略目标服务器的未知主机密钥
connection.connect(host, port=port, username=username, password=password, look_for_keys=False) # 连接到目标服务器
self.connections.append(connection) # 将连接对象添加到连接列表中
def remove_connection(self, index):
"""
移除指定索引处的Linux服务器连接,并关闭该连接。
:param index: 要移除的连接的索引
"""
if index < len(self.connections): # 确保索引有效
connection = self.connections.pop(index) # 从连接列表中移除该连接
connection.close() # 关闭该连接
def execute_command(self, index, command):
"""
在指定的Linux服务器上执行命令,并返回输出结果。
:param index: 连接列表中的索引
:param command: 要执行的命令
:return: 命令执行后的输出结果
"""
if index < len(self.connections): # 确保索引有效
connection = self.connections[index] # 获取指定索引的连接对象
stdin, stdout, stderr = connection.exec_command(command) # 执行命令
output = stdout.read().decode().strip() # 获取标准输出,解码为字符串并去除两端空格
return output # 返回命令执行的输出结果
def execute_command_all(self, command):
"""
在所有已连接的Linux服务器上执行命令,并返回每个服务器的输出结果。
:param command: 要执行的命令
:return: 包含所有连接输出结果的字典,key为连接索引,value为命令输出
"""
output_dict = {} # 用字典保存每个服务器的命令输出
for index, connection in enumerate(self.connections): # 遍历所有连接
output_dict[index] = self.execute_command(index, command) # 执行命令并保存输出
return output_dict # 返回输出字典
def close_all_connections(self):
"""
关闭所有Linux服务器的SSH连接。
"""
for connection in self.connections: # 遍历所有连接
connection.close() # 关闭每一个连接
self.connections = [] # 清空连接列表
# 以下部分用于测试代码,实际应用时可以注释掉
if __name__ == '__main__':
# 初始化MultiLinuxConnection对象
li = MultiLinuxConnection()
# 添加多个Linux服务器的连接
li.add_connection('127.0.0.1', 22, 'root', 'password')
li.add_connection('127.0.0.2', 22, 'root', 'password')
# 在第一个连接的服务器上执行"ifconfig"命令并输出结果
output1 = li.execute_command(0, "ifconfig")
print(output1) # 打印第一个服务器的命令输出
# 可选:在所有连接的服务器上执行"df -h"命令并输出结果
# output2 = li.execute_command_all("df -h")
# print(output2) # 打印所有服务器的命令输出
# 移除第二个连接
# li.remove_connection(1)
# 关闭所有连接
li.close_all_connections()
注意:
密钥认证:在实际的应用场景中,建议使用SSH密钥认证而非用户名/密码认证。可以提高安全性,避免暴露密码。
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 会自动添加主机密钥,可能会有安全风险
并发执行:命令多个服务器同时执行命令时,可能会遇到性能瓶颈,尤其是当命令执行较长时间时。可以考虑使用多线程或异步编程来提高执行效率(大家自己尝试下)
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=10) as executor:
futures = [executor.submit(self.run_command_on_server, ip, port, username, password, command) for ip in self.connections]
for future in futures:
result = future.result()
print(result)