通过SSH连接华为交换机/路由器,自动执行状态检查、告警查询、安全风险排查,生成巡检报告。
使用pexpect建立交互式SSH连接(华为设备直接SSH会被断开):
python
import pexpect
def connect_device(host, username, password):
child = pexpect.spawn(f'ssh -o StrictHostKeyChecking=no {username}@{host}', timeout=60)
child.expect(['password:', 'Password:'], timeout=10)
child.sendline(password)
child.expect(['>', '#'], timeout=15)
return child
按顺序执行以下命令收集信息:
命令用途display version系统版本、运行时间display device硬件状态display cpu-usageCPU使用率display memory-usage内存使用率display alarm active活动告警display temperature all温度状态display power电源状态display fan风扇状态display anti-attack statistics攻击防御统计
重点关注以下告警类型:
告警名称级别说明hwSysSecureRiskWarningWarning安全风险警告hwGtlDefaultValueMajorLicense未激活hwLdtPortLoopDetectWarning端口环路检测linkDownCritical接口断开
报告包含:
markdown
## 华为 [型号] 巡检报告
### 基本信息
| 项目 | 状态 |
|------|------|
| 型号 | xxx |
| 系统版本 | xxx |
| 运行时间 | xxx |
### 硬件状态
| 组件 | 状态 |
|------|------|
| 主控板 | Normal |
| 电源 | Normal |
| 风扇 | Normal |
| 温度 | xx°C |
### 资源使用
| 指标 | 当前值 | 状态 |
|------|--------|------|
| CPU | xx% | ✅/⚠️ |
| 内存 | xx% | ✅/⚠️ |
### 活动告警
| 级别 | 告警 | 说明 |
|------|------|------|
| Warning | xxx | xxx |
### 问题总结
1. xxx
2. xxx
---- More ----),需要发送空格继续