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.

98 lines
3.5 KiB

<?php
include(__DIR__ . '/../lib/mCrossCheck.php');
ini_set('date.timezone', 'Asia/Shanghai');
// 同一时间只能有一个进程在运行
$lock_file = __DIR__ . '/crosscheck_auto_shell.lock';
if (file_exists($lock_file)) {
echo '另一个进程正在运行,请稍后重试。' . PHP_EOL;
exit(1);
}
// 创建锁文件
touch($lock_file);
$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{
$obj = new mCrossCheck();
$list = $obj->getCrossCheckAccount();
// 测试数据
// $list[] = [
// 'user' => 'mayi001',
// 'passwd' => 'Eg98*!f@42',
// 'ext_info' => 'crossref-26027,zzz',
// ];
if (empty($list)){
throw new Exception('获取crosscheck账号列表为空');
}
foreach($list as $v){
// 获得crosscheck账号密码
$user = trim($v['user']);
$passwd = $v['passwd'];
$extdata = explode(',', $v['ext_info']);
$tenant = $extdata[0];
// 账号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);
}
// 检测账号cookie是否有效
$res = $obj->islogin();
if ($res) {
// 账号cookie有效,无需登录
//exit('账号cookie有效,无需登录'.$user.PHP_EOL);
$str_log = date('Y-m-d H:i:s')."|验证成功|".$user;
file_put_contents($log_file, $str_log . PHP_EOL, FILE_APPEND);
}else{
//exit('账号cookie过期,无需登录'.$user.PHP_EOL);
// 账号cookie过期,需要重新登录
unlink($cookie_path); // 删除过期cookie
// 执行java登录脚本,获取脚本运行的时间
$start_time = time(true);
$cmd = 'java -jar /datacenter/java/crossrefLogin.jar "' . $user.'" "'.$passwd.'" "'.$cookie_path.'"';
$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) { // 登录失败
// 记录日志
$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);
// 推送cookie到正式服务器
}
system("pkill -9 chrome");
}
}
sleep(5);
// 删除锁文件
unlink($lock_file);
}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);
// 删除锁文件
unlink($lock_file);
}