我想了解/proc/net/xt_recent/ip_list文件(由recent的iptables扩展创build)中logging的含义,例如:
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时间提取正确的值?
谢谢。
UPDATE
只是为了避免误解:
$ date Mon Jun 15 14:14:00 MSK 2015
这应该工作:
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
这是一个jiffies ,这是一个内部的内核variables每增加1 / HZ秒。 如Maxiko所示,您可以从/proc/timer_list获取当前的jiffies值。 您通常可以从/boot/config.<kernel-version>获取HZ ,因此您应该能够使用类似于以下内容的后处理数据:
#! /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; }
在你的样本和我的系统上(和你的样子不一样)给出了:
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]
我写了这个:
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