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

【网络】每天掌握一个Linux命令 - iftop

在Linux系统中&#xff0c;iftop是网络管理的得力助手&#xff0c;能实时监控网络流量、连接情况等&#xff0c;帮助排查网络异常。接下来从多方面详细介绍它。 目录 【网络】每天掌握一个Linux命令 - iftop工具概述安装方式核心功能基础用法进阶操作实战案例面试题场景生产场景…...

uni-app学习笔记二十二---使用vite.config.js全局导入常用依赖

在前面的练习中&#xff0c;每个页面需要使用ref&#xff0c;onShow等生命周期钩子函数时都需要像下面这样导入 import {onMounted, ref} from "vue" 如果不想每个页面都导入&#xff0c;需要使用node.js命令npm安装unplugin-auto-import npm install unplugin-au…...

《Playwright:微软的自动化测试工具详解》

Playwright 简介:声明内容来自网络&#xff0c;将内容拼接整理出来的文档 Playwright 是微软开发的自动化测试工具&#xff0c;支持 Chrome、Firefox、Safari 等主流浏览器&#xff0c;提供多语言 API&#xff08;Python、JavaScript、Java、.NET&#xff09;。它的特点包括&a…...

UDP(Echoserver)

网络命令 Ping 命令 检测网络是否连通 使用方法: ping -c 次数 网址ping -c 3 www.baidu.comnetstat 命令 netstat 是一个用来查看网络状态的重要工具. 语法&#xff1a;netstat [选项] 功能&#xff1a;查看网络状态 常用选项&#xff1a; n 拒绝显示别名&#…...

2024年赣州旅游投资集团社会招聘笔试真

2024年赣州旅游投资集团社会招聘笔试真 题 ( 满 分 1 0 0 分 时 间 1 2 0 分 钟 ) 一、单选题(每题只有一个正确答案,答错、不答或多答均不得分) 1.纪要的特点不包括()。 A.概括重点 B.指导传达 C. 客观纪实 D.有言必录 【答案】: D 2.1864年,()预言了电磁波的存在,并指出…...

基于Uniapp开发HarmonyOS 5.0旅游应用技术实践

一、技术选型背景 1.跨平台优势 Uniapp采用Vue.js框架&#xff0c;支持"一次开发&#xff0c;多端部署"&#xff0c;可同步生成HarmonyOS、iOS、Android等多平台应用。 2.鸿蒙特性融合 HarmonyOS 5.0的分布式能力与原子化服务&#xff0c;为旅游应用带来&#xf…...

论文解读:交大港大上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化学习框架(一)

宇树机器人多姿态起立控制强化学习框架论文解析 论文解读&#xff1a;交大&港大&上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化学习框架&#xff08;一&#xff09; 论文解读&#xff1a;交大&港大&上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化…...

智能仓储的未来:自动化、AI与数据分析如何重塑物流中心

当仓库学会“思考”&#xff0c;物流的终极形态正在诞生 想象这样的场景&#xff1a; 凌晨3点&#xff0c;某物流中心灯火通明却空无一人。AGV机器人集群根据实时订单动态规划路径&#xff1b;AI视觉系统在0.1秒内扫描包裹信息&#xff1b;数字孪生平台正模拟次日峰值流量压力…...

稳定币的深度剖析与展望

一、引言 在当今数字化浪潮席卷全球的时代&#xff0c;加密货币作为一种新兴的金融现象&#xff0c;正以前所未有的速度改变着我们对传统货币和金融体系的认知。然而&#xff0c;加密货币市场的高度波动性却成为了其广泛应用和普及的一大障碍。在这样的背景下&#xff0c;稳定…...

打手机检测算法AI智能分析网关V4守护公共/工业/医疗等多场景安全应用

一、方案背景​ 在现代生产与生活场景中&#xff0c;如工厂高危作业区、医院手术室、公共场景等&#xff0c;人员违规打手机的行为潜藏着巨大风险。传统依靠人工巡查的监管方式&#xff0c;存在效率低、覆盖面不足、判断主观性强等问题&#xff0c;难以满足对人员打手机行为精…...