Skip to content

Instantly share code, notes, and snippets.

@rikumi
Created September 14, 2017 01:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rikumi/cfebe99ac1acc53208572552ffae5bef to your computer and use it in GitHub Desktop.
Save rikumi/cfebe99ac1acc53208572552ffae5bef to your computer and use it in GitHub Desktop.
seu-wlan 自动登录/轮询登录脚本
const axios = require('axios');
const read = require('readline-sync');
const program = require('commander');
program
.version('1.0.0')
.option('-u, --username [username]', '设置用户名')
.option('-p, --password [password]', '设置密码')
.option('-d, --daemon [seconds]', '每隔数秒轮询网络状态,一旦退出自动重新登录', '0')
.parse(process.argv);
const seu = axios.create({
baseURL: 'http://w.seu.edu.cn/index.php/index/',
timeout: 3000,
validateStatus: () => true,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
});
(async () => {
const daemonInterval = parseInt(program.daemon)
if (daemonInterval) {
let username = program.username || read.question('用户名:');
let password = program.password || read.question('密码:');
password = new Buffer(password).toString('base64');
while (true) {
let status = await seu.get('init');
console.log(new Date(), status.data.info);
if (!status.data.status) {
let status = await seu.post('login', 'username=' + username + '&password=' + password + '&enablemacauth=1');
console.log(new Date(), status.data.info);
}
await new Promise(r => setTimeout(r, daemonInterval * 1000));
}
} else {
let status = await seu.get('init');
console.log(status.data.info);
if (status.data.status) {
if (read.question('是否退出?y/N:').toLowerCase() === 'y') {
let status = await seu.get('logout');
console.log(status.data.info);
}
} else {
let username = program.username || read.question('用户名:');
let password = program.password || read.question('密码:');
password = new Buffer(password).toString('base64');
let status = await seu.post('login', 'username=' + username + '&password=' + password + '&enablemacauth=1');
console.log(status.data.info);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment