锐捷网络自动连接python脚本

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

import os
import sys
import time

ip = '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('网络连接正常')
View Code

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

import threading
import time
import os
import sys

def 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()
View Code

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

参考: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

 

posted on 2019-06-30 18:23  吃我一枪  阅读(949)  评论(0编辑  收藏  举报

导航