python网络登录脚本_锐捷网络自动连接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 iP == 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

?来源:https://www.icode9.com/content-1-284801.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值