我想了解文件中記錄的含義(由擴展名/proc/net/xt_recent/ip_list
創建),例如:recent
iptables
src=127.0.0.1 ttl: 128 last_seen: 4298627364 oldest_pkt: 3 4298623492, 4298625777, 4298627364
所有字段看起來都非常明顯並且last_look
看起來像時間戳。但它不是 UNIX 時間格式的時間戳記。轉換為 UNIX 時間等於 03/21/2106 18:19:24。顯然這不是「最後一次見面」的時間。
如何從last_seen
時間中提取正確的值?
謝謝。
更新
只是為了避免誤解:
$ date
Mon Jun 15 14:14:00 MSK 2015
答案1
這應該有效:
FILE=iplist #This is file name of recent module output. It may vary on your system (like iplist)
TICKS=$(grep CONFIG_HZ= /boot/config-$(uname -r)|awk -F= '{print $2}') # Get current ticks per sec
printit()
{
Len=`echo $1|wc -c`
Date=$DATE
Dot="."
Loop=`echo 50-$Len|bc`
loop=0
while [ $loop -le $Loop ]
do
loop=`echo $loop+1|bc`
Dot=`echo $Dot.`
done
echo "$1$Dot$DATE"
}
cat $FILE|while read LINE
do
IP=`echo $LINE|awk '{print $1}'|awk -F= {'print $2'}`
DATE=$(date -d@$(date +'%s'-$(echo \($(cat /proc/timer_list|grep -m1 -E '^jiffies'|cut -d" " -f2)-$(awk '{print $5}' $FILE)\)/$TICKS|bc)|bc))
printit $IP $DATE
done
您的範例有一個輸出:
127.0.0.1..........................................Пн. мая 18 14:24:40 OMST 2015
時區可能與您的區域設定不同
您也可以檢查https://stackoverflow.com/questions/2731463/converting-jiffies-to-milli-seconds
答案2
我寫了這個:
https://github.com/peppelinux/xt_recent_parser
輸出是這樣的:
python3 xt_recent_parser.py
XT_RECENT python parser
<[email protected]>
114.241.108.160, last seen: 2017-03-25 18:21:42 after 13 Connections
46.165.210.17, last seen: 2017-03-25 13:07:54 after 10 Connections
61.53.219.162, last seen: 2017-03-25 17:39:17 after 20 Connections
179.37.141.232, last seen: 2017-03-25 18:08:23 after 2 Connections
114.42.117.39, last seen: 2017-03-25 13:22:14 after 18 Connections
177.12.84.234, last seen: 2017-03-25 16:22:14 after 17 Connections
答案3
這是一些吉菲斯,這是一個內部內核變量,每 1/HZ 秒遞增一次。如 Maxiko 所示,您可以jiffies
從 取得目前值/proc/timer_list
。您通常可以HZ
從獲取/boot/config.<kernel-version>
,因此您應該能夠使用以下方法對該資料進行後處理:
#! /usr/bin/perl
use Time::HiRes qw(gettimeofday);
use POSIX;
my $kernel = (uname())[2];
open CONFIG, "<", "/boot/config-$kernel" or
die "Can't find kernel config file: $!";
my $hz;
while (<CONFIG>) {
if (/^CONFIG_HZ=(\d+)/) {
$hz = $1;
last;
}
}
close CONFIG;
die "Can't determine HZ" unless $hz;
open TIMERS, "<", "/proc/timer_list" or
die "Can't open /proc/timer_list: $!";
my $jiffies;
while (<TIMERS>) {
if (/^jiffies: (\d+)/) {
$jiffies = $1;
last;
}
}
close TIMERS;
die "Can't determine jiffies" unless $jiffies;
my ($seconds, $microseconds) = gettimeofday;
$seconds += $microseconds / 1e6;
while (<>) {
s{(?<=last_seen: )\d+|\d+(?=,|$)}{
my $t = $seconds + ($& - $jiffies) / $hz;
$& . strftime(" [%F %T", localtime($t)) .
sprintf(".%03d]", ($t * 1000 + 0.5) % 1000);
}ge;
print;
}
在您的示例和我的系統上(因此與您的系統具有不同的 jiffies)給出:
src=127.0.0.1 ttl: 128 last_seen: 4298627364 [2016-08-16 17:21:00.882] oldest_pkt: 3 4298623492 [2016-08-16 17:20:45.394], 4298625777 [2016-08-16 17:20:54.534], 4298627364 [2016-08-16 17:21:00.882]