校园网自动登录脚本及定时任务设置

校园网自动登录脚本:

login.py

# -*- coding: utf-8 -*-
"""
@author: tan
"""

from urllib import request, parse
import subprocess
import re
import time

def get_Local_ipv6_address():

    """

    This function will return your local machine's ipv6 address if it exits.

    If the local machine doesn't have a ipv6 address,then this function return None.

    This function use subprocess to execute command "ipconfig", then get the output

    and use regex to parse it ,trying to  find ipv6 address.

    """

    getIPV6_process = subprocess.Popen("ifconfig", stdout = subprocess.PIPE)

    output = (getIPV6_process.stdout.read())

    ipv6_pattern='(([a-f0-9]{1,4}:){7}[a-f0-9]{1,4})'

    m = re.search(ipv6_pattern, str(output))

    if m is not None:

        return m.group()

    else:

        return None
    
url = "http://202.204.48.82"

parms = {'uname' : '******', 'upass' : '*******'}

username = ''******','
password = ''******','

headers = {"Connection": "keep-alive",

           "Cookie": "myusername=%s; username=%s; smartdot=%s" % (username, username, password)}

body = "DDDDD=%s&upass=%s&0MKKey=123456789&v6ip=%s" % (username,password,get_Local_ipv6_address())
querystring = parse.urlencode(parms)
req = request.Request(url, body.encode('ascii'), headers=headers)

u = request.urlopen(req)
resp = u.read()

在该文件中修改用户名和对应密码即可实现自动登录。

定时任务(shell脚本)

目标功能:实现每两秒自动登录一次。
原因:实验室的服务器每次校园网登录成功之后的不定期就会自动登出,造成断网,非常影响用户体验!

实现:crontab定时任务 + shell脚本
分析:crontab定时任务只能实现1分钟以上的定时启动任务,为了实现秒级别的定时启动,需要配合shell脚本来实现任务。

crontab中主要有四个参数(f1 f2 f3 f4 f5 program)

其中 f1 是表示分钟,f2 表示小时,f3 表示一个月份中的第几日,f4 表示月份,f5 表示一个星期中的第几天。program 表示要执行的程序。
当 f1 为 * 时表示每分钟都要执行 program,f2 为 * 时表示每小时都要执行程序,其馀类推
当 f1 为 a-b 时表示从第 a 分钟到第 b 分钟这段时间内要执行,f2 为 a-b 时表示从第 a 到第 b 小时都要执行,其馀类推
当 f1 为 */n 时表示每 n 分钟个时间间隔执行一次,f2 为 */n 表示每 n 小时个时间间隔执行一次,其馀类推
当 f1 为 a, b, c,... 时表示第 a, b, c,... 分钟要执行,f2 为 a, b, c,... 时表示第 a, b, c...个小时要执行,其馀类推
*    *    *    *    *
-    -    -    -    -
|    |    |    |    |
|    |    |    |    +----- 星期中星期几 (0 - 6) (星期天 为0)
|    |    |    +---------- 月份 (1 - 12) 
|    |    +--------------- 一个月中的第几天 (1 - 31)
|    +-------------------- 小时 (0 - 23)
+------------------------- 分钟 (0 - 59)

recurLog.sh脚本:

step=2
echo "hello,当前时间:$(date)"
i=0
while(($i<60))
do
	time=$(date)
	echo "执行一次login:$i, $time"
	sh /home/zw/Desktop/login_control.sh
	sleep 2
	i=`expr $i + $step`
done

该shell脚本实现的功能是在一分钟内,每两秒钟就启动一次login.py。

crontab定时任务设置为:
crontabFile1:

*/1 * * * * /home/zw/Desktop/school_net_autoLogin/recurLogin.sh >> /home/zw/Desktop/school_net_autoLogin/myLoginLog

作用是:每分钟执行一次recurLogin.sh脚本,并且把输入打到日志中去。

crontab的常用命令:

crontab crontabFile1 // 添加定时任务
crontab -l  // 查看当前正在运行的定时任务
systemctl status cron  // 查看cron的状态(注意在ubuntu中是服务名是cron,在别的地方是crond)
sudo service cron start // 启动cron服务
sudo service cron restart // 重启cron服务
sudo service cron stop // 关闭cron服务

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值