crosscheck自动登录脚本
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
6.6 KiB

<?php
include(__DIR__ . '/../lib/mCrossCheck.php');
ini_set('date.timezone', 'Asia/Shanghai');
// 避免程序重复运行
$need_run = cProcessNum();
if(!$need_run) {
exit("is running, exit.\n");
}
$log_file = __DIR__ . '/../logs/'.date('Y-m-d').'/crosscheck_auto_shell.log';
$log_dir = dirname($log_file);
if(!is_dir($log_dir)){
mkdir($log_dir, 0777, true);
}
try{
// 获取目录下所有文件
$dir = __DIR__ . '/../cookie_file/';
$list = scandir($dir);
// 过滤出需要登录的账号文件
$list = array_filter($list, function($v) use ($dir){
return strpos($v, 'needlogin_') === 0 && strpos($v, '.txt') !== false;
});
foreach($list as $v){
$need_login_file = $dir.$v;
// 获得文件内容
$param = file_get_contents($need_login_file);
// 获得crosscheck账号密码
$arr_user_info = explode("\n", $param);
$user = ltrim($arr_user_info[0]);
$passwd = ltrim($arr_user_info[1]);
$tenant = ltrim($arr_user_info[2]);
$fail_login_file = $dir.'fail_'.$user.'.txt';
$ban_file = $dir.'ban_'.$user.'.txt';
$ban_server_file = $dir.'server_ban.txt';
// 检测是否被禁用
if(file_exists($ban_file)){
continue;
}
// 处理错误登录文件
if(file_exists($fail_login_file)){
// 获得文件内容
$fail_info = file_get_contents($fail_login_file);
1 month ago
$arr_fail_info = explode("\n", $fail_info);
if($arr_fail_info[0]+0 >=15){
continue;
}
}
// 检测是否被禁用服务器
if(file_exists($ban_server_file)){
$ban_server_limit_times = 3;
// 获得文件的创建时间
$create_time = filemtime($ban_server_file);
// 检查是否超过1小时
if(time() - $create_time > 60*60){
// 重置禁用服务器文件
unlink($ban_server_file);
file_put_contents($ban_server_file, ($ban_server_limit_times-1) . PHP_EOL);
// 记录日志
$str_log = date('Y-m-d H:i:s')."|重置禁用服务器文件server_ban.txt|".$user;
file_put_contents($log_file, $str_log . PHP_EOL, FILE_APPEND);
}
$ban_server_info = file_get_contents($ban_server_file);
$arr_ban_server_info = explode("\n", $ban_server_info);
if($arr_ban_server_info[0]+0 >= $ban_server_limit_times){
continue;
}
}
// 账号cookie文件路径
$cookie_path = __DIR__ . '/../cookie_file/'.$user.'.txt';
$obj->cookie_path = $cookie_path ;
if (file_exists($cookie_path)) {
chown($cookie_path, 'nobody');
chgrp($cookie_path, 'nobody');
chmod($cookie_path, 0777);
}
$capture_path = __DIR__ . '/../cookie_file/screen_shots/';
if (!is_dir($capture_path)) {
mkdir($capture_path, 0777, true);
chown($capture_path, 'nobody');
chgrp($capture_path, 'nobody');
}
// 执行java登录脚本,获取脚本运行的时间
$start_time = time(true);
$cmd = "java -jar /datacenter/java/crossrefLogin_v2.jar -u '{$user}' -p '{$passwd}' -f '{$cookie_path}' -t '{$tenant}' -capture_path '{$capture_path}' -only_login";
echo $cmd.PHP_EOL;
//java -jar crossref-1.0-SNAPSHOT.jar -u '2105444612@qq.com' -p '038300asz_A' -f './mayi2_cookie.txt' -capture
$console_str = trim(shell_exec($cmd));
// 计算脚本运行时间
$end_time = time(true);
$run_time = $end_time - $start_time;
$run_time_str = date('i:s', $run_time);
$str_log = date('Y-m-d H:i:s')."|登录信息|".$console_str."|运行时间:".$run_time_str;
file_put_contents($log_file, $str_log . PHP_EOL, FILE_APPEND);
// 判断登录是否成功
if (strpos($console_str, '登录成功') === false) { // 登录失败
//被禁
if(strpos($console_str, 'deactivated') !== false){
// 写入被禁用账号文件
file_put_contents($ban_file, $console_str . PHP_EOL);
// 记录日志
$str_log = date('Y-m-d H:i:s')."|账号被禁用|".$user."|运行时间:".$run_time_str;
file_put_contents($log_file, $str_log . PHP_EOL, FILE_APPEND);
}
// 写入错误登录文件
$fail_info = @$arr_fail_info[0]+1;
file_put_contents($fail_login_file, $fail_info);
// 记录日志
$str_log = date('Y-m-d H:i:s')."|登录失败|第".$fail_info."次|".$user."|运行时间:".$run_time_str;
file_put_contents($log_file, $str_log . PHP_EOL, FILE_APPEND);
// 服务器被限制访问
if(strpos($console_str, '当前页面URL') == false){
// 写入被禁用服务器文件
$ban_server_info = @$arr_ban_server_info[0]+1;
file_put_contents($ban_server_file, $ban_server_info . PHP_EOL);
// 记录日志
$str_log = date('Y-m-d H:i:s')."|服务器被禁用|".$user."|运行时间:".$run_time_str;
file_put_contents($log_file, $str_log . PHP_EOL, FILE_APPEND);
}
}else{
// 记录日志
$str_log = date('Y-m-d H:i:s')."|登录成功|".$user."|运行时间:".$run_time_str;
file_put_contents($log_file, $str_log . PHP_EOL, FILE_APPEND);
// 删除需要登录的账号文件
unlink($need_login_file);
@unlink($fail_login_file);
@unlink($ban_file);
@unlink($ban_server_file);
}
system("pkill -9 chrome");
$sec = rand(100,245);
sleep($sec);
}
}catch(Exception $e){
$str_log = date('Y-m-d H:i:s')."|异常退出|异常信息:".$e->getMessage();
file_put_contents($log_file, $str_log . PHP_EOL, FILE_APPEND);
}
/**
* 进程上限限制
*
* @return boolean
*/
function cProcessNum() {
// 当前程序文件名包含路径
$path_deal = __FILE__;
$processnum = 1;
$cmd = "ps -ef | grep '{$path_deal}' | grep -v grep | grep -v '\/bin\/sh' | grep -v 'sh -c' | wc -l";
$rcmd = @popen($cmd, 'r');
$num = @fread($rcmd, 512);
$num += 0;
echo "[$path_deal]进程数超过{$processnum}个,当前进程数{$num}个\n";
@pclose($rcmd);
if($num >$processnum) {
return false;
}
return true;
}