20个华为路由器常用的Python脚本,网工写自动化脚本时候可以参考!

简介: 20个华为路由器常用的Python脚本,网工写自动化脚本时候可以参考!

你好,这里是网络技术联盟站。

昨天给大家介绍了10个华为交换机的Python脚本:

大家反响不错,后期我会陆续出一下思科、H3C、锐捷等厂商的脚本,前期会分享简单的,单一的脚本,后面会分享复杂的脚本,大家及时给公众号加星标⭐哦,这样每天都能及时接收到公众号文章!

今天给大家分享20个常用的Python脚本,用于控制和管理华为路由器:

1、登录和退出路由器:

import telnetlib

HOST = "192.168.1.1"
user = "admin"
password = "admin"

tn = telnetlib.Telnet(HOST)

tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")

tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")

tn.write(b"quit\n")

2、获取路由器的系统信息:

import paramiko

host = "192.168.1.1"
port = 22
username = "admin"
password = "admin"

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=host, port=port, username=username, password=password)

stdin, stdout, stderr = client.exec_command("display version")

for line in stdout:
    print(line.strip())

client.close()

3、配置路由器端口:

import telnetlib

HOST = "192.168.1.1"
user = "admin"
password = "admin"

tn = telnetlib.Telnet(HOST)

tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")

tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")

tn.write(b"system-view\n")
tn.write(b"interface GigabitEthernet0/0/1\n")
tn.write(b"ip address 192.168.2.1 255.255.255.0\n")
tn.write(b"quit\n")

tn.write(b"quit\n")

4、查看路由器端口状态:

import paramiko

host = "192.168.1.1"
port = 22
username = "admin"
password = "admin"

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=host, port=port, username=username, password=password)

stdin, stdout, stderr = client.exec_command("display interface GigabitEthernet0/0/1")

for line in stdout:
    print(line.strip())

client.close()

5、配置路由器的SNMP:

import telnetlib

HOST = "192.168.1.1"
user = "admin"
password = "admin"

tn = telnetlib.Telnet(HOST)

tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")

tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")

tn.write(b"system-view\n")
tn.write(b"snmp-agent sys-info version all\n")
tn.write(b"snmp-agent community read public\n")
tn.write(b"snmp-agent target-host trap address udp-domain 192.168.2.2 params securityname public\n")
tn.write(b"quit\n")

tn.write(b"quit\n")

6、配置路由器的ACL:

import telnetlib

HOST = "192.168.1.1"
user = "admin"
password = "admin"

tn = telnetlib.Telnet(HOST)

tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")

tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")

tn.write(b"system-view\n")

tn.write(b"acl number 2000\n")
tn.write(b"rule 5 permit source 192.168.2.0 0.0.0.255\n")
tn.write(b"quit\n")

tn.write(b"interface GigabitEthernet0/0/1\n")
tn.write(b"ip address 192.168.2.1 255.255.255.0\n")
tn.write(b"traffic-filter inbound acl 2000\n")
tn.write(b"quit\n")

tn.write(b"quit\n")

7、配置路由器的静态路由:

import telnetlib

HOST = "192.168.1.1"
user = "admin"
password = "admin"

tn = telnetlib.Telnet(HOST)

tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")

tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")

tn.write(b"system-view\n")
tn.write(b"ip route-static 192.168.3.0 255.255.255.0 192.168.2.2\n")
tn.write(b"quit\n")

tn.write(b"quit\n")

8、配置路由器的NAT:

import telnetlib

HOST = "192.168.1.1"
user = "admin"
password = "admin"

tn = telnetlib.Telnet(HOST)

tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")

tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")

tn.write(b"system-view\n")
tn.write(b"interface GigabitEthernet0/0/0\n")
tn.write(b"nat enable\n")
tn.write(b"quit\n")

tn.write(b"nat address-group 1 192.168.2.0 0.0.0.255\n")
tn.write(b"nat server protocol tcp global 202.96.209.5 inside 192.168.2.100 80\n")
tn.write(b"quit\n")

tn.write(b"quit\n")

9、配置路由器的DHCP:

import telnetlib

HOST = "192.168.1.1"
user = "admin"
password = "admin"

tn = telnetlib.Telnet(HOST)

tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")

tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")

tn.write(b"system-view\n")
tn.write(b"dhcp enable\n")
tn.write(b"interface GigabitEthernet0/0/1\n")
tn.write(b"dhcp server excluded-ip-address 192.168.2.1\n")
tn.write(b"dhcp server pool 1\n")
tn.write(b"network 192.168.2.0 mask 255.255.255.0\n")
tn.write(b"gateway-list 192.168.2.1\n")
tn.write(b"dns-list 8.8.8.8 8.8.4.4\n")
tn.write(b"quit\n")

tn.write(b"quit\n")

10、查看路由器的ARP表:

import paramiko

host = "192.168.1.1"
port = 22
username = "admin"
password = "admin"

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=host, port=port, username=username, password=password)

stdin, stdout, stderr = client.exec_command("display arp")
arp_table = stdout.readlines()

for line in arp_table:
    print(line.strip())

client.close()

11、查看路由器的MAC地址表:

import paramiko

host = "192.168.1.1"
port = 22
username = "admin"
password = "admin"

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=host, port=port, username=username, password=password)

stdin, stdout, stderr = client.exec_command("display mac-address")
mac_table = stdout.readlines()

for line in mac_table:
    print(line.strip())

client.close()

12、查看路由器的路由表:

import paramiko

host = "192.168.1.1"
port = 22
username = "admin"
password = "admin"

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=host, port=port, username=username, password=password)

stdin, stdout, stderr = client.exec_command("display ip routing-table")
route_table = stdout.readlines()

for line in route_table:
    print(line.strip())

client.close()

13、查看路由器的接口状态:

import paramiko

host = "192.168.1.1"
port = 22
username = "admin"
password = "admin"

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=host, port=port, username=username, password=password)

stdin, stdout, stderr = client.exec_command("display interface brief")
interface_table = stdout.readlines()

for line in interface_table:
    print(line.strip())

client.close()

14、查看路由器的系统资源使用情况:

import paramiko

host = "192.168.1.1"
port = 22
username = "admin"
password = "admin"

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=host, port=port, username=username, password=password)

stdin, stdout, stderr = client.exec_command("display system resource")
resource_info = stdout.readlines()

for line in resource_info:
    print(line.strip())

client.close()

15、查看路由器的系统版本:

import paramiko

host = "192.168.1.1"
port = 22
username = "admin"
password = "admin"

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=host, port=port, username=username, password=password)

stdin, stdout, stderr = client.exec_command("display version")
version_info = stdout.readlines()

for line in version_info:
    print(line.strip())

client.close()

16、查看路由器的运行时间:

import paramiko

host = "192.168.1.1"
port = 22
username = "admin"
password = "admin"

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=host, port=port, username=username, password=password)

stdin, stdout, stderr = client.exec_command("display clock")
clock_info = stdout.readlines()

for line in clock_info:
    print(line.strip())

client.close()

17、查看路由器的接口详细信息:

import paramiko

host = "192.168.1.1"
port = 22
username = "admin"
password = "admin"

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=host, port=port, username=username, password=password)

stdin, stdout, stderr = client.exec_command("display interface")
interface_info = stdout.readlines()

for line in interface_info:
    print(line.strip())

client.close()

18、查看路由器的NAT转换表:

import paramiko

host = "192.168.1.1"
port = 22
username = "admin"
password = "admin"

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=host, port=port, username=username, password=password)

stdin, stdout, stderr = client.exec_command("display nat session table")
nat_table = stdout.readlines()

for line in nat_table:
    print(line.strip())

client.close()

19、查看路由器的DNS服务器列表:

import paramiko

host = "192.168.1.1"
port = 22
username = "admin"
password = "admin"

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=host, port=port, username=username, password=password)

stdin, stdout, stderr = client.exec_command("display dns server")
dns_info = stdout.readlines()

for line in dns_info:
    print(line.strip())

client.close()

20、查看路由器的DHCP服务器配置:

import paramiko

host = "192.168.1.1"
port = 22
username = "admin"
password = "admin"

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=host, port=port, username=username, password=password)

stdin, stdout, stderr = client.exec_command("display dhcp server configuration")
dhcp_info = stdout.readlines()

for line in dhcp_info:
    print(line.strip())

client.close()

这些Python脚本可以帮助你快速查看华为路由器的各种信息,也可以作为开发的基础,进行更多高级的操作和自动化任务。

目录
相关文章
|
29天前
|
搜索推荐 Python
使用Python自动化生成物业通知单
本文介绍如何使用Python结合Pandas和python-docx库自动化生成物业通知单。通过读取Excel数据并填充至Word模板,实现高效准确的通知单批量制作。包括环境准备、代码解析及效果展示,适用于物业管理场景。
62 14
|
1月前
|
数据采集 数据可视化 数据挖掘
利用Python自动化处理Excel数据:从基础到进阶####
本文旨在为读者提供一个全面的指南,通过Python编程语言实现Excel数据的自动化处理。无论你是初学者还是有经验的开发者,本文都将帮助你掌握Pandas和openpyxl这两个强大的库,从而提升数据处理的效率和准确性。我们将从环境设置开始,逐步深入到数据读取、清洗、分析和可视化等各个环节,最终实现一个实际的自动化项目案例。 ####
105 10
|
7天前
|
Python
自动化微信朋友圈:Python脚本实现自动发布动态
本文介绍如何使用Python脚本自动化发布微信朋友圈动态,节省手动输入的时间。主要依赖`pyautogui`、`time`、`pyperclip`等库,通过模拟鼠标和键盘操作实现自动发布。代码涵盖打开微信、定位朋友圈、准备输入框、模拟打字等功能。虽然该方法能提高效率,但需注意可能违反微信使用条款,存在风险。定期更新脚本以适应微信界面变化也很重要。
106 60
|
27天前
|
数据采集 存储 监控
21个Python脚本自动执行日常任务(2)
21个Python脚本自动执行日常任务(2)
86 7
21个Python脚本自动执行日常任务(2)
|
2天前
|
Web App开发 数据采集 JavaScript
Chrome浏览器实例的TypeScript自动化脚本
Chrome浏览器实例的TypeScript自动化脚本
|
17天前
|
数据挖掘 vr&ar C++
让UE自动运行Python脚本:实现与实例解析
本文介绍如何配置Unreal Engine(UE)以自动运行Python脚本,提高开发效率。通过安装Python、配置UE环境及使用第三方插件,实现Python与UE的集成。结合蓝图和C++示例,展示自动化任务处理、关卡生成及数据分析等应用场景。
77 5
|
19天前
|
JSON 数据可视化 测试技术
python+requests接口自动化框架的实现
通过以上步骤,我们构建了一个基本的Python+Requests接口自动化测试框架。这个框架具有良好的扩展性,可以根据实际需求进行功能扩展和优化。它不仅能提高测试效率,还能保证接口的稳定性和可靠性,为软件质量提供有力保障。
51 7
|
1月前
|
运维 Kubernetes Devops
自动化运维:从脚本到工具的演进之旅
在数字化浪潮中,自动化运维成为提升效率、保障系统稳定的关键。本文将探索自动化运维的发展脉络,从基础的Shell脚本编写到复杂的自动化工具应用,揭示这一技术变革如何重塑IT运维领域。我们将通过实际案例,展示自动化运维在简化工作流程、提高响应速度和降低人为错误中的重要作用。无论你是初学者还是资深专家,这篇文章都将为你提供宝贵的洞见和实用的技巧。
|
1月前
|
安全 API 文件存储
Yagmail邮件发送库:如何用Python实现自动化邮件营销?
本文详细介绍了如何使用Yagmail库实现自动化邮件营销。Yagmail是一个简洁强大的Python库,能简化邮件发送流程,支持文本、HTML邮件及附件发送,适用于数字营销场景。文章涵盖了Yagmail的基本使用、高级功能、案例分析及最佳实践,帮助读者轻松上手。
35 4
|
1月前
|
敏捷开发 测试技术 持续交付
自动化测试之美:从零开始搭建你的Python测试框架
在软件开发的马拉松赛道上,自动化测试是那个能让你保持节奏、避免跌宕起伏的神奇小助手。本文将带你走进自动化测试的世界,用Python这把钥匙,解锁高效、可靠的测试框架之门。你将学会如何步步为营,构建属于自己的测试庇护所,让代码质量成为晨跑时清新的空气,而不是雾霾中的忧虑。让我们一起摆脱手动测试的繁琐枷锁,拥抱自动化带来的自由吧!