华为路由器常用的Python脚本(网工自动化脚本编写)

2023年 7月 30日 135.2k 0

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

  • 华为交换机常用的Python脚本

大家反响不错,后期我会陆续出一下思科、H3C、锐捷等厂商的脚本,前期会分享简单的,单一的脚本,后面会分享复杂的脚本。

图片[1]-华为路由器常用的Python脚本(网工自动化脚本编写)-不念博客

今天给大家分享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"quitn")

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-viewn")
tn.write(b"interface GigabitEthernet0/0/1n")
tn.write(b"ip address 192.168.2.1 255.255.255.0n")
tn.write(b"quitn")

tn.write(b"quitn")

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-viewn")
tn.write(b"snmp-agent sys-info version alln")
tn.write(b"snmp-agent community read publicn")
tn.write(b"snmp-agent target-host trap address udp-domain 192.168.2.2 params securityname publicn")
tn.write(b"quitn")

tn.write(b"quitn")

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-viewn")

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

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

tn.write(b"quitn")

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-viewn")
tn.write(b"ip route-static 192.168.3.0 255.255.255.0 192.168.2.2n")
tn.write(b"quitn")

tn.write(b"quitn")

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-viewn")
tn.write(b"interface GigabitEthernet0/0/0n")
tn.write(b"nat enablen")
tn.write(b"quitn")

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

tn.write(b"quitn")

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-viewn")
tn.write(b"dhcp enablen")
tn.write(b"interface GigabitEthernet0/0/1n")
tn.write(b"dhcp server excluded-ip-address 192.168.2.1n")
tn.write(b"dhcp server pool 1n")
tn.write(b"network 192.168.2.0 mask 255.255.255.0n")
tn.write(b"gateway-list 192.168.2.1n")
tn.write(b"dns-list 8.8.8.8 8.8.4.4n")
tn.write(b"quitn")

tn.write(b"quitn")

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

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论