扫描翻墙进程
<?php
//这个是processScanner.php的源码
$search_item = ["x-ui","xray-linux-amd6"]; // 进程名
$database_ip = "127.0.0.1";
$database_user = "idc";
$database_pass = "d844F7AiMF8KPxPC";
$database_name = "idc";
$database_mj_ip = "154.40.44.133";
$database_mj_user = "read";
$database_mj_pass = "read85462";
$database_mj_name = "cloud";
// 查询表
$sql = "SELECT b.ip, a.rootpassword, a.port FROM cloud_host a INNER JOIN cloud_ip b ON a.id = b.hostid WHERE a.port = 22";
$conn = new mysqli($database_mj_ip, $database_mj_user, $database_mj_pass, $database_mj_name);
if ($conn->connect_errno) {
die("数据库连接错误: " . $conn->connect_errno);
}
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->get_result();
// 构建 ps 命令
$ps_command = "ps aux";
// 循环结果
if ($result->num_rows > 0) {
$conn = new mysqli($database_ip, $database_user, $database_pass, $database_name);
if ($conn->connect_error) {
die("数据库连接错误: " . $conn->connect_errno);
}
while ($row = $result->fetch_assoc()) {
$ip = $row["ip"];
$rootpassword = $row["rootpassword"];
$port = $row["port"];
// 构建 ssh 命令
$timeout = 1; // 超时时间,单位为秒
$ssh_command = "sshpass -p '$rootpassword' ssh -o StrictHostKeyChecking=no -o ConnectTimeout=$timeout -p $port root@$ip \"$ps_command\" 2>&1";
$max_retries = 3;
$attempt = 0;
$connected = false;
$output = null;
$error_message = '';
// 尝试连接
while ($attempt < $max_retries && !$connected) {
$output = shell_exec($ssh_command);
if ($output === null || strpos($output, 'ssh: connect to host') !== false) {
// 连接错误
$attempt++;
if ($attempt < $max_retries) {
sleep(1); // 等待 1 秒再重试
}
} elseif (strpos($output, 'REMOTE HOST IDENTIFICATION HAS CHANGED') !== false) {
// 主机密钥变更错误
$error_message = "REMOTE HOST IDENTIFICATION HAS CHANGED";
// 移除旧的主机密钥
$remove_key_command = "ssh-keygen -R $ip";
shell_exec($remove_key_command);
// 等待 1 秒再重试
sleep(1);
} else {
$connected = true;
}
}
if (!$connected) {
// 记录连接错误
$error_message = $error_message ?: ($output ? $output : "未知错误");
$sql_error_insert = "INSERT INTO process_scanner (ip_address, process_info, note, insert_time) VALUES (?, ?, ?, NOW())";
$stmt_error_insert = $conn->prepare($sql_error_insert);
if (!$stmt_error_insert) {
echo "准备插入错误记录时出错: " . $conn->error . "\n";
continue;
}
$process_info = "";
$stmt_error_insert->bind_param("sss", $ip, $process_info, $error_message);
if (!$stmt_error_insert->execute()) {
echo "插入错误记录时出错,IP地址: $ip: " . $stmt_error_insert->error . "\n";
} else {
echo "错误记录已保存,IP地址: $ip\n";
}
$stmt_error_insert->close();
continue;
}
// 处理找到的进程信息
$found_processes = explode("\n", trim($output));
$process_info = implode(",", $found_processes);
// 插入数据库 进程信息使用数组字符串
$sql_insert = "INSERT INTO process_scanner (ip_address, process_info, note, insert_time) VALUES (?,?, ?, NOW())";
$stmt_insert = $conn->prepare($sql_insert);
if (!$stmt_insert) {
echo "准备插入记录时出错: " . $conn->error . "\n";
continue;
}
// 查看进程中包含search_item 中的哪些
$found_processes = array_filter($found_processes, function($process) use ($search_item) {
return in_array($process, $search_item);
});
// 如果没找到,写入备注
if (empty($found_processes)) {
$process_info_str = "未找到进程";
$stmt_insert->bind_param("sss", $ip, $process_info, $process_info_str);
} else {
// 如果找到,写入备注
$process_info_str = "找到进程: " . implode(",", $found_processes) . ",执行关机";
$stmt_insert->bind_param("sss", $ip, $process_info, $process_info_str);
// ssh 发送关机命令
$ssh_command = "sshpass -p '$rootpassword' ssh -o StrictHostKeyChecking=no -p $port root@$ip \"shutdown -h now\" 2>&1";
$output = shell_exec($ssh_command);
if ($output === null || strpos($output, 'ssh: connect to host') !== false) {
echo "连接到 $ip 时出错: $output\n";
} else {
echo "关机命令已发送到 $ip\n";
}
}
if (!$stmt_insert->execute()) {
echo "插入记录时出错,IP地址: $ip: " . $stmt_insert->error . "\n";
} else {
echo "插入成功,IP地址: $ip\n";
}
$stmt_insert->close();
}
} else {
echo "未找到结果。";
}
// 获取今天和昨天的日期
$today = date("Y-m-d");
$yesterday = date("Y-m-d", strtotime("-1 day"));
// SQL 查询:删除不是今天也不是昨天的记录
$sql_delete = "DELETE FROM process_scanner WHERE DATE(insert_time) < ? OR (DATE(insert_time) > ? AND DATE(insert_time) <> ?)";
$stmt = $conn->prepare($sql_delete);
if (!$stmt) {
echo "准备删除记录时出错: " . $conn->error . "\n";
} else {
// 绑定参数
$stmt->bind_param("sss", $yesterday, $today, $today);
if (!$stmt->execute()) {
echo "删除记录时出错: " . $stmt->error . "\n";
$conn->close();
exit;
} else {
echo "删除成功\n";
}
}
$stmt->close();
$conn->close();
scanner.service
[Unit]
Description=process_scanner
[Service]
Type=oneshot
ExecStart=/www/server/php/81/bin/php /www/wwwroot/idc.zaojiamao.cn/processScanner.php
scanner.timer
[Unit]
Description=Run scanner service daily at 3 AM
[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true
[Install]
WantedBy=timers.target
取消函数shell_exec禁用
安装
sshpass
启用 EPEL 源:
sudo yum install epel-release
安装 sshpass
:
sudo yum install sshpass
自动控制速
main.service
[Unit]
Description=My Script Service
After=network.target
[Service]
ExecStart=/www/server/php/81/bin/php /www/wwwroot/idc.zaojiamao.cn/main.php
Restart=always
RestartSec=5
StartLimitIntervalSec=0
[Install]
WantedBy=multi-user.target
main.php
<?php
require __DIR__ . '/vendor/autoload.php';
//当前时间
// 设置时区为中国标准时间(北京时间)
$reactConnector = new \React\Socket\Connector([
'dns' => '8.8.8.8',
'timeout' => 10
]);
$loop = \React\EventLoop\Loop::get();
$connector = new \Ratchet\Client\Connector($loop, $reactConnector);
$NetInSpeed = 0;
$NetOutSpeed = 0;
$LastLimitTime = null;
function getAccessToken()
{
static $access_token = null;
static $token_expiry = null;
// 检查令牌是否已存在且未过期
if ($access_token && $token_expiry && time() < $token_expiry) {
return $access_token;
}
$url = 'http://154.40.44.133/jNzhtEmbEa/v1/login';
$data = array(
'username' => 'admin001',
'password' => 'BLACK001',
'customfield' => array(
'google_code' => ''
)
);
$headers = array(
'accept: application/json, text/plain, */*',
'accept-language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,en-GB;q=0.6',
'cache-control: no-cache',
'content-security-policy: upgrade-insecure-requests',
'content-type: application/json;charset=UTF-8',
'think-lang: zh-cn'
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_REFERER, 'http://154.40.44.133/jNzhtEmbEa/');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 不验证证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 不验证主机名
curl_setopt($ch, CURLOPT_HEADER, false); // 不返回头部信息
curl_setopt($ch, CURLOPT_NOBODY, false); // 返回正文
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
return null;
}
curl_close($ch);
// 去掉开头和末尾的引号
$access_token = substr($response, 1, -1);
// 设置令牌过期时间为当前时间加上10分钟
$token_expiry = time() + 600;
return $access_token;
}
$connector('ws://154.37.215.213:8008/ws', [], ['Origin' => 'http://154.37.215.213:8008'])
->then(function (\Ratchet\Client\WebSocket $conn) use (&$NetInSpeed, &$NetOutSpeed, &$LastLimitTime) {
$conn->on('message', function (\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($conn, &$NetInSpeed, &$NetOutSpeed,&$LastLimitTime) {
$servers = json_decode($msg)->servers;
//找到"ID": 3的数据
$server = array_filter($servers, function ($server) {
return $server->ID == 3;
});
$NetInSpeed = $server[2]->State->NetInSpeed;
$NetOutSpeed = $server[2]->State->NetOutSpeed;
//转换成MB
$NetInSpeed = $NetInSpeed / 1024 / 1024;
$NetOutSpeed = $NetOutSpeed / 1024 / 1024;
$limitup = "5";
if ($NetInSpeed > $limitup || $NetOutSpeed > $limitup) {
echo "查询到带宽为" . $limitup . ",开始执行限速\n";
//获取access_token
$access_token = getAccessToken();
$LastLimitTime = time();
$ch = curl_init("http://154.40.44.133/jNzhtEmbEa/v1/clouds/real_data");
// 设置 cURL 选项
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 将响应作为字符串返回,而不是直接输出
curl_setopt($ch, CURLOPT_POST, true); // 设置请求方法为 POST
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array("id" => array()))); // 设置请求体内容
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json, text/plain, */*',
'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'Access-Token: '. $access_token ,
'Cache-Control: no-cache',
'Content-Security-Policy: upgrade-insecure-requests',
'Content-Type: application/json;charset=UTF-8',
'Pragma: no-cache',
'Think-Lang: zh-cn'
));
// 执行 cURL 请求
$response = curl_exec($ch);
// 检查是否有错误发生
if(curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$url = 'http://154.40.44.133/jNzhtEmbEa/v1/clouds?page=1&per_page=50&search=&sort=desc&orderby=current_in_bw&searchtype=0&image_version=&advanced_cpu=&advanced_bw=';
$headers = array(
'accept: application/json, text/plain, */*',
'accept-language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,en-GB;q=0.6',
'access-token: ' . $access_token,
'cache-control: no-cache',
'pragma: no-cache',
'think-lang: en-us',
'Cache-Control: no-cache',
'Pragma: no-cache'
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_REFERER, 'http://154.40.44.133/jNzhtEmbEa/');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 不验证证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 不验证主机名
curl_setopt($ch, CURLOPT_HEADER, false); // 不返回头部信息
curl_setopt($ch, CURLOPT_NOBODY, false); // 返回正文
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
curl_close($ch);
$response = json_decode($response);
if (isset($response->data) && is_array($response->data) && count($response->data) > 5) {
//获取前5个服务器
$servers = $response->data;
$servers = array_slice($servers, 0, 5);
foreach ($servers as $server) {
$my_in_bw = (int) ($server->in_bw);
$my_out_bw = $server->out_bw;
$my_id = (int) ($server->id);
// 去掉单位符号bps并转换为整数
$before_in_bw_bps = (int) str_replace("bps", "", $server->current_in_bw);
$before_out_bw_bps = (int) str_replace("bps", "", $server->current_out_bw);
if ($before_in_bw_bps > 20) {
// 将bps转换为MBps
// echo $before_in_bw_bps ."单位是b";
$before_in_bw = $before_in_bw_bps / 1024; // 1 MBps = 8,000,000 bps
$before_out_bw = $before_out_bw_bps / 1024;
} else {
// 将bps转换为MBps
// echo $before_in_bw_bps ."本身就是m";
$before_in_bw = $before_in_bw_bps; // 1 MBps = 8,000,000 bps
$before_out_bw = $before_out_bw_bps;
};
$in_bw = $before_in_bw;
$out_bw = $before_out_bw;
$url = 'http://154.40.44.133/jNzhtEmbEa/v1/bws/' . $server->id;
$need_limit = 0;
if ($in_bw > 1) {
$in_bw = ceil($in_bw / 2); // 将 in_bw 大于 1 的值改为一半,使用 ceil() 函数确保结果为整数
// $in_bw = ceil(1); // 将 in_bw 大于 1 的值改为一半,使用 ceil() 函数确保结果为整数
$need_limit = 1;
echo "限速为" . $in_bw;
}
if ($out_bw > 1) {
$in_bw = ceil($in_bw / 2); // 将 in_bw 大于 1 的值改为一半,使用 ceil() 函数确保结果为整数
// $in_bw = ceil(1); // 将 in_bw 大于 1 的值改为一半,使用 ceil() 函数确保结果为整数
$need_limit = 1;
echo "限速为" . $in_bw;
}
echo "$need_limit=" . $need_limit;
if ($need_limit == 1) {
echo "限速为" . $in_bw;
date_default_timezone_set('Asia/Shanghai');
// 获取当前时间戳
$current_time = time();
// 使用 date 函数格式化时间,并输出
$time = date('Y-m-d H:i:s', $current_time);
echo $time;
//实例ip:103.112.96.20 当前带宽为: 2Mbps,需要进行限速处理
echo "实例ip: " . $server->mainip . " 当前带宽为: " . $before_in_bw . "Mbps,需要进行限速处理 " . $in_bw . "\n";
$data = array(
'id' => 192,
'in_bw' => $in_bw,
'out_bw' => $in_bw,
);
// echo $data[in_bw];
// var_dump($data);
echo "限速为" . $in_bw;
$headers = array(
'accept: application/json, text/plain, */*',
'accept-language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,en-GB;q=0.6',
'access-token: ' . $access_token,
'cache-control: no-cache',
'content-type: application/json;charset=UTF-8',
'pragma: no-cache',
'think-lang: zh-cn',
'referrer: http://154.40.44.133/jNzhtEmbEa/'
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
} else {
$current_time = time();
// 使用 date 函数格式化时间,并输出
$time = date('Y-m-d H:i:s', $current_time);
// echo $time."\n";
$record_time = $time;
#将修改前的带宽值保存到数据库中 插入
$servername = "8.218.114.138";
$username = "idc";
$password = "d844F7AiMF8KPxPC";
$dbname = "idc";
$mysqlconn = new mysqli($servername, $username, $password, $dbname);
if ($mysqlconn->connect_error) {
die("连接失败: " . $mysqlconn->connect_error);
}
// 准备查询IP是否存在的SQL语句
$check_sql = "SELECT COUNT(*) FROM `bw_limit` WHERE `ip` = ?";
$check_stmt = $mysqlconn->prepare($check_sql);
// 检查预处理语句是否成功
if (!$check_stmt) {
die("准备查询IP预处理语句失败: " . $mysqlconn->error);
}
// 绑定参数并执行查询
$check_stmt->bind_param("s", $server->mainip);
$check_stmt->execute();
$check_stmt->bind_result($count);
$check_stmt->fetch();
$check_stmt->close();
// 如果IP存在,则更新其他列,否则插入新记录
if ($count > 0) {
// 更新SQL语句
$update_sql = "UPDATE `bw_limit` SET `id` = ?, `in_bw` = ?, `out_bw` = ? WHERE `ip` = ?";
$stmt = $mysqlconn->prepare($update_sql);
if (!$stmt) {
die("准备更新预处理语句失败: " . $mysqlconn->error);
}
$stmt->bind_param("ssss",$server->id, $in_bw, $out_bw, $server->mainip);
echo "设置的带宽" . $my_in_bw;
} else {
$sql = "INSERT INTO `bw_limit` (`id`, `ip`, `before_in_bw`, `before_out_bw`, `in_bw`, `out_bw`)
VALUES (?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE `in_bw`=VALUES(`in_bw`), `out_bw`=VALUES(`out_bw`)";
echo "设置的带宽" . $my_in_bw;
$stmt = $mysqlconn->prepare($sql);
$stmt->bind_param("ssssss", $server->id, $server->mainip, $my_in_bw, $my_out_bw, $in_bw, $out_bw);
}
if (!$stmt->execute()) {
echo "Error executing statement: " . $stmt->error;
}
$stmt->close();
$servername = "8.218.114.138";
$username = "idc";
$password = "d844F7AiMF8KPxPC";
$dbname = "idc";
$mysqlconn = new mysqli($servername, $username, $password, $dbname);
if ($mysqlconn->connect_error) {
die("连接失败: " . $mysqlconn->connect_error);
}
// 构建 SQL 查询
// 准备更新语句
$sql = "UPDATE bw_limit SET cisu=cisu+1 WHERE ip = '" . $server->mainip . "'";
// $sql = "SELECT * FROM `bw_limit` WHERE record_time < DATE_SUB(NOW(), INTERVAL ".$mm." minute)";
echo $sql;
$result = $mysqlconn->query($sql);
$mysqlconn->close();
$chan_bw = $in_bw * 2;
date_default_timezone_set('Asia/Shanghai');
// 获取当前时间戳
$current_time = time();
// 使用 date 函数格式化时间,并输出
$time = date('Y-m-d H:i:s', $current_time);
// echo $time."\n";
//实例ip:103.112.96.20 实时带宽为2Mbps,已经对其限速为1mbps
echo "实例ip: " . $server->mainip . " 实时带宽为" . $before_in_bw . "Mbps,已经对其限速为" . $in_bw . "mbps\n";
}
curl_close($ch);
} else {
date_default_timezone_set('Asia/Shanghai');
// 获取当前时间戳
$current_time = time();
// 使用 date 函数格式化时间,并输出
$time = date('Y-m-d H:i:s', $current_time);
// echo $time."\n";
echo "实例ip: " . $server->mainip . " 实时带宽为" . $before_in_bw . "Mbps,不需要限速\n";
}
}
}
}
});
$conn->on('close', function ($code = null, $reason = null) {
echo "Connection closed ({$code} - {$reason})\n";
});
}, function (\Exception $e) use ($loop) {
echo "Could not connect: {$e->getMessage()}\n";
$loop->stop();
});
$loop->addPeriodicTimer(5, function () use ($loop,&$LastLimitTime) {
if(time() - $LastLimitTime < 10){
return;
}
//$tt=$rowtime;
//$mm=formatTime($seconds);
// $tt = $rowtime;
$mm = "5";
echo $mm . "s...";
$servername = "8.218.114.138";
$username = "idc";
$password = "d844F7AiMF8KPxPC";
$dbname = "idc";
$mysqlconn = new mysqli($servername, $username, $password, $dbname);
if ($mysqlconn->connect_error) {
die("连接失败: " . $mysqlconn->connect_error);
}
// 定义变量
// $interval = $mm; // 6分钟作为变量
$interval = "5"; // 6分钟作为变量
// 构建 SQL 查询
/* $sql = "SELECT * FROM `bw_limit` WHERE record_time < DATE_SUB(NOW(), INTERVAL $interval MINUTE) and record_time>0";*/
$sql = "SELECT *
FROM `bw_limit`
WHERE record_time < DATE_SUB(NOW(), INTERVAL $interval SECOND)
AND record_time > 0
AND cisu < '80'
AND (CASE WHEN before_in_bw >= in_bw THEN before_in_bw - in_bw ELSE 0 END) >= 1
ORDER BY record_time ASC";
// $sql = "SELECT * FROM `bw_limit` WHERE record_time < DATE_SUB(NOW(), INTERVAL ".$mm." second)";
// echo $sql;
$result = $mysqlconn->query($sql);
echo "执行完了/n";
$mysqlconn->close();
if ($result->num_rows > 0 ) {
echo "有复合条件的";
while ($row = $result->fetch_assoc()) {
echo "while条件的";
//*/******************************************************************************************************
$iid = $row['id'];
$iip = $row['ip'];
$iin_bw = $row['in_bw'];
//*/******************************************************************************************************
//获取access_token
$access_token = getAccessToken();
//现有带宽向上加1即可
$aabefore_in_bw = $row['before_in_bw'];
$aain_bw = $row['in_bw'];
$aainw = $row['in_bw'];
if ($row['before_in_bw'] - $row['in_bw'] >= 1) {
$inw = $row['in_bw'] + 1;
} else {
$inw = $row['before_in_bw'];
$servername = "8.218.114.138";
$username = "idc";
$password = "d844F7AiMF8KPxPC";
$dbname = "idc";
$mysqlconn = new mysqli($servername, $username, $password, $dbname);
if ($mysqlconn->connect_error) {
die("连接失败: " . $mysqlconn->connect_error);
}
$current_time = time();
// 使用 date 函数格式化时间,并输出
$time = date('Y-m-d H:i:s', $current_time);
// echo $time."\n";
$record_time = $time;
// 构建 SQL 查询
// 准备更新语句
$sql = "UPDATE bw_limit SET record_time = '" . $record_time . " ' WHERE id = '" . $row['id'] . "'";
//echo $sql."\n" ;
// $sql = "SELECT * FROM `bw_limit` WHERE record_time < DATE_SUB(NOW(), INTERVAL ".$mm." minute)";
// echo $sql;
$result = $mysqlconn->query($sql);
$mysqlconn->close();
};
// echo "实例{$iip} 设置带宽为{$row['before_in_bw']}Mbps,已经对其限速为{$row['in_bw']}Mbps,现在已经解除限制为{$inw}Mbps\n";
$url = 'http://154.40.44.133/jNzhtEmbEa/v1/bws/' . $row['id'];
echo $url."/n" ;
$data = array(
'in_bw' => $inw,
'out_bw' => $inw,
);
$servername = "8.218.114.138";
$username = "idc";
$password = "d844F7AiMF8KPxPC";
$dbname = "idc";
$mysqlconn = new mysqli($servername, $username, $password, $dbname);
if ($mysqlconn->connect_error) {
die("连接失败: " . $mysqlconn->connect_error);
}
$current_time = time();
// 使用 date 函数格式化时间,并输出
$time = date('Y-m-d H:i:s', $current_time);
// echo $time."\n";
// 构建 SQL 查询
// 准备更新语句
//$sql = "UPDATE bw_limit SET in_bw = '" . $inw . "' ,record_time='" . $time . "' WHERE id = '" . $row['id'] . "'";
$sql = "SELECT cisu , before_in_bw FROM bw_limit WHERE id = '" . $row['id'] . "'";
// $sql = "SELECT * FROM `bw_limit` WHERE record_time < DATE_SUB(NOW(), INTERVAL ".$mm." minute)";
// echo $sql;
$result = $mysqlconn->query($sql);
// 检查查询是否成功
if ($result === false) {
echo "查询失败: " . $mysqlconn->error;
} else {
// 检查是否有结果行
if ($result->num_rows > 0) {
// 输出获取到的 cishu 字段的值
$row = $result->fetch_assoc(); // 获取查询结果的关联数组形式
// echo "cisu 字段的值为: " . $row['cisu']."\n";
$life = $row['cisu'];
$xisu= $row['before_in_bw'];
} else {
echo "未找到匹配的记录。\n";
}
}
// 释放查询结果
$result->free();
// 关闭数据库连接
$mysqlconn->close();
if ($life < 80 and $aabefore_in_bw - $aain_bw >= 1) {
echo "$life < 80匹$aabefore_in_bw - $aain_bw >= 1配的记录。\n";
// $inw=$row['in_bw']+1;
$servername = "8.218.114.138";
$username = "idc";
$password = "d844F7AiMF8KPxPC";
$dbname = "idc";
$mysqlconn = new mysqli($servername, $username, $password, $dbname);
if ($mysqlconn->connect_error) {
die("连接失败: " . $mysqlconn->connect_error);
}
$current_time = time();
// 使用 date 函数格式化时间,并输出
// $time = date('Y-m-d H:i:s', $current_time);
if ($life < 10){
$limit=10;
}elseif($life < 20){
$limit=40;
}elseif($life < 30){
$limit=120;
}elseif($life < 40){
$limit=240;
}elseif($life < 50){
$limit=340;
}elseif($life < 60){
$limit=440;
}elseif($life < 70){
$limit=550;
}elseif($life < 80){
$limit=30;
}else{
$limit=30;
};
date_default_timezone_set('Asia/Shanghai');
// 获取当前时间戳
$current_time = time();
$future_time = $current_time + $limit;
//对时间延期效果进行控制,通过次数
$time = date('Y-m-d H:i:s', $future_time);
// 加上10秒的时间戳
// echo $time."\n";
echo "原始时间=".$current_time."次数为".$life."延迟时间+".$limit."当前时间=".$time."\n";
// 构建 SQL 查询
// 准备更新语句
// $sql = "UPDATE bw_limit SET in_bw = '" . $inw . "' ,record_time='" . $time . "' WHERE id = '" . $iid . "'";
$sql = "UPDATE bw_limit SET in_bw = '" . $inw . "' , record_time = '" . $time . "' WHERE id = '" . $iid . "'";
// $sql = "SELECT * FROM `bw_limit` WHERE record_time < DATE_SUB(NOW(), INTERVAL ".$mm." minute)";
// echo $sql;
$result = $mysqlconn->query($sql);
$mysqlconn->close();
// $row['before_in_bw']
// $row['before_out_bw']
$url = 'http://154.40.44.133/jNzhtEmbEa/v1/bws/' . $iid;
echo "新的url".$url;
$headers = array(
'accept: application/json, text/plain, */*',
'accept-language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,en-GB;q=0.6',
'access-token: ' . $access_token,
'cache-control: no-cache',
'content-type: application/json;charset=UTF-8',
'pragma: no-cache',
'think-lang: zh-cn',
'referrer: http://154.40.44.133/jNzhtEmbEa/'
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
};
echo "xinde 实例{$iip} 设置带宽为{$aabefore_in_bw}Mbps,已经对其限速为{$aain_bw}Mbps,现在已经解除限制为{$inw}Mbps\n";
/* $servername = "8.218.114.138";
$username = "idc";
$password = "d844F7AiMF8KPxPC";
$dbname = "idc";
$mysqlconn = new mysqli($servername, $username, $password, $dbname);
$mysqlconn->query("DELETE FROM `bw_limit` WHERE `id` = '{$row['id']}'");
$mysqlconn->close();
curl_close($ch);*/
date_default_timezone_set('Asia/Shanghai');
// 获取当前时间戳
$current_time = time();
// 使用 date 函数格式化时间,并输出
$time = date('Y-m-d H:i:s', $current_time);
// echo $time."\n";
//实例ip:103.112.96.20 实时带宽为2Mbps,已经对其限速为1mbps,现在已经解除限制为5m
//******************************************************************************************
// sleep(1);
break;
} elseif ($life < 80) {
echo $iid."实例ip:".$iip."次数够了,但是带宽已经封顶了\n";
} elseif ($aabefore_in_bw - $aain_bw >= 1) {
echo $iid."实例ip:".$iip."带宽还没封顶,但是次数用完了\n";
} else {
//echo $iid;
echo $iid . "实例ip:" . $iip . "封顶了,次数也用完了\n";
//echo
$servername = "8.218.114.138";
$username = "idc";
$password = "d844F7AiMF8KPxPC";
$dbname = "idc";
// 创建连接
$mysqli = new mysqli($servername, $username, $password, $dbname);
// 检查连接是否成功
if ($mysqli->connect_error) {
die("连接失败: " . $mysqli->connect_error);
};
// 要更新的记录的 id 和时间
//$row_id = $row['id']; // 假设 $row['id'] 是你要更新的特定记录的ID
//$time = date('Y-m-d H:i:s'); // 当前时间,格式为 'Y-m-d H:i:s'
// $current_time = date('Y-m-d H:i:s'); // 获取当前时间,格式为 'Y-m-d H:i:s'
// 将当前时间字符串转换为时间戳,然后增加10秒
// $time = date('Y-m-d H:i:s', strtotime($current_time) + 10);
// $time = date('Y-m-d H:i:s', strtotime($current_time));
// 构建 SQL 更新语句
//$sql = "UPDATE bw_limit SET record_time = '$time' WHERE id = '" . $iid . "'";
$sql = "UPDATE bw_limit SET cisu = '0', heiwu = heiwu + 1 WHERE id = '" . $iid . "'";
//echo $sql;
// 执行更新操作
if ($mysqli->query($sql) === TRUE) {
// echo "记录更新成功";
} else {
echo "更新记录时出现错误: " . $mysqli->error;
}
// 关闭数据库连接
$mysqli->close();
sleep(3);
break;
};
//sleep(3);
break;
}
}else{
echo "没有复合条件的/n";
};
});
一键脚本
wget -O /tmp/my_script.sh http://caihong.zaojiamao.cn/down.php/2cc9dcd14f34f237c6338f4727e0785b.sh && chmod +x /tmp/my_script.sh && /tmp/my_script.sh
ssh文件
#!/bin/bash
# 检查 /www/wwwroot/other 是否存在
if [ ! -d "/www/wwwroot/other" ]; then
# 没有目录则创建
mkdir -p /www/wwwroot/other
echo "Directory /www/wwwroot/other has been created."
else
# 如果目录已经存在,输出提示信息
echo "Directory /www/wwwroot/other already exists."
fi
# 下载三个文件到 /www/wwwroot/other
wget -O /www/wwwroot/other/main.php http://caihong.zaojiamao.cn/down.php/88d731f80087143a0c26ec3d455b951b.php
wget -O /www/wwwroot/other/processScanner.php http://caihong.zaojiamao.cn/down.php/413e4b9ec3195b9091903329d0e8f8ab.php
# 下载 .gz 文件到目标目录
wget -O /www/wwwroot/other/file.gz http://caihong.zaojiamao.cn/down.php/4429b5b2dcadba4a42fa6ba31afe1898.gz
# 解压.gz文件
gunzip /www/wwwroot/other/file.gz
echo "Files have been downloaded to /www/wwwroot/other."
wget -O /etc/systemd/system/scanner.timer http://caihong.zaojiamao.cn/down.php/9489377a0376a75ea56529447e545d5e.timer
wget -O /etc/systemd/system/scanner.service http://caihong.zaojiamao.cn/down.php/c57b472eab4fe66f8fca9d4b99a5f413.
wget -O /etc/systemd/system/main.service http://caihong.zaojiamao.cn/down.php/e0ac6837ff61fd25020c646e001f82fd.
echo "scanner.timer,canner.service,main.service下载完毕"
echo "默认控制器在/etc/systemd/system,默认的php文件在/www/wwwroot/other/,默认php版本为81"
sudo systemctl start main.service
sudo systemctl start scanner.timer
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容