作者 | ssw 来源:Python 技术 新人入职,我们经常需要登记他的ip和MAC地址,为什么呢?因为行政MM经常来找我“打印机又出问题了”,作为一个桌面维护工程师,我知道打印机的ip是固定的,但员工修改自己的跟它一样引起冲突。这样的话,一不小心涉及到网络安全了,emm.. 2、查看录入情况 为方便查看,需要准备一个后台页面。员工提交一条数据,后台就能看到新用户。以新员工邢道荣为例,看看他的录入, 整个流程是这样的: linux执行arping命令,会返回对方的MAC。 我们需要找一台内网linux机器,用paramiko模块登录上去,让它替我们arping员工网页提交过来的ip 翻译成python: 办公网络分有线和无线,两者的ip网段不一样。员工如果通过无线访问这个页面,要提示他仅有线网络需要提交IP信息,无线网络无需提交,请不要使用代理访问本页面 换成python表示 根据员工网页提交的姓名,自动分配公司邮箱,格式为“名字拼音的简写+公司邮箱后缀”。 这里用到pypinyin模块 名字长度一般为2~4个汉字,解析成拼音后进行拼接,如'邢道荣'分配的邮箱为[email protected],'潘凤'为[email protected] 用requests提交员工信息到后台 登录思科交换机用到python的第三方模块ciscolib = ciscolib.Device('192.168.14.10', '123456') 这个可以在管理后台增加一个"保存"按钮,让它执行相关交换机命令。 绑定的相关命令如下: 这样就将交换机的FastEthernet0/17与192.168.14.6绑定了 包括html文件,已上传到 linux服务器上,http://ssw.fit/file/ 通过设计这样的网页办事窗口,员工入职从mac绑定到邮箱分配,再到其它信息登记,一条流水线服务,是不是规范和便捷呢,我的工作量也减少了,行政部的入职流程也更清晰了。 本文是由 Python技术 公众号粉丝 ssw 投稿,欢迎大家继续踊跃投稿!
思路
1、我们需要的信息有ip、MAC、姓名(用于生成公司邮箱)

具体操作
MAC地址怎么获取?
[root@vm3 ~]# arping -f 192.168.14.6 -I ens33|grep replyUnicast reply from192.168.14.6 [00:0E:C6:83:3B:F9] 1.007msdefget_mac(ip):importparamikoclient = paramiko.SSHClient()private_key = paramiko.RSAKey.from_private_key_file('C:/Users/0717/Documents/id_rsa')client.set_missing_host_key_policy(paramiko.AutoAddPolicy())client.connect(hostname='192.168.14.173',username='root',port=22,pkey=private_key,)stdin,stdout, stderr = client.exec_command('arping -f %s -I ens33|grep reply' % ip)msg = stdout.read().decode('utf-8')client.close()returnmsg检测ip
defcheck_ip(addr):#只匹配有线网络的网段 v = re.compile('(192.168.14).(\d+)')return v.match(addr)defrecord(request): ip = request.META.get('REMOTE_ADDR')if check_ip(ip):return render(request, 'ipinfo.html', {'ip':ip})#check_mac根据re.compile('.*(\w{2}:\w+:\w+:\w+:\w+:\w+).*')进行正则匹配ifnot check_mac(ip): ip = ip + '检测到MAC地址异常,请联系管理员'return render(request, 'ipinfo.html', {'ip': ip})else: ip = ip + '仅有线网络需要提交IP信息,无线网络无需提交,请不要使用代理访问本页面'return render(request, 'ipinfo.html', {'ip': ip})根据姓名分配邮箱
#安装命令,pip install pypinyinfrom pypinyin import lazy_pinyinprint(lazy_pinyin('上将潘凤'))['shang', 'jiang', 'pan', 'feng']deftest1(name_list): name_list = lazy_pinyin(name_list)if len(name_list) == 2: email_name = name_list[0] + name_list[1]elif len(name_list) == 3: email_name = name_list[0] + name_list[1][0] + name_list[2][0]elif len(name_list) == 4: email_name = name_list[0] + name_list[1] + name_list[2][0] + name_list[3][0] email = email_name + '@163.com' print(email)test1('邢道荣')[email protected]test1('潘凤')[email protected]提交数据
user_info = {'username': name, 'password': user_id, 'email': email, 'ip': ip, 'MAC': mac}conn = requests.session()ret = conn.post('http://127.0.0.1:8887/api/v1/users/', data=json.dumps(user_info),
交换机绑定MAC
switchswitch.connect()switch.enable(password='BxAdmin')switch.cmd("write")cisco(config)#show mac-address-tablecisco(config)#arp 192.168.14.6 0000.e268.9980ARPAdefget_mac(ip):importparamikoclient = paramiko.SSHClient()private_key = paramiko.RSAKey.from_private_key_file('C:/Users/0717/Documents/id_rsa')client.set_missing_host_key_policy(paramiko.AutoAddPolicy())client.connect(hostname='192.168.14.173',username='root',port=22,pkey=private_key,)stdin,stdout, stderr = client.exec_command('arping -f %s -I ens33|grep reply' % ip)msg = stdout.read().decode('utf-8')client.close()returnmsg0代码下载地址

小结
正文
我用django偷偷绑定员工MAC信息
此篇文章发布距今已超过1027天,您需要注意文章的内容或图片是否可用!









还没有评论,来说两句吧...