[英]How to extract IP address ,MAC address and name from dhcp.lease file with a shell script?
I am trying to parse dhcp.lease file from /tmp/dhcp.lease in openwrt . 我正在尝试从openwrt中的/tmp/dhcp.lease解析dhcp.lease文件。
root@OpenWrt:/# cat /tmp/dhcp.leases
1568953482 70:B3:D5:14:D0:31 192.168.3.51 device1 01:70:B3:D5:14:D0:31
2867821468 38:B8:EB:10:00:22 192.168.5.93 device2 01:38:B8:EB:10:00:22
8984532872 00:01:0A:33:11:33 192.168.5.44 CISCOee 01:00:01:0A:33:11:33
Where, 2nd column - MAC address , 3rd column - IP address and 4th column- Name
I want to run a shell script to parse No of devices
, MAC address
, IP address
and Device Name
from this dhcp.lease list . 我想运行一个shell脚本来解析此dhcp.lease列表中
No of devices
, MAC address
, IP address
和Device Name
。
sample output : 样本输出:
if there is 3 device
list is present in dhpcp.lease
file , I want to print output like : 如果
dhpcp.lease
文件中存在3 device
列表,我想打印输出:
3
70:B3:D5:14:D0:31/192.168.3.51/device1
38:B8:EB:10:00:22/192.168.5.93/device2
00:01:0A:33:11:33/192.168.5.44/CISCOee
and if no device list found , it should return 如果找不到设备列表,则应返回
0
Can i do with simple file content iteration ? 我可以进行简单的文件内容迭代吗? or any fast method like sed/awk ?
还是像sed / awk这样的快速方法? Any sample code ?
任何示例代码?
awk
command may help you. awk
命令可能会帮助您。
$ awk -v OFS='\n' '$2 ~ /[0-9A-Z]:/{n=n+1;a[n]=$2"/"$3"/"$4} END{print n==""?0:n; for(i in a)print a[i]}' /tmp/dhcp.leases
Brief explanation, 简要说明,
$2
matched to regex [0-9A-Z]:
$2
匹配正则表达式[0-9A-Z]:
n
, and also save $2/$3/$3
to the array a
n
,还将$2/$3/$3
到数组a
n
and the value in the array a
in the end n
的计数和数组a
中的值 The previous awk statement seems over complicated to me. 以前的awk声明对我来说似乎过于复杂。 The following is simpler:
以下更简单:
awk '{ dat[NR]=$2"/"$3"/"$4 } END { print NR;for ( i=1;i<=NR;i++) { print dat[i] } }' /tmp/dhcp.lease
The leases file is in a fixed format with just the data required and the total count will be the number of records (NR variable). 租约文件采用固定格式,其中仅包含所需的数据,总计数将是记录数(NR变量)。 Read the data required into an array dat and then print NR and loop through the array printing each entry.
将所需的数据读取到数组数据中,然后打印NR并遍历数组,打印每个条目。
Besides awk
, you can use grep
with regex
and using a temporary file in where you'll save the matched output for further counting its lines and displaying it like this example: 除了
awk
之外,您还可以将grep
与regex
一起使用,并使用一个临时文件,在其中保存匹配的输出,以进一步计算其行数并像下面的示例一样显示它:
~$ cat /tmp/dhcp.leases | grep -oE "([0-9A-Z]{2}:){5}[0-9A-Z]{2}.*([0-9]{1,3}\.){3}[0-9]{1,3}.*[a-zA-Z]{3,}[0-9]+?" | tr ' ' '/' > /tmp/testFile
~$ cat /tmp/testFile | wc -l; cat /tmp/testFile
Output: 输出:
3
70:B3:D5:14:D0:31/192.168.3.51/device1
38:B8:EB:10:00:22/192.168.5.93/device2
00:01:0A:33:11:33/192.168.5.44/CISCOee
Explanation: 说明:
o
nly the matched (non-empty) parts of the matching line and use the E
xtended regexp see the man page . o
NLY匹配(非空)的匹配线的部分,并使用E
xtended正则表达式见手册页 。 /tmp/testFile
. /tmp/testFile
。 /tmp/testFile
using wc
and print its lines. wc
计数/tmp/testFile
的行并打印其行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.