导入所需模块

from ncclient import manager import paramiko import time import datetime

定义设备IP地址、用户名和密码

device_ip = '192.168.1.1' device_username = 'admin' device_password = 'admin123'

定义时间戳

timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

定义保存结果的文件名

result_file = 'result.txt'

定义要执行的命令列表

commands = ['display memory', 'display ospf peer br']

连接设备并执行命令

with manager.connect(host=device_ip, port=830, username=device_username, password=device_password, hostkey_verify=False) as m: for command in commands: # 发送命令 result = m.get(('subtree', f'{command}')) # 提取结果 output = result.data_xml # 写入结果到文件 with open(result_file, 'a') as f: f.write(f'

{timestamp}

') f.write(f'{command}

') f.write(f'{output}

')

读取结果文件并输出

with open(result_file, 'r') as f: print(f.read())

华为设备命令执行脚本 - 使用Python、NetConf和Paramiko

原文地址: https://www.cveoy.top/t/topic/n39B 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录