我有一个/var/log/pihole.log
包含如下行的内容:
Nov 29 18:49:33 dnsmasq[13568]: query[AAAA] firebaselogging-pa.googleapis.com from 192.168.0.10
Nov 29 18:49:33 dnsmasq[13568]: query[A] firebaselogging-pa.googleapis.com from 192.168.0.10
Nov 29 18:49:41 dnsmasq[13568]: query[AAAA] app-measurement.com from 192.168.0.10
Nov 29 18:49:41 dnsmasq[13568]: query[A] app-measurement.com from 192.168.0.10
Nov 29 18:49:57 dnsmasq[13568]: query[AAAA] in.treasuredata.com from 192.168.0.10
Nov 29 18:49:57 dnsmasq[13568]: query[A] in.treasuredata.com from 192.168.0.10
arp -a
给出 MAC 地址和 IP 地址之间的匹配结果:
root@raspberrypi:/var/log# arp -n
Address HWtype HWaddress Flags Mask Iface
192.168.0.81 ether c8:5b:76:e1:3c:6f C wlan0
192.168.0.136 ether 14:ab:c5:09:de:81 C wlan0
192.168.0.207 ether 68:c6:3a:a2:f4:e1 C wlan0
192.168.0.213 ether 60:01:94:70:e9:9c C wlan0
192.168.0.67 ether 8c:fe:57:51:1d:51 C wlan0
我想生成一个新的实时pihole2.log
文件,最后使用 MAC 地址。这是我的脚本:
tail -f /var/log/pihole.log|grep "query" |grep -v "<name"| awk 'NR==1{print $0,"MIME_type"; next}
{ cmd = "/var/log/nico.sh " $8
cmd | getline result
close(cmd)
print $1,$2,$3,$6,$8, result }'
我用来nico.sh
执行arp
搜索。nico.sh
包含:
#!/bin/sh
param=$1
cmd="$(arp -n|awk '$1 == x {print $3}' x=$param)"
echo $cmd
pihole.log
显示附加行时脚本不会继续运行。
答案1
这行 Bash 命令对我有用:
tail -f /var/log/pihole.log | while read line ; do echo -n "$line" ; ip=$(echo "$line" | awk '{print $NF}') ; mac=$(arp -n | grep '^'$ip' ' | awk '{print $3}') ; [ "$mac" = "" ] && echo " xx:xx:xx:xx:xx:xx" || echo " $mac" ; done >> /var/log/pihole2.log
或以多行形式:
#!/bin/bash
tail -f /var/log/pihole.log | while read line ; do
echo -n "$line"
ip=$(echo "$line" | awk '{print $NF}')
mac=$(arp -n | grep '^'$ip' '| awk '{print $3}')
[ "$mac" = "" ] && echo " xx:xx:xx:xx:xx:xx" || echo " $mac"
done >> /var/log/pihole2.log