首页 技术 正文
技术 2022年11月15日
0 收藏 469 点赞 2,545 浏览 1263 个字

1 实现锐捷网络的连接,当断开后自动重连

import os
import sys
import timeip = 'www.baidu.com'
print('开始ping百度')
backinfo = os.system('ping -c 1 -w 1 %s'%ip) # 实现pingIP地址的功能,-c1指发送报文一次,-w1指等待1秒
# print('backinfo is:', backinfo)
for i in range(500000):
if backinfo:
print('网络已断开')
print('正在连接............')
os.system('sudo /home/sxtj/sw/rj/rj.sh -u 账户 -p 密码')
print('网络已经断开')
print('马上连接,清稍等')
if i%50 == 0 :
# 如果多次超过50次未连接,不是被别的电脑挤掉线,需重启电脑才能连接
# os.system('reboot')
else:
print('网络连接正常')

2 实现锐捷网络的连接,并定时检查是否断开,如果断开自动连接

import threading
import time
import os
import sysdef connect_network():
print('网络已断开')
print('正在连接............')
# 直接执行Linux系统中的Linux shell脚步
os.system('sudo /home/sxtj/sw/rj/rj.sh -u 账号 -p 密码')
def check_network():
while True:
time.sleep(5) # 等待connect_network()函数连接网络
ip = 'www.baidu.com'
# 实现pingIP地址的功能,-c1指发送报文一次,-w1指等待1秒
backinfo = os.system('ping -c 1 -w 1 %s' % ip)
if backinfo:
connect_network()
else:
print('网络连接正常')
time.sleep(600) # 隔十分钟检查一次网络
# 用于存放线程名称
threads = []
threads.append(threading.Thread(target=connect_network))
threads.append(threading.Thread(target=check_network))
if __name__ == '__main__':
for t in threads:
t.start()

两种方法都可以实现断网后立即自动重连,第二种方法更高级些

参考:https://www.oschina.net/question/2008758_2286029

https://www.cnblogs.com/hei-hei-hei/p/7216434.html

https://blog.csdn.net/wang_da_bing/article/details/82729462

https://www.cnblogs.com/winterbear/p/10964682.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,910
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,435
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,250
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,061
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,693
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,731