思科(Cisco)和华三(H3C)交换机的Python巡检脚本

1. 安装依赖

 pip install paramiko

2. Python脚本

import paramiko
import time

# 设备列表,包含IP地址、用户名、密码、设备类型(cisco或h3c)
devices = [
{'ip': '192.168.1.1', 'username': 'admin', 'password': 'cisco123', 'type': 'cisco'},
{'ip': '192.168.1.2', 'username': 'admin', 'password': 'h3c123', 'type': 'h3c'},
# 添加更多设备
]

# 定义命令字典,根据设备类型执行不同的命令
commands = {
'cisco': {
'cpu': 'show processes cpu',
'memory': 'show processes memory',
'logs': 'show logging',
'version': 'show version',
'interfaces': 'show ip interface brief',
'hardware': 'show environment'
},
'h3c': {
'cpu': 'display cpu-usage',
'memory': 'display memory',
'logs': 'display logbuffer',
'version': 'display version',
'interfaces': 'display interface brief',
'hardware': 'display device'
}
}

def ssh_connect(ip, username, password):
"""通过SSH连接到设备"""
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(ip, username=username, password=password)
return client

def execute_command(client, command):
"""执行命令并返回输出"""
stdin, stdout, stderr = client.exec_command(command)
output = stdout.read().decode('utf-8')
return output

def check_device(device):
"""检查设备状态"""
ip = device['ip']
username = device['username']
password = device['password']
device_type = device['type']

print(f"正在检查设备: {ip} ({device_type})")

try:
client = ssh_connect(ip, username, password)

# 检查CPU使用率
cpu_command = commands[device_type]['cpu']
cpu_output = execute_command(client, cpu_command)
print(f"CPU使用率:\n{cpu_output}")

# 检查内存使用率
memory_command = commands[device_type]['memory']
memory_output = execute_command(client, memory_command)
print(f"内存使用率:\n{memory_output}")

# 检查系统日志
logs_command = commands[device_type]['logs']
logs_output = execute_command(client, logs_command)
print(f"系统日志:\n{logs_output}")

# 检查系统版本
version_command = commands[device_type]['version']
version_output = execute_command(client, version_command)
print(f"系统版本:\n{version_output}")

# 检查接口状态
interfaces_command = commands[device_type]['interfaces']
interfaces_output = execute_command(client, interfaces_command)
print(f"接口状态:\n{interfaces_output}")

# 检查硬件状态(电源、风扇)
hardware_command = commands[device_type]['hardware']
hardware_output = execute_command(client, hardware_command)
print(f"硬件状态:\n{hardware_output}")

client.close()
except Exception as e:
print(f"连接设备 {ip} 失败: {e}")

def main():
"""主函数,遍历所有设备并检查"""
for device in devices:
check_device(device)
print("-" * 40) # 分隔线

if __name__ == "__main__":
main()

 

3. 脚本说明

  • 设备列表devices列表中包含了每个设备的IP地址、用户名、密码和设备类型(Cisco或H3C)。

  • 命令字典:commands字典根据设备类型定义了不同的命令。Cisco和H3C的命令有所不同,因此需要分别定义。
  • SSH连接:使用paramiko库通过SSH连接到设备。
  • 执行命令:通过SSH连接执行命令并获取输出。
  • 检查设备check_device函数负责检查设备的CPU、内存、日志、版本、接口状态和硬件状态。
  • 主函数main函数遍历所有设备并调用check_device函数进行检查。

4. 运行脚本

将脚本保存为switch_inspection.py,然后在终端中运行:

 
 python switch_inspection.py

5. 注意事项

  • 安全性:在生产环境中,建议使用更安全的方式存储和管理密码,如使用环境变量或密钥管理工具。

  • 错误处理:脚本中包含了基本的错误处理,但在生产环境中可能需要更详细的错误处理和日志记录。

  • 命令兼容性:确保命令适用于你的设备型号和操作系统版本。不同型号的交换机可能会有不同的命令。

这个脚本是一个基础版本,大家可以根据实际需求进行扩展和优化。