我想了解文件中记录的含义(由扩展/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]