免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3649 | 回复: 0

无线AP检测状态是否在线 [复制链接]

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2018-07-05 14:02 |显示全部楼层
本帖最后由 wangyanjie_001 于 2018-07-05 14:04 编辑

由于工作需要,编写了一个自动检查办公区无线AP是否掉线的python脚本,我这里用的是python3环境,请大家注意
还有要注意的是我这里用的是锐捷的无线AC及无线AP。其它品牌只需要替换相关命令即可,就是脚本内容的中的command内容更改成你的品牌无线AC命令即可。
下面是实际脚本内容:
#!/usr/local/python3/bin/python3
import telnetlib,time,os
def do_telnet(Host,password,finish,commands):
    import telnetlib
    '''''Telnet远程登录:Windows客户端连接Linux服务器'''

    # 连接Telnet服务器
    tn = telnetlib.Telnet(Host, port=23)
    #tn.set_debuglevel(2)#开启telnet调试模式

    # 输入登录用户名
    # tn.read_until(b'Password: ')
    # tn.write(password + b'\n')

    # 输入登录密码
    tn.read_until(b'Password:')
    tn.write(password + b'\n')

    # 登录完毕后执行命令
    tn.read_until(finish)
    tn.write(commands1 + b'\r\n')
    time.sleep(1)
    tn.read_until(b'Password:')
    tn.write(password + b'\n')
    time.sleep(1)
    tn.read_until(b'#')
    tn.write(commands + b'\n')
    time.sleep(10)  # 这里一定要等待10秒,因为你write命令以后,会等待很长时间。
    # 执行完毕后,终止Telnet连接(或输入exit退出)
    tn.write(b'exit\n')
    result = tn.read_all()
    file_object = open('/opt/scripts/networkresult.txt', 'wb')
    file_object.write(result)
    file_object.close()
    tn.close()
if __name__ == '__main__':
    # 配置选项
    Host = '192.168.4.5'  # Telnet服务器IP
    # username = 'xxxx'.encode(encoding='utf-8')  # 登录用户名
    password = 'Yhcs@123'.encode(encoding='utf-8')  # 登录密码
    finish = '>'.encode(encoding='utf-8')  # 命令提示符
    commands = 'show web-api ibeacon'.encode(encoding='utf-8')
    commands1 = 'en'.encode(encoding='utf-8')
    do_telnet(Host,password,finish,commands)
#判断AP是否在线
with open('/opt/scripts/network/result.txt','r') as f:
    for i in f:
        if 'code' in i:
            #把ac@exit替换成空的并转换成字典
            i = eval(i.replace('ac#exit',''))
            aplist = i['data']['list']
            day = time.strftime('%Y-%m-%d', time.localtime())
            time = time.strftime('%H:%M:%S', time.localtime())
            for j in aplist:
                if j['status'] != 'run':
                    ip = j['ip']
                    mac = j['mac']
                    name = j['name']
                    status = j['status']
                    with open('satus.txt','a') as f1:
                         message = '当前时间:' + day + ' ' + time + ',' + '无线AP的ip:' + ip + ',' + '当前状态是不在线,' + '无线AP的mac地址:' + mac + ',' + '无线AP名称是:' + name + ',' + '无线AP状态:不在线'
                         f1.write(message + '\n')
                         #发送微信报警
                         os.system('./weixin.py wangyanjie_001 wangyang 望京无线AP名称:%s不在线,请检查!' %name)
                else:
                    print('无线AP状态正常!')
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP