我编写了一个python脚本来自动检查互联网可用并使用我的学生帐户登录,并在找到失去与互联网的连接时重新启动ngrok。我在安装了Ubuntu 14.04的笔记本电脑上运行这些脚本。
python脚本如下所示:
eat
python脚本调用的bash脚本如下所示:
read_all
在终端中运行#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import os
import urllib
import http
from urllib.request import urlopen
import http.cookiejar
import time
import urllib.request, urllib.parse, urllib.error
import urllib.request, urllib.error, urllib.parse
def is_online():
"""check net available"""
pattern = "location.href='http://210.77.16.21:80"
with urlopen('http://www.baidu.com') as fh:
data = fh.read().decode()
if len(data) < 500 or pattern in data:
return False
else:
return True
def login(user, password):
login_url = 'http://210.77.16.21/eportal/InterFace.do?method=login'
login_data = {
'userId': user,
'password': password,
'service': '',
'queryString': 'wlanuserip=0bc386d9e643d188b011a0d00c9b5c40&wlanacname=5fcbc245a7ffdfa4&ssid=&nasip=2c0716b583c8ac3cbd7567a84cfde5a8&mac=53ba540bde596b811a6d5617a86fa028&t=wireless-v2&url=2c0328164651e2b4f13b933ddf36628bea622dedcc302b30',
'operatorPwd': '',
'validcode': ''
}
login_data = urllib.parse.urlencode(login_data)
cookie = http.cookiejar.CookieJar()
cookie_support = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(cookie_support, urllib.request.HTTPHandler)
urllib.request.install_opener(opener)
request = urllib.request.Request(url=login_url, data=login_data.encode())
response = urllib.request.urlopen(request)
html = response.read().decode()
logined = True
if "success" in html:
pass
else:
logined = False
return logined
if __name__ == '__main__':
while True:
print (time.strftime('%l:%M%p %Z on %b %d, %Y'))
if is_online():
print ("++++++++++ you have already been online ++++++++++\n")
else:
print ("---------- you have lost net connection ----------\n")
os.system('sudo hclient -r -v eth0')
os.system('sudo dhclient -v eth0')
login('username', 'password')
os.system('./ngrok.sh')
time.sleep(20)
时,应该每20秒连续打印一次信息。
然而,有线的事情是终端会在一段时间后停止打印并卡在这里:
#!/bin/bash
sudo killall sunny
gnome-terminal -x bash -c './sunny clientid de70fb9da4504ca2,4b7359b627c409a5,925860be03fffe58'
似乎错误发生在行python3 ./net.py
之后。
我已经测试了两次,第一次在运行大约12小时后卡住,第二次在不到20分钟内卡住了。
我还使用命令12:17AM CST on Aug 06, 2017
++++++++++ you have already been online ++++++++++
12:18AM CST on Aug 06, 2017
++++++++++ you have already been online ++++++++++
12:18AM CST on Aug 06, 2017
++++++++++ you have already been online ++++++++++
12:18AM CST on Aug 06, 2017
++++++++++ you have already been online ++++++++++
12:19AM CST on Aug 06, 2017
++++++++++ you have already been online ++++++++++
12:19AM CST on Aug 06, 2017
检查了python脚本状态,它显示如下:
print (time.strftime('%l:%M%p %Z on %b %d, %Y'))
那么,有人能弄清楚为什么会出现这种奇怪的现象吗?