python交换机ssh巡检_python自动巡检H3C交换机

1.通过netmiko模块登录交换机,协议ssh,执行收集信息命令,保存至txt文件

2.过滤txt文件中的内容,保存到excel,使用xlwt模块实现。

3.sendmai发送excel邮件。或者发送给钉钉机器人也可以

4.使用crond定时发送巡检报表。

#!/usr/bin/python3

#H3c交换机

import time

from netmiko import ConnectHandler

now = time.strftime("%Y%m%d",time.localtime(time.time()))

log_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())

ip_list = [

['sw-001','192.168.1.1'],

['sw-002','192.168.1.2'],

]

SW = {

'device_type':'hp_comware',

'username':'admin',

'ip':'',

'password':"$password"

}

for ip_item in ip_list:

SW['ip'] = ip_item[1]

connect = ConnectHandler(**SW)

print(log_time + 'Successfully connected to ' + ip_item[0])

output = connect.send_command('system view')

iproute = connect.send_command("display ip routing-table")

version = connect.send_command('dis version')

fan = connect.send_command('display fan')

cpu = connect.send_command('display cpu-usage')

mem = connect.send_command('display memory')

env = connect.send_command('display environment')

fo = open('xusj','w')

fo.write(iproute)

fo.write(fan)

fo.write(cpu)

fo.write(mem)

fo.write(env)

fo.close()

好的,这里是一个基于 Python自动备份华为、H3C交换机配置的代码示例: ```python import paramiko import time # 配置交换机信息 device_ip = '192.168.1.1' # 交换机 IP 地址 device_username = 'admin' # 交换机用户名 device_password = 'password' # 交换机密码 device_type = 'huawei' # 交换机类型,可选 huawei 和 h3c # 连接交换机 ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=device_ip, username=device_username, password=device_password) # 登录交换机 if device_type == 'huawei': ssh_shell = ssh.invoke_shell() ssh_shell.send('screen-length 0 temporary\n') time.sleep(1) ssh_shell.send('sys\n') time.sleep(1) ssh_shell.send('user-interface vty 0 4\n') time.sleep(1) ssh_shell.send('user privilege level 15\n') time.sleep(1) ssh_shell.send('quit\n') time.sleep(1) ssh_shell.send('quit\n') time.sleep(1) else: ssh_shell = ssh.invoke_shell() ssh_shell.send('screen-length 0 temporary\n') time.sleep(1) ssh_shell.send('system-view\n') time.sleep(1) ssh_shell.send('user-interface vty 0 4\n') time.sleep(1) ssh_shell.send('user privilege level 3\n') time.sleep(1) ssh_shell.send('quit\n') time.sleep(1) ssh_shell.send('quit\n') time.sleep(1) # 进入系统视图 if device_type == 'huawei': ssh_shell.send('sys\n') time.sleep(1) else: ssh_shell.send('system-view\n') time.sleep(1) # 备份配置 if device_type == 'huawei': ssh_shell.send('save configuration to tftp 192.168.1.2 huawei.cfg\n') time.sleep(5) ssh_shell.send('y\n') time.sleep(5) else: ssh_shell.send('save configuration to tftp 192.168.1.2 h3c.cfg\n') time.sleep(5) ssh_shell.send('y\n') time.sleep(5) # 关闭连接 ssh_shell.close() ssh.close() ``` 说明: 1. 首先设置交换机的 IP 地址、用户名、密码和类型。 2. 使用 Paramiko 库连接到交换机,并登录到交换机。 3. 根据交换机类型,进入相应的系统视图,备份配置。 4. 关闭连接。 在代码中,备份配置是通过将配置文件保存到 TFTP 服务器上来实现的,你需要将 `save configuration to tftp` 命令中的 IP 地址改为你的 TFTP 服务器 IP 地址,将 `huawei.cfg` 或 `h3c.cfg` 改为你想要保存的配置文件名。同时,由于备份配置需要一定时间,代码中使用了 `time.sleep()` 方法来暂停执行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值