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脚本可以帮助你快速查看华为路由器的各种信息,也可以作为开发的基础,进行更多高级的操作和自动化任务。

目录
相关文章
|
14天前
|
Java 测试技术 持续交付
【入门思路】基于Python+Unittest+Appium+Excel+BeautifulReport的App/移动端UI自动化测试框架搭建思路
本文重点讲解如何搭建App自动化测试框架的思路,而非完整源码。主要内容包括实现目的、框架设计、环境依赖和框架的主要组成部分。适用于初学者,旨在帮助其快速掌握App自动化测试的基本技能。文中详细介绍了从需求分析到技术栈选择,再到具体模块的封装与实现,包括登录、截图、日志、测试报告和邮件服务等。同时提供了运行效果的展示,便于理解和实践。
52 4
【入门思路】基于Python+Unittest+Appium+Excel+BeautifulReport的App/移动端UI自动化测试框架搭建思路
|
4天前
|
开发者 Python
使用Python实现自动化邮件通知:当长时程序运行结束时
本文介绍了如何使用Python实现自动化邮件通知功能,当长时间运行的程序完成后自动发送邮件通知。主要内容包括:项目背景、设置SMTP服务、编写邮件发送函数、连接SMTP服务器、发送邮件及异常处理等步骤。通过这些步骤,可以有效提高工作效率,避免长时间等待程序结果。
40 9
|
4天前
|
运维 监控 网络安全
自动化运维的崛起:如何利用Python脚本简化日常任务
【10月更文挑战第43天】在数字化时代的浪潮中,运维工作已从繁琐的手工操作转变为高效的自动化流程。本文将引导您了解如何运用Python编写脚本,以实现日常运维任务的自动化,从而提升工作效率和准确性。我们将通过一个实际案例,展示如何使用Python来自动部署应用、监控服务器状态并生成报告。文章不仅适合运维新手入门,也能为有经验的运维工程师提供新的视角和灵感。
|
9天前
|
存储 Python
Python自动化脚本编写指南
【10月更文挑战第38天】本文旨在为初学者提供一条清晰的路径,通过Python实现日常任务的自动化。我们将从基础语法讲起,逐步引导读者理解如何将代码块组合成有效脚本,并探讨常见错误及调试技巧。文章不仅涉及理论知识,还包括实际案例分析,帮助读者快速入门并提升编程能力。
35 2
|
11天前
|
运维 监控 Python
自动化运维:使用Python脚本简化日常任务
【10月更文挑战第36天】在数字化时代,运维工作的效率和准确性成为企业竞争力的关键。本文将介绍如何通过编写Python脚本来自动化日常的运维任务,不仅提高工作效率,还能降低人为错误的风险。从基础的文件操作到进阶的网络管理,我们将一步步展示Python在自动化运维中的应用,并分享实用的代码示例,帮助读者快速掌握自动化运维的核心技能。
27 3
|
10天前
|
数据采集 IDE 测试技术
Python实现自动化办公:从基础到实践###
【10月更文挑战第21天】 本文将探讨如何利用Python编程语言实现自动化办公,从基础概念到实际操作,涵盖常用库、脚本编写技巧及实战案例。通过本文,读者将掌握使用Python提升工作效率的方法,减少重复性劳动,提高工作质量。 ###
24 1
|
16天前
|
缓存 运维 NoSQL
python常见运维脚本_Python运维常用脚本
python常见运维脚本_Python运维常用脚本
20 3
|
16天前
|
数据采集 JSON 数据安全/隐私保护
Python常用脚本集锦
Python常用脚本集锦
16 2
|
15天前
|
Web App开发 测试技术 数据安全/隐私保护
自动化测试的魔法:使用Python进行Web应用测试
【10月更文挑战第32天】本文将带你走进自动化测试的世界,通过Python和Selenium库的力量,展示如何轻松对Web应用进行自动化测试。我们将一起探索编写简单而强大的测试脚本的秘诀,并理解如何利用这些脚本来确保我们的软件质量。无论你是测试新手还是希望提升自动化测试技能的开发者,这篇文章都将为你打开一扇门,让你看到自动化测试不仅可行,而且充满乐趣。
|
1月前
|
机器学习/深度学习 人工智能 运维
构建高效运维体系:从自动化到智能化的演进
本文探讨了如何通过自动化和智能化手段,提升IT运维效率与质量。首先介绍了自动化在简化操作、减少错误中的作用;然后阐述了智能化技术如AI在预测故障、优化资源中的应用;最后讨论了如何构建一个既自动化又智能的运维体系,以实现高效、稳定和安全的IT环境。
65 4
下一篇
无影云桌面