Skip to content

Instantly share code, notes, and snippets.

@Sg4Dylan
Last active December 4, 2017 11:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Sg4Dylan/429f34982b817d14c12b1b6161344491 to your computer and use it in GitHub Desktop.
Save Sg4Dylan/429f34982b817d14c12b1b6161344491 to your computer and use it in GitHub Desktop.
模拟深澜Srun系统认证(Python/Shell)
#!/usr/bin/env python
#coding:utf-8
# Author: Sg4Dylan --<sg4dylan#gmail.com>
# Purpose: A simple script to complete srun's webauth
# Created: 03/01/2017
# 配合 crontab 使用
# */5 6-22 * * * /usr/bin/python3 /pathtoyourscript/LoginSrunWebAuth.py
import requests
import ast
import pexpect
import time
class LoginSrunWebauth:
username = ''
password = ''
# set your school's suffix
suffix = '@xxx.edu.cn'
header = {
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25',
}
# set your api url
url = "http://10.1.88.4/cgi-bin/do_login_juniper"
def __init__(self, username, password):
self.username = username;
self.password = password;
if not self.suffix in self.username:
self.username += self.suffix
print("Initializing...");
def genLoginForm(self):
LoginForm = "{'username': '" + \
self.username + \
"','password': '" + \
self.password + \
"','drop': '1','type': '1','n': '100',}"
return ast.literal_eval(LoginForm)
def finalLogin(self):
print("Try to login with %s (at) %s" % (self.username,self.password))
r = requests.post(self.url, data=self.genLoginForm(), headers=self.header)
if "Srun_Login" in r.text:
print("Login Successful!")
return ""
return r.text
class NetStatusCheck:
check_target = ""
def __init__(self, check_target="qq.com", username, password):
if check_target != "":
self.check_target = check_target
status, result = self.ping_module(self.check_target)
while status != "OK":
insta = LoginSrunWebauth(username, password)
insta_result = insta.finalLogin()
if "Login Successful!" == insta_result:
print("Waiting for recheck status.")
time.sleep(2)
if "E2532" in insta_result:
print("Script will redial after five seconds.")
time.sleep(5)
continue
status, result = self.ping_module(self.check_target)
print("Dial complete.")
def ping_module(self, requests_address, ping_times=4, timeout_set=5):
ping_result = pexpect.spawn("ping -c%s %s" % (str(ping_times), requests_address),timeout=timeout_set)
ping_result_list = ["9999.99"] * 4
ping_status = "OK"
try:
for line in ping_result:
perline = line.decode('utf-8')
# print(perline)
if "Usage" in perline:
ping_status = "Need address"
if "unknown" in perline or "not known" in perline or "Cannot assign" in perline:
ping_status = "Unknow server"
if "rtt" in perline:
ping_result_list = perline[23:-5].split("/",3)
except pexpect.exceptions.TIMEOUT:
ping_status = "Time out"
pass
finally:
print("Net connection status: %s" % ping_status)
return ping_status, ping_result_list
NetStatusCheck(username="", password="")
#!/usr/bin/env python
#coding:utf-8
# Author: Sg4Dylan --<sg4dylan#gmail.com>
# Purpose: A simple script to complete srun's webauth
# Created: 10/14/2016
import requests
import ast
class LoginSrunWebauth:
username = ''
password = ''
# set your school's suffix
suffix = '[INPUT SUFFIX]'
header = {
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25',
}
url = "http://10.1.88.4/cgi-bin/do_login_juniper"
def __init__(self, username, password):
self.username = username;
self.password = password;
if not self.suffix in self.username:
self.username += self.suffix
print("Initializing...");
def genLoginForm(self):
LoginForm = "{'username': '" + \
self.username + \
"','password': '" + \
self.password + \
"','drop': '1','type': '1','n': '100',}"
return ast.literal_eval(LoginForm)
def finalLogin(self):
print("Try to login with %s (at) %s" % (self.username,self.password))
r = requests.post(self.url, data=self.genLoginForm(), headers=self.header)
if "Srun_Login" in r.text:
print("Login Successful:")
return ""
return r.text
# input your username & password
msg = LoginSrunWebauth("[INPUT USERNAME]", "[INPUT PASSWORD]")
print(msg.finalLogin())
#!/bin/sh
# Usage: ./srun_login.sh [username] [password]
DOWN="wget -qO-"
DATE=`date "+%Y-%m-%d %H:%M:%S"`
echo "[$DATE] Logging..."
DATA="username=$1&password=$2&drop=1&type=1&n=100"
RESULT=`$DOWN --post-data="$DATA" "http://10.1.88.4/cgi-bin/do_login_juniper" 2> /dev/null`
if echo $RESULT | grep -q "<script"
then
echo "Success"
else
echo "Failed"
echo $RESULT
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment