深圳网站制作开发排名/互联网推广方案
工作原因使用到EC20模块来给设备提供网络,使用写了个监控4G网络的脚本来监控网络状态,进行保活
该脚本可以将相关网络参数定时写入日志文件中,起到检测网络状态的作用。
安装工具与python串口库
apt install psmisc
#下载pyserial库
pip install pyserial
脚本代码
#!/usr/bin/python3# 本脚本用于监控4G模块的运行状态
# 运行环境:linux
import os,sys,re,time # 系统基础库
import serialDialer="/usr/local/bin/quectel-CM" # 拨号程序
tty_com="/dev/ttyUSB2" # AT通信口# 测试IP与相关调节参数
IP1="111.230.129.137" #公司自有IP
IP2="114.114.114.114" #DNS IP
ping_cycle = 5 #ping测试周期(秒)# 发送AT指令接收数据返回的函数
def send_AT(tty,cmd):#print("串口对象参数:",tty)tty.write(cmd.encode())data=serial.read(1)time.sleep(0.1)data = (data + serial.read(serial.inWaiting())).decode()return data# 记录当前4G网络信息
def fun_4gnet_record():print(">>> 发送询问指令,查询4G网络参数~~~~~~~~")# 配置串口接口global serial serial = serial.Serial(tty_com, 115200,bytesize=serial.EIGHTBITS, # 数据位:8位parity=serial.PARITY_NONE, # 校验位:无stopbits=serial.STOPBITS_ONE, # 停止位:1位timeout=3600) # 读超时时间if serial.is_open != False :print("打开串口成功,串口详情参数:",serial)else:print("串口打开失败!")# 获取4G信号强度 ###########################################datas = send_AT(serial,"AT+CSQ\r\n")signal_intensity_info = datas.split('\r\n')[1]# 获取卡状态 ################################################datas = send_AT(serial,"AT+CPIN?\r\n")card_status = datas.split('\r\n')[1]# 获取4G网络注册状态 #########################################datas = send_AT(serial,"AT+CREG?\r\n")registration_status = datas.split('\r\n')[1]# 查询4G的网络状态 #############################################datas = send_AT(serial,"AT+CPIN?\r\n")network_status = datas.split('\r\n')[1]# 查询PDP上下文信息 #############################################datas = send_AT(serial,"AT+CGDCONT?\r\n")PDP_data = datasserial.close() #关闭串口print(">>> 写入日志文件~~~")# # 打开日志文件log_file = open("4gnet.log","a")# 数据写入# print("["+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+"]")log_file.write(" \n\r")log_file.write("["+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+"]"+"\r\n")if(state == False): # 判断是正常状态还是异常状态?log_file.write("ping tset is failing!!! "+"\n\r")else:log_file.write("ping tset is OK!!! "+"\n\r")log_file.write(signal_intensity_info+"\n\r")log_file.write(card_status+"\n\r")log_file.write(registration_status+"\n\r")log_file.write(network_status+"\n\r")log_file.write("\b\r")log_file.write(PDP_data+"\n\r")log_file.write(" \n\r")log_file.close() #关闭日志文件# 主函数
if __name__ == "__main__":print("4G网络监测程序V1.0 启动!~")#检查是否识别到EC20模块ret = os.popen("lsusb | grep Quectel").read()if(ret.find("Quectel") != -1):print(">>> EC20设备已接入!~")# 检查是否存在ttyUSB2if(os.path.exists(tty_com) == False):print(">>> AT串口未识别~~~")exit(-1)else:print(">>> AT串口已识别~~~")# 先检查是否正在运行,关闭procedure_PID = os.popen("ps aux | grep "+Dialer+" | awk 'NR==1 {print $2}'").read()print("procedure_PID = %s"%procedure_PID)if procedure_PID != 0:os.system("kill -9 "+procedure_PID)print(">>> 存在残留的拨号进程,已关闭~~~")time.sleep(10)#再启动拨号程序cmd_ret_fp = os.system(Dialer+" &")time.sleep(7) #延时5秒,等待拨号程序运行完成# 获取生成的网卡名card_name = os.popen("ifconfig | grep ww").read().split(":")[0]print(card_name)#检测状态while True:# 简单ping公司服务器IPret_ping_1 = os.system("ping -c 1 "+IP1+" -I "+ card_name)# print(">>>"+str(ret_ping_1))if(ret_ping_1 != 0): # 不为零则为没有ping通# 再次ping别的IP,减少误判ret_ping_2 = os.system("ping -c 1 "+IP2+" -I "+ card_name)if(ret_ping_2 != 0):# 第二个IP都ping不通了,80%网络已经断开了fun_4gnet_record(False) #记录信息# 重新拨号cmd_ret_fp = os.system(Dialer+" &")time.sleep(6) #延时5秒,等待拨号程序运行完成else:time.sleep(ping_cycle) #5秒后再ping一次 #fun_4gnet_record(True) #记录信息