当前位置: 首页 > 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开发实战技巧…...

springBoot自动配置过程介绍

什么是自动配置 以前整合spring mybatis框架时候&#xff0c;需要加很多的bean, 比如说sqlSessionFactory等等 现在springboot帮我们干了&#xff0c;我们只需要引入对应的starter就可以了。 springBoot可以帮我们配置好了一些bean. 如mysql, mogondb相关操作等等&#xff…...

PostgreSQL最后的救命稻草 — pg_resetwal

pg_resetwal— 重置 PostgreSQL 数据库集群的预写日志和其他控制信息 适用版本&#xff1a;PostgreSQL 12/13/14/15语法 pg_resetwal [ -f | --force ] [ -n | --dry-run ] [option...] [ -D | --pgdata ]datadir描述pg_resetwal清除预写日志 WAL&#xff0c;并可选地重置pg_c…...

彻底关闭Windows更新

一、关闭Windows Update服务 1、按“Windows R”键&#xff0c;打开运行对话框&#xff0c;并输入“services.msc”&#xff0c;然后再单击“确定”。 2、在弹出的服务窗口中&#xff0c;找到“Windows Update”选项并双击打开它。 3、在弹出的“Windows Update的属性”对话框…...

Java正则表达式语法

Java正则表达式的语法与示例 | |目录 1匹配验证-验证Email是否正确 2在字符串中查询字符或者字符串 3常用正则表达式 4正则表达式语法 1匹配验证-验证Email是否正确 public static void main(String[] args) { // 要验证的字符串 String str "servicexsoftlab.net&q…...

【2023-3-29】JavaScript使用promise顺序调用函数并抛出异常

JavaScript使用promise顺序调用函数并抛出异常 场景 新建或者编辑时&#xff0c;一个页面中存在多个表单&#xff0c;每个表单都有单独进行表单验证。点击提交时&#xff0c;若有一个表单校验失败&#xff0c;则不能提交。 ps&#xff1a;为啥不放在一个表单中&#xff1f; (…...

Python实现GWO智能灰狼优化算法优化随机森林分类模型(RandomForestClassifier算法)项目实战

说明&#xff1a;这是一个机器学习实战项目&#xff08;附带数据代码文档视频讲解&#xff09;&#xff0c;如需数据代码文档视频讲解可以直接到文章最后获取。 1.项目背景 灰狼优化算法(GWO)&#xff0c;由澳大利亚格里菲斯大学学者 Mirjalili 等人于2014年提出来的一种群智能…...

从redis到epoll到mmap

redis为什么这么快&#xff1f; 比较容易答出的答案 1)纯粹的内存操作 2)单线程操作,不用考虑线程切换 其他优势 3)I/O 多路复用,使用epoll 4)Reactor 设计模式 I/O 多路复用有三种 select、poll、epoll select&#xff1a;使用数组存储轮询 poll&#xff1a;使用链表轮询 epo…...

STM32CubeMX快速构造工程模板(一)

STM32CubeMX作为一个免费开源的软件,能够可视化配置STM32或其他产品硬件资源,能过快速地构造工程模板,很是方便!!! 目录 STM32CubeMX快速构造工程模板 首先第一步,打开软件-点击按钮-输入型号-双击打开。...

Java Web中的ServletContext对象

目录 ServletContext对象 获取上下文初始化参数的相关方法 创建ServletContext对象 1&#xff09;通过 GenericServlet 提供的 getServletContext() 方法 2&#xff09;通过 ServletConfig 提供的 getServletContext() 方法 3&#xff09;通过 HttpSession 提供的 getServletCo…...

回归预测 | MATLAB实现PSO-RF粒子群算法优化随机森林多输入单输出回归预测

回归预测 | MATLAB实现PSO-RF粒子群算法优化随机森林多输入单输出回归预测 目录回归预测 | MATLAB实现PSO-RF粒子群算法优化随机森林多输入单输出回归预测效果一览基本介绍程序设计参考资料效果一览 基本介绍 MATLAB实现PSO-RF粒子群算法优化随机森林多输入单输出回归预测 粒子…...