当前位置: 首页 > news >正文

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

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

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

  • 10个华为华为交换机常用的Python脚本,网络工程师收藏!

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

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

1、登录和退出路由器:

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

相关文章:

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

你好,这里是网络技术联盟站。 昨天给大家介绍了10个华为交换机的Python脚本: 10个华为华为交换机常用的Python脚本,网络工程师收藏! 大家反响不错,后期我会陆续出一下思科、H3C、锐捷等厂商的脚本,前期会…...

【kubernetes云原生】k8s资源管理命令与Namespace使用详解

目录 一、前言 二、k8s概述 三、k8s常用操作管理命令 3.1 kubectl 命令用法 3.2 常用控制台管理命令演示 3.2.1 获取全部节点信息 3.2.2 获取当前集群下全部pod 3.2.3 查看某个pod信息 3.2.4 获取当前集群下的所有namespace信息 3.2.5 查看当前集群下已创建的资源 3…...

String源码深度刨析

前言 我们将从源码角度深度分析特点,来提升对他们的了解以及设计。 String、StringBuilder、StringBuffer的常见面试题及四大区别可以参考:String、StringBuilder、StringBuffer的四大区别解析 String public final class Stringimplements java.io.Se…...

FreeRTOS - 消息队列

一.消息队列的概念及应用消息队列(queue):可以在任务与任务间、中断和任务间传递消息,实现任务接收来自其他任务或中断的不固定的消息1.1任务需求1、使用消息队列检测串口输入2、通过串口发送字符串openled1,openled2&…...

怎样正确做 Web 应用的压力测试?

环境 首先环境是非常重要的,需要尽可能跟生产环境靠近。 比方说,使用同样的nginx版本,php的话需要启用fpm,zend-optimizer等等,参数配置也最好跟生产环境保持一致。 当然,php的版本更加需要保持一致&#x…...

php mysql大学生求职招聘资源信息网zkfdzkf67a8

1.系统登录:系统登录是用户访问系统的路口,设计了系统登录界面,包括用户名、密码和验证码,然后对登录进来的用户判断身份信息,判断是管理员用户还是普通用户。 2.系统用户管理:不管是…...

2023上海市“星光计划”职业院校技能大赛 网络安全竞赛试题任务书

2023上海市“星光计划”职业院校技能大赛 网络安全竞赛试题任务书 A模块基础设施设置/安全加固(200分) 一、项目和任务描述: 假定你是某企业的网络安全工程师,对于企业的服务器系统,根据任务要求确保各服务正常运行&…...

Spring事务源码:创建代理类

参考文章: 《Spring事务源码解析之tx:annotation-driven标签解析》 《Spring 源码解析—事务执行》 参考资料: 《Spring AOP源码:开启注解读取》 《Spring AOP源码2:查找增强器》 《Spring AOP源码3:实现代理》 …...

java14 使用增强的模式匹配切换表达式

野旷天低树,江清月近人。——唐代杜甫《月夜忆舍弟》 使用增强的模式匹配切换表达式(Switch Expressions with Enhanced Pattern Matching) Java 14中引入的“Switch Expressions with Enhanced Pattern Matching”这个功能。 这个功能可以让我们在使用switch cas…...

python【正则表达式】

正则表达式 1.正则的作用 正则表达式式一种可以让复杂的字符串变得简单的工具。 写正则表达式的时候就是用正则符号来描述字符串规则。 2.正则语法 需要导入模块 from re import fullmatch, findall, search2.1.第一类:匹配类符号 1)普通字符—在…...

Ubuntu常见系统问题解决方式

Ubuntu常见系统问题解决方式Ubuntu每次开机后提示检测到系统程序出现问题的解决方法Ubuntu循环登陆问题问题描述原因解决方法文件夹打开缓慢Ubuntu启动后GUI界面卡住不动Ubuntu18.04使用过程中常遇到的问题Ubuntu每次开机后提示检测到系统程序出现问题的解决方法 首先&#xf…...

C/C++中的虚拟内存

文章目录一、虚拟内存二、C中的虚拟内存分配模型三、C中的虚拟内存分配模型四、堆区和栈区的区别一、虚拟内存 虚拟内存是一种实现在计算机软硬件之间的内存管理技术,它将程序使用到的内存地址(虚拟地址)映射到计算机内存中的物理地址&#…...

Qt C++与Python混合编程:补充错误

在提示中,需要引用Python.h,出现错误。 1、找不到Python.h 如果是pro工程,需要在里面配置; INCLUDEPATH /Users/xinnianwang/opt/anaconda3/include LIBS /Users/xinnianwang/opt/anaconda3/lib 如果是CMakeLists.txt需要配…...

2023-04-01:当Go语言遇见FFmpeg视频解码器,使用Go语言改写decode_video.c文件,提升视频解码效率与开发体验。

2023-04-01:当Go语言遇见FFmpeg视频解码器,使用Go语言改写decode_video.c文件,提升视频解码效率与开发体验。 答案2023-04-01: 步骤如下: 1.导入必要的依赖库,包括 fmt、os、unsafe 和其它 FFmpeg 库相关…...

Solidity 学习笔记

主要参考网上资料学习,个人学习笔记有删改,参考出处在文末列出。 0 基础 IDE: remixType Bool: bool public _bool true; 默认false;整型:int、uint、uint256,默认0;地址类型:address,分为 payable 和普…...

ThreadLocal原理

关键点总结: ThreadLocal更像是对其他类型变量的一层包装,通过ThreadLocal的包装使得该变量可以在线程之间隔离和当前线程全局共享。在Thread中有一个threadLocals变量,类型为ThreadLocal.ThreadLocalMap,ThreadLocalMap中key是Th…...

串操作指令详解 MOVS,LODS,STOS,CMPS,SCAS,REP

指令包括:MOVS,LODS,STOS,CMPS,SCAS,REP 串的概念:串是连续存放再内存中的字节块或字块。每个串有一个起始地址和长度, 待操作的数据串称为源串,目的地址称为目标串 目录…...

Java实现判断素数

1 问题 判断101-200之间有多少个素数&#xff0c;并输出所有素数。 2 方法 package homework04; public class Test05 { public static void main(String[] args) { for (int i 101; i < 201; i) { boolean flag true; for (int j 2; j…...

PHP初级教程------------------(2)

目录 运算符 赋值运算符 算术运算符 比较运算符 逻辑运算符 连接运算符 错误抑制符 三目运算符 自操作运算符 ​编辑 计算机码 位运算符 运算符优先级 流程控制 控制分类 顺序结构 分支结构 If分支 ​ Switch分支 循环结构 For循环 while循环 do-while循环 循环控制 ​ …...

【SQL开发实战技巧】系列(三十五):数仓报表场景☞根据条件返回不同列的数据以及Left /Full Join注意事项

系列文章目录 【SQL开发实战技巧】系列&#xff08;一&#xff09;:关于SQL不得不说的那些事 【SQL开发实战技巧】系列&#xff08;二&#xff09;&#xff1a;简单单表查询 【SQL开发实战技巧】系列&#xff08;三&#xff09;&#xff1a;SQL排序的那些事 【SQL开发实战技巧…...

铭豹扩展坞 USB转网口 突然无法识别解决方法

当 USB 转网口扩展坞在一台笔记本上无法识别,但在其他电脑上正常工作时,问题通常出在笔记本自身或其与扩展坞的兼容性上。以下是系统化的定位思路和排查步骤,帮助你快速找到故障原因: 背景: 一个M-pard(铭豹)扩展坞的网卡突然无法识别了,扩展出来的三个USB接口正常。…...

基于Flask实现的医疗保险欺诈识别监测模型

基于Flask实现的医疗保险欺诈识别监测模型 项目截图 项目简介 社会医疗保险是国家通过立法形式强制实施&#xff0c;由雇主和个人按一定比例缴纳保险费&#xff0c;建立社会医疗保险基金&#xff0c;支付雇员医疗费用的一种医疗保险制度&#xff0c; 它是促进社会文明和进步的…...

iPhone密码忘记了办?iPhoneUnlocker,iPhone解锁工具Aiseesoft iPhone Unlocker 高级注册版​分享

平时用 iPhone 的时候&#xff0c;难免会碰到解锁的麻烦事。比如密码忘了、人脸识别 / 指纹识别突然不灵&#xff0c;或者买了二手 iPhone 却被原来的 iCloud 账号锁住&#xff0c;这时候就需要靠谱的解锁工具来帮忙了。Aiseesoft iPhone Unlocker 就是专门解决这些问题的软件&…...

高频面试之3Zookeeper

高频面试之3Zookeeper 文章目录 高频面试之3Zookeeper3.1 常用命令3.2 选举机制3.3 Zookeeper符合法则中哪两个&#xff1f;3.4 Zookeeper脑裂3.5 Zookeeper用来干嘛了 3.1 常用命令 ls、get、create、delete、deleteall3.2 选举机制 半数机制&#xff08;过半机制&#xff0…...

大数据学习(132)-HIve数据分析

​​​​&#x1f34b;&#x1f34b;大数据学习&#x1f34b;&#x1f34b; &#x1f525;系列专栏&#xff1a; &#x1f451;哲学语录: 用力所能及&#xff0c;改变世界。 &#x1f496;如果觉得博主的文章还不错的话&#xff0c;请点赞&#x1f44d;收藏⭐️留言&#x1f4…...

USB Over IP专用硬件的5个特点

USB over IP技术通过将USB协议数据封装在标准TCP/IP网络数据包中&#xff0c;从根本上改变了USB连接。这允许客户端通过局域网或广域网远程访问和控制物理连接到服务器的USB设备&#xff08;如专用硬件设备&#xff09;&#xff0c;从而消除了直接物理连接的需要。USB over IP的…...

为什么要创建 Vue 实例

核心原因:Vue 需要一个「控制中心」来驱动整个应用 你可以把 Vue 实例想象成你应用的**「大脑」或「引擎」。它负责协调模板、数据、逻辑和行为,将它们变成一个活的、可交互的应用**。没有这个实例,你的代码只是一堆静态的 HTML、JavaScript 变量和函数,无法「活」起来。 …...

深入理解Optional:处理空指针异常

1. 使用Optional处理可能为空的集合 在Java开发中&#xff0c;集合判空是一个常见但容易出错的场景。传统方式虽然可行&#xff0c;但存在一些潜在问题&#xff1a; // 传统判空方式 if (!CollectionUtils.isEmpty(userInfoList)) {for (UserInfo userInfo : userInfoList) {…...

【SpringBoot自动化部署】

SpringBoot自动化部署方法 使用Jenkins进行持续集成与部署 Jenkins是最常用的自动化部署工具之一&#xff0c;能够实现代码拉取、构建、测试和部署的全流程自动化。 配置Jenkins任务时&#xff0c;需要添加Git仓库地址和凭证&#xff0c;设置构建触发器&#xff08;如GitHub…...

ubuntu22.04有线网络无法连接,图标也没了

今天突然无法有线网络无法连接任何设备&#xff0c;并且图标都没了 错误案例 往上一顿搜索&#xff0c;试了很多博客都不行&#xff0c;比如 Ubuntu22.04右上角网络图标消失 最后解决的办法 下载网卡驱动&#xff0c;重新安装 操作步骤 查看自己网卡的型号 lspci | gre…...