昨天给大家介绍了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脚本可以帮助你快速查看华为路由器的各种信息,也可以作为开发的基础,进行更多高级的操作和自动化任务。
© 版权声明
本站文章由不念博客原创,未经允许严禁转载!
THE END