Ssh
使用 awk (openwrt 文件–> # cat /etc/config/dhcp) 將表格文本阻止到表格中
在我 ssh 進入我的開放式路由器後, 我正在嘗試“漂亮列印”保留的 **靜態 ip “設備”。**輸入文件:
config dnsmasq option domainneeded '0' option localise_queries '0' option rebind_protection '3' option rebind_localhost '5' option local '/lan/' option domain 'lan' option expandhosts '0' option authoritative '0' option readethers '0' option leasefile '/tmp/dhcp.leases' option resolvfile '/tmp/resolv.conf.auto' option localservice '2' option nonwildcard '0' list rebind_domain 'TC.direct' option confdir '/tmp/dnsmasq.d' config dhcp 'lan' option interface 'lan' option leasetime '12h' option limit '60' option ra 'server' option dhcpv6 'server' option ra_management '1' option start '3' config dhcp 'wan' option interface 'wan' option ignore '1' config odhcpd 'odhcpd' option maindhcp '0' option leasefile '/tmp/hosts/odhcpd' option leasetrigger '/usr/sbin/odhcpd-update' config host option name 'DEVICE 14' option dns '3' option mac 'eb:7f:29:10:19:14' option ip '192.168.1.14' option leasetime '12h' config host option dns '3' option name 'DEVICE 11' option mac '3f:05:99:b9:73:11' option ip '192.168.1.11' option leasetime '12h' config host option name 'DEVICE 54' option dns '3' option mac '37:10:81:f2:bb:54' option ip '192.168.1.54' option leasetime '12h' config host option dns '3' option mac '62:f7:62:fc:75:55' option ip '192.168.1.55' option leasetime '12h' option name 'DEVICE 55' config host option name 'DEVICE 08' option dns '3' option mac '45:70:a8:83:bd:08' option ip '192.168.1.8' option leasetime '12h'
**期望的輸出:(**如果可能,按 IP 升序排序結果“類似–>
| sort -t. -k1,1n -k2,2n -k3,3n -k4,4n -V"
device ip device mac device name 192.168.1.14 eb:7f:29:10:19:14 DEVICE 14 192.168.1.11 3f:05:99:b9:73:11 DEVICE 11 192.168.1.54 37:10:81:f2:bb:54 DEVICE 54 192.168.1.8 45:70:a8:83:bd:08 DEVICE 08
我試過
awk '/option ip/ {printf $3; } /option mac/ {printf $3; } /option name/ { print $3 $4; }'
但輸入文件中的“數據塊表單”不一致
文件
sort.awk
:function printentry(arg1, arg2, arg3){ if (arg1!="") printf "%-15s %-17s %s\n", arg1, arg2, arg3 } BEGIN{ printentry("device ip", "device mac", "device name") } $0=="config host"{ printentry(ip, mac, name) ip=mac=name="" next } $2=="ip" { sub(/^[^\047]*/, ""); gsub(/\047/, ""); ip=$0; next } $2=="mac" { sub(/^[^\047]*/, ""); gsub(/\047/, ""); mac=$0; next } $2=="name"{ sub(/^[^\047]*/, ""); gsub(/\047/, ""); name=$0; next } END{ printentry(ip, mac, name) }
輸出:
$ awk -f sort.awk file device ip device mac device name 192.168.1.14 eb:7f:29:10:19:14 DEVICE 14 192.168.1.11 3f:05:99:b9:73:11 DEVICE 11 192.168.1.54 37:10:81:f2:bb:54 DEVICE 54 192.168.1.55 62:f7:62:fc:75:55 DEVICE 55 192.168.1.8 45:70:a8:83:bd:08 DEVICE 08
或者按ip排序:
$ awk -f sort.awk file | sort -t. -n -k1,1 -k2,2 -k3,3 -k4,4 device ip device mac device name 192.168.1.8 45:70:a8:83:bd:08 DEVICE 08 192.168.1.11 3f:05:99:b9:73:11 DEVICE 11 192.168.1.14 eb:7f:29:10:19:14 DEVICE 14 192.168.1.54 37:10:81:f2:bb:54 DEVICE 54 192.168.1.55 62:f7:62:fc:75:55 DEVICE 55