柳州建站公司,python怎么做网站,佛山网站推广怎么做,网站建设代码背景#xff1a;门禁机器使用的WiFi连接#xff0c;因为某些原因会不定期自动断开连接#xff0c;需要人工及时干预#xff0c;以免影响门禁数据同步#xff0c;故写此脚本#xff0c;定时检测门禁网络联通性。
#首次使用要安装tcping模块
pip install tcpingfrom tcpin…背景门禁机器使用的WiFi连接因为某些原因会不定期自动断开连接需要人工及时干预以免影响门禁数据同步故写此脚本定时检测门禁网络联通性。
#首次使用要安装tcping模块
pip install tcpingfrom tcping import Ping
import csv
from datetime import datetime
import requests, jsondef SendWeiXinWork(user,context):corpid填企业ID #企业IDappsecret填secret #secretagentid填AgentID #AgentID#获取accesstokentoken_urlhttps://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid corpid corpsecret appsecretreqrequests.get(token_url)accesstokenreq.json()[access_token]#发送消息msgsend_urlhttps://qyapi.weixin.qq.com/cgi-bin/message/send?access_token accesstokentouseruserparams{touser: touser,# toparty: toparty,msgtype: text,agentid: agentid,text: {content: context},safe:0}reqrequests.post(msgsend_url, datajson.dumps(params))def pingip(ipAddress,request_nums):ping ip:param ipAddress::param request_nums: 请求次数:return: 丢包率loss和统计结果resping Ping(ipAddress,3718,3)#3718是端口号我的门禁机器只有这个端口号可以ping通ping.ping(request_nums)res ping.result.tableret ping.result.rawretlist list(ret.split(\n))loss retlist[2].split(,)[3].split( )[1] # 获取数据包发送成功率return loss, resdef main():# 获取待ping的设备地址信息with open(C:\\Users\\Junson\\Desktop\\Script\\巡检\\门禁\\门禁设备列表.csv,r) as ipList_csv:ipList csv.reader(ipList_csv)next(ipList) #跳过首行for ipAddress in ipList:# 调用pingip方法得到数据包发送成功率loss, res pingip(ipAddress[0], 4)if float(loss.strip(%)) / 100 0.3: # 0.3为自定义数据包丢包率阈值可修改#数据包发送成功率低于30%时发送消息到企微机器人SendWeiXinWork(all,%s %s无法ping通请检查设备的网络连接%(ipAddress[0],ipAddress[1]))#记录日志file_handleopen(C:\\Users\\Junson\\Desktop\\Script\\巡检\\门禁\\log.txt,modea)file_handle.write(\n%s %s %s无法ping通请检查设备的网络连接%(datetime.now(),ipAddress[0],ipAddress[1]))file_handle.close()else:#记录日志file_handleopen(C:\\Users\\Junson\\Desktop\\Script\\巡检\\门禁\\log.txt,modea)file_handle.write(\n%s %s %sping访问正常。%(datetime.now(),ipAddress[0],ipAddress[1]))file_handle.close()if __name__ __main__:#实现服务器网络状态监控main()passcsv文件结构 创建计划任务
最终效果