https://trungdungmedia.com/kiemtranhamang
----------------------------------------------------------------------------------
trên server 4g
----------------------------------------------------------------------------------
sudo apt-get update
sudo apt-get install geoip-bin
----------------------------------------------------------------------------------
tạo file nano /home/server4g/log_connections.sh
----------------------------------------------------------------------------------
#!/bin/bash
# Thư mục chứa file log và cache
LOGDIR="/home/server4g"
# File log duy nhất
LOGFILE="${LOGDIR}/connections.log"
# File cache lưu thông tin IP đã tra cứu để tránh gọi API nhiều lần
CACHEFILE="${LOGDIR}/ip_cache.txt"
# --- Kiểm tra thư mục và các công cụ cần thiết ---
[ -d "$LOGDIR" ] || mkdir -p "$LOGDIR"
[ -f "$CACHEFILE" ] || touch "$CACHEFILE"
command -v curl >/dev/null 2>&1 || { echo "curl chưa cài đặt!"; exit 1; }
command -v jq >/dev/null 2>&1 || { echo "jq chưa cài đặt!"; exit 1; }
# --- Đảm bảo chỉ có 1 file log duy nhất ---
# Nếu muốn luôn ghi đè (tạo log mới mỗi lần chạy), bỏ comment dòng dưới:
# > "$LOGFILE"
# Nếu muốn ghi nối tiếp vào file log, để nguyên.
# --- Hàm tra cứu cache ---
lookup_cache() {
local ip="$1"
grep "^${ip}|" "$CACHEFILE" 2>/dev/null | head -n1
}
# --- Hàm ghi cache ---
write_cache() {
local ip="$1"
local country="$2"
local region="$3"
local city="$4"
local isp="$5"
echo "${ip}|${country}|${region}|${city}|${isp}" >> "$CACHEFILE"
}
# --- Bắt đầu ghi log ---
{
echo "=== $(date '+%Y-%m-%d %H:%M:%S') ==="
# Lấy danh sách kết nối TCP đang hoạt động với các cổng 65101, 65280, 65228, 65248
ss -ntp | grep -E ':(65101|65280|65228|65248)\b' | grep -oP '(\d{1,3}\.){3}\d{1,3}' | sort | uniq -c | sort -nr | while read count ip; do
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
cached=$(lookup_cache "$ip")
if [ -n "$cached" ]; then
IFS='|' read -r country region city isp <<< "$(echo "$cached" | cut -d'|' -f2-)"
else
info=$(curl -s "http://ip-api.com/json/${ip}?fields=status,country,regionName,city,isp")
status=$(echo "$info" | jq -r '.status')
if [ "$status" == "success" ]; then
country=$(echo "$info" | jq -r '.country')
region=$(echo "$info" | jq -r '.regionName')
city=$(echo "$info" | jq -r '.city')
isp=$(echo "$info" | jq -r '.isp')
write_cache "$ip" "$country" "$region" "$city" "$isp"
else
country="Unknown"; region="Unknown"; city="Unknown"; isp="Unknown"
fi
fi
echo "${count} ${ip} | Time: ${timestamp} | Country: ${country} | Region: ${region} | City: ${city} | ISP: ${isp}"
done
} >> "$LOGFILE"
----------------------------------------------------------------------------------
mở crontab -e thêm vào
----------------------------------------------------------------------------------
*/5 * * * * /home/server4g/log_connections.sh
==================================================================================
==================================================================================
==================================================================================
tạo kết nối SSH trên máy WEB
----------------------------------------------------------------------------------
ssh-keygen -t rsa -b 4096
ssh-copy-id -p 65222 ten_server_4g@ip_server_4g_192.168.14.238
ssh -p 65222 ten_server_4g@ip_server_4g_192.168.14.238 (thử kết nối)
----------------------------------------------------------------------------------
tạo file copy_log.sh
#!/bin/bash
REMOTE_USER="ten_server_4g"
REMOTE_IP="ip_server_4g_192.168.14.238"
REMOTE_PORT=65222
REMOTE_PATH="/home/server4g/connections_$(date +%Y%m%d).log"
LOCAL_DIR="/home/your_user_sv02/logs"
# Tạo thư mục local nếu chưa có
mkdir -p "$LOCAL_DIR"
# Copy file log từ remote về local, đổi tên file để tránh ghi đè (có thể giữ nguyên tên nếu muốn)
scp -P $REMOTE_PORT "$REMOTE_USER@$REMOTE_IP:$REMOTE_PATH" "$LOCAL_DIR/"
----------------------------------------------------------------------------------
chmod +x /home/your_user/copy_log.sh
----------------------------------------------------------------------------------
crontab -e
*/10 * * * * /home/sv02/copy_log.sh
----------------------------------------------------------------------------------
tạo file kiemtranhamang.php bỏ vào web
----------------------------------------------------------------------------------
<?php
$logfile = "/home/sv02/logs/connections.log";
if (!file_exists($logfile)) {
echo "Log file không tồn tại.";
exit;
}
$content = file_get_contents($logfile);
// Chèn CSS trực tiếp vào đầu file (nên đặt trước khi xuất bảng)
echo <<<STYLE
<style>
body {
background: linear-gradient(135deg, #184d47 0%, #a47149 100%);
color: #fff;
font-family: 'Segoe UI', Arial, sans-serif;
margin: 0;
padding: 30px;
}
table {
width: 95%;
margin: 0 auto 30px auto;
border-collapse: separate;
border-spacing: 0;
background: rgba(22, 34, 47, 0.92);
border-radius: 12px;
box-shadow: 0 6px 32px 0 #1b2a2f66;
overflow: hidden;
}
thead tr {
background: linear-gradient(90deg, #258b83, #b17f5b 80%);
color: #fff;
text-align: center;
font-size: 1.1em;
letter-spacing: 0.03em;
}
th, td {
padding: 14px 12px;
border-bottom: 1px solid #334e4a;
}
th {
text-shadow: 0 1px 4px #0008;
font-weight: 700;
}
tbody tr {
transition: background 0.18s;
}
tbody tr:nth-child(odd) {
background: rgba(41, 75, 88, 0.6);
}
tbody tr:nth-child(even) {
background: rgba(120, 75, 38, 0.48);
}
tbody tr:hover {
background: #2f564f;
}
td {
text-align: center;
font-size: 1em;
}
::-webkit-scrollbar {
width: 8px;
background: #436e66;
}
::-webkit-scrollbar-thumb {
background: #a47149;
border-radius: 6px;
}
</style>
STYLE;
// Chuyển đổi dòng log thành mảng
$lines = explode("\n", trim($content));
// Đảo ngược mảng để thời gian mới nhất lên trên
$lines = array_reverse($lines);
// Tạo bảng HTML để hiển thị
echo "<table border='1' cellpadding='5' cellspacing='0'>";
echo "<thead><tr>
<th>Số lượng</th>
<th>IP</th>
<th>Thời gian kết nối</th>
<th>Quốc gia</th>
<th>Khu vực</th>
<th>Thành phố</th>
<th>Nhà mạng (ISP)</th>
</tr></thead>";
echo "<tbody>";
foreach ($lines as $line) {
// Bỏ qua dòng trống hoặc dòng không đúng định dạng
if (trim($line) == "") continue;
// Mẫu regex mới có thêm trường Time
$pattern = '/^(\d+)\s+(\d+\.\d+\.\d+\.\d+)\s+\|\s+Time:\s*([^|]+)\s+\|\s+Country:\s*([^|]+)\s+\|\s+Region:\s*([^|]+)\s+\|\s+City:\s*([^|]+)\s+\|\s+ISP:\s*(.+)$/';
if (preg_match($pattern, $line, $matches)) {
echo "<tr>";
echo "<td>" . htmlspecialchars($matches[1]) . "</td>";
echo "<td>" . htmlspecialchars($matches[2]) . "</td>";
echo "<td>" . htmlspecialchars($matches[3]) . "</td>"; // Thời gian kết nối
echo "<td>" . htmlspecialchars($matches[4]) . "</td>";
echo "<td>" . htmlspecialchars($matches[5]) . "</td>";
echo "<td>" . htmlspecialchars($matches[6]) . "</td>";
echo "<td>" . htmlspecialchars($matches[7]) . "</td>";
echo "</tr>";
}
}
echo "</tbody></table>";
?>
----------------------------------------------------------------------------------
chỉnh file .htaccess bỏ tiền tố .php
RewriteEngine On
# bỏ .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
# bỏ .html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
----------------------------------------------------------------------------------
2025-05-17