python-华为路由器交换机批量处理ip与mac绑定

  • 以下为代码
  • 运行后会产生日志文件

以下为代码

#-*- coding: utf-8 -*-
#!/usr/bin/python
#仅供相互学习
#本次脚本测试环境为华为路由器
#一、通过dis arp命令查看arp表
#二、把一行ip与mac存为一个字典,再把字典存在列表里
#三、因为192.168.0.0/16网段为管理接口网段不需要绑定所以过滤掉
#四、mac地址有重复的字典会被过滤掉
#五、ip-mac-bind.xls存放需要操作的设备ipimport paramiko
import threading
import time
import os
import xlrd
import re
#这里引入了第三方模块,cmd运行pip install 相关模块即可#定义连接与操作
def ssh2(ip,username,passwd,cmd):try:ssh = paramiko.SSHClient()ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())ssh.connect(ip,22,username,passwd,timeout=5)ssh_shell = ssh.invoke_shell()for m in cmd:res = ssh_shell.sendall(m)time.sleep(float(1))print (ssh_shell.recv(1024))ssh.close()except :print ('%s\tError\n'%(ip))#定义读取excel函数
def read_excel_data():filename = 'ip-mac-bind.xls'data = xlrd.open_workbook(filename)table = data.sheet_by_name('Sheet1')row_num = table.nrows  # 行数# col_num = table.ncols  # 列数datas=[]for i in range(row_num):datas.append(table.row_values(i)[0])return datasdef sshSwitch(ip,username,passwd):try:ssh = paramiko.SSHClient()ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())ssh.connect(ip,22,username,passwd,timeout=5)ssh_shell = ssh.invoke_shell()ssh_shell.send('user-interface vty 0 4 \n')ssh_shell.send('screen-length 0 \n')ssh_shell.send('dis arp \n')# time.sleep(float(5))index=0contentArr=[]while True:index+=1   stdout=ssh_shell.recv(9999) decon=str(stdout)contentArr.append(decon)#当屏幕输入Total时候停止if 'Total' in decon:break;finalResult=''.join(contentArr)matchResult= re.findall('(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).+?([a-z\d]{4}-[a-z\d]{4}-[a-zd]{4})',finalResult)#正则匹配ip 与 macif matchResult:macCount=dict()# print(matchResult)for bind in matchResult:if bind[0][0:7] != '192.168':#过滤192.168.0.0/16网段mac = bind[1]macCount[mac]=mac not in macCount# 简化下面4行代码,这里过滤重复的mac地址# if mac in macCount:#     macCount[mac]=False# else:#     macCount[mac]=Truefor bind in matchResult:mac = bind[1]# print(bind)if bind[0][0:7] != '192.168' and macCount[mac]: #这里排除192.168.0.0/24地址的IP地址绑定    #这里匹配到IP mac并且拼接好了命令docmd=(f' user-bind static ip-address {bind[0]} mac-address {bind[1]}')print(docmd)ssh_shell.send('end \n')ssh_shell.send('system-view \n')##################################################################################以下为执行操作的代码,执行前先打印结果。确认后再执行。################################################################################ssh_shell.send(f'{docmd} \n')# print(docmd)with open("ip-mac-bind-dolog.txt","a+") as f:f.write(f'连接{ip}操作命令{docmd} \n')time.sleep(float(0.5))ssh_shell.send('end \n')ssh_shell.send('sys \n')ssh_shell.send('user-interface vty 0 4 \n')ssh_shell.send('undo screen-length  \n')ssh_shell.send('end \n')ssh_shell.send('save \n')ssh_shell.send('y \n')with open("ip-mac-bind-dolog.txt","a+") as f:f.write('运行结束 结束时间:')f.write(str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))+'\n')print ('%s执行完成'%(ip))ssh.close()except Exception as e :print(e)with open("ip-mac-bindlog.txt","a+") as f:f.write(f'{ip} 连接异常'+'\n')print ('\n'+'%s连接异常'%(ip))#设置交换机SSH用户名密码及线程
username = "cc"  #用户名
passwd = "cc123"    #密码
threads = 3  #多线程if __name__=='__main__':print ("Begin......")swip =read_excel_data()thread_list=[]count1=0count2=0#print (swip[0])while True:ip=swip[count2]a=threading.Thread(target=sshSwitch,args=(ip,username,passwd))thread_list.append(a)a.start()print(ip+'执行')count2+=1if count2%threads==0:print('线程超过'+str(threads)+',等待资源')  while True:thread_list[count1].join()count1+=1if count1==count2:print('资源释放完毕')  breakif count2>=len(swip):time.sleep(10)print(('执行结束'))with open("ip-mac-bindlog.txt","a+") as f:f.write('本次运行结束 结束时间:')f.write(str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))+'\n')break

运行后会产生日志文件

python-华为路由器交换机批量处理ip与mac绑定相关推荐

  1. Cisco交换机IP和MAC绑定设置方案

    目前,很多单位的内部网络,都采用了MAC地址与IP地址的绑定技术.下面我们就针对Cisco的交换机介绍一下IP和MAC绑定的设置方案. 在Cisco中有以下三种方案可供选择,方案1和方案2实现的功能是 ...

  2. 【收藏】华为路由器交换机配置命令大全

    华为路由器交换机配置命令:计算机命令 PCAlogin:root;使用root用户 password:linux;口令是linux #shutdown-hnow;关机 #init0;关机 #logou ...

  3. 常毅 华为计算机,华为路由器交换机配置.docx

    华为路由器交换机VLAN 配置 华为路由器交换机 VLAN 配置 使用 4 台 PC(pc 多和少,原理是一样的,所以这里我只用了4 台 pc) ,华为路由 器 (R2621) .交换机 (S3026 ...

  4. 华为路由器交换机配置命令

    华为路由器交换机配置命令:计算机命令 PCAlogin:root:使用root用户 password:linux:口令是linux shutdown-hnow:关机 init0:关机 logout:用 ...

  5. 华为路由器/交换机配置telnet,ssh远程登录

    华为路由器/交换机配置telnet,ssh远程登录 文章目录 华为路由器/交换机配置telnet,ssh远程登录 1. Telnet 1.1 新建账号 1.2 配置远程访问 1.3 客户端远程访问 1 ...

  6. 限制计算机通讯端口mac绑定,h3c交换机ip和mac绑定配置教程

    h3c交换机ip和mac绑定配置教程 首先登录交换机,进入管理状态System-View 第一种情况:1个端口只有一台电脑如何绑定 如:某台电脑的IP:10.119.100.1 MAC:00-1A-4 ...

  7. 华为路由器交换机eNSP配置命令

    华为交换机基础配置命令参考 交换机可以隔离冲突域,路由器可以隔离广播域,这两种设备在企业网络中应用越来越广泛.随着越来越多的终端接入到网络中,网络设备的负担也越来越重,这时网络设备可以通过华为专有的V ...

  8. 华为路由器怎么查看设备本身的MAC地址

    华为路由器的 MAC 地址可以在管理界面中查看.具体步骤如下: 打开浏览器,输入路由器的 IP 地址,默认为 192.168.1.1 输入用户名和密码,进入路由器管理界面 在菜单栏中找到 " ...

  9. IP与MAC绑定的难题

    问:我的计算机原来采用公网固定IP地址.为了避免被他人盗用,使用"arp -s ip mac"命令对MAC地址和IP地址进行了绑定.后来,由于某种原因,又使用"arp - ...

最新文章

  1. Android -- Annotation(注解)原理详解及常见框架应用
  2. Java开发中的23种设计模式详解(转)
  3. html之marquee详解[转]
  4. python是用c写的吗-python和c语言的区别是什么
  5. 某机器字长8位,试用如下所给芯片设计一个存储器,容量为10KW,其中RAM为高8KW,ROM为低2KW,最低地址为0(RAM芯片类型为:4K×8。ROM芯片为:2K×4)。
  6. c++矩阵连乘的动态规划算法并输出_「Javascript算法设计」× 动态规划与回溯算法...
  7. 百面机器学习 #3 经典算法:01-1 线性可分(硬间隔)支撑向量机SVM
  8. 张珺 2015/07/13 个人文档
  9. GTJ2018如何导出全部工程量_新清单计量规范征求意见稿第二期来啦!来看看那些让你烦恼的操作如何解决...
  10. 3DMine V202006体验版本下载
  11. 洛谷P3097 - [USACO13DEC]最优挤奶Optimal Milking
  12. 傻瓜攻略(十六)——MATLAB实现txt文件复杂内容的读取
  13. 用命令提示符打开资源管理器目录
  14. SpringBoot+MyBatis+MySql实现的医院管理系统
  15. 人机大战5个误区:人类没那么差阿尔法狗不会防水
  16. 量子运算-比算子描述更广泛的一类刻画量子态在客观世界演化的数学工具
  17. 需求分析报告应该包含哪些部分_2020最新抖音用户画像分析报告:粉丝都有哪些特点和需求?...
  18. 51单片机基础之蜂鸣器
  19. c++函数返回一个数组
  20. [年终总结]一年一年又一年

热门文章

  1. linux进阶52——pthread_cond_t
  2. Android学习-使用WebView在app上显示网页
  3. PostgreSQL 11 与 pgAdmin4 在 Windows 平台上的安装和使用
  4. html浏览器兼容moz,webkit_css3中-moz、-ms、-webkit各什么意思
  5. 华三模拟器之完成IPv6 to IPv4隧道实验
  6. Android 扫码盒子全局接收付款码(全局事件上层接收多重下发)
  7. Python教你18个高效编程的方法
  8. 爱了!这18个 Python 高效编程技巧真香!
  9. 保留至百位并且向上取整 例如:125631 >>125700
  10. autocad java api_在java中实现 AutoCAD api