【发布时间】:2018-01-08 01:23:01
【问题描述】:
大家好,我在我的 Linksys EA6500 路由器上运行 DD-WRT v.3.0,我正在运行以下脚本,以便让我访问当前连接到路由器的 WiFi MAC 地址:
echo "#!/bin/ash" > /tmp/getmac.sh
echo 'echo { > /tmp/www/list.html' >>/tmp/getmac.sh
echo "for i in \$(arp | awk '{print toupper(\$4)}'); do echo \$i, >> /tmp/www/list.html; done" >>/tmp/getmac.sh
echo 'echo } >> /tmp/www/list.html' >>/tmp/getmac.sh
chmod +x /tmp/getmac.sh
/tmp/getmac.sh
我可以访问http://192.168.1.1/user/list.html,它会显示当前连接到路由器的wifi MAC地址列表:
例子:
{ 01:81:18:3d:49:5e, 04:10:87:8c:47:9a, }
但是,我想对其进行修改,使其也包括 IP 地址 和 设备名称。
我在 DD-WRT 网站上找到了this,但是在运行命令并检查目录时,我在任何地方都看不到它。
# mkdir -p /tmp/www
while [ 1 ];
do
wl assoclist | awk '{print tolower($2)}' > /tmp/assocLIST
# echo "<meta http-equiv="refresh" content="10"><b>Hostnames and IP addresses of WLAN clients</b> (last update: $(date))<p>" > /tmp/www/wlan.html
while read assocLINE
do
dumpleases | awk '/'"$assocLINE"'/ {print "Hostname: " $1, "MAC: " $2, "IP: " $3}'
# echo "<br>";
done < /tmp/assocLIST # >> /tmp/www/wlan.html
sleep 10;
done;
我希望它像这样输出:
{
"data": [{
"IP": "192.168.1.55",
"MAC": "01:81:18:3d:49:5e",
"HOST": "DavidsAndroidPhone"
}, {
"IP": "192.168.1.79",
"MAC": "04:10:87:8c:47:9a",
"HOST": "BobsIphone"
}]
}
谁能帮我修改我发布的第一个脚本以包含 IP 和 NAME?
更新
当我在 PuTTYtel 中执行命令 arp 时,我得到以下信息:
DD-WRT login: root
Password:
==========================================================
___ ___ _ _____ ______ ____ ___
/ _ \/ _ \___| | /| / / _ \/_ __/ _ __|_ / / _ \
/ // / // /___/ |/ |/ / , _/ / / | |/ //_ <_/ // /
/____/____/ |__/|__/_/|_| /_/ |___/____(_)___/
DD-WRT v3.0
http://www.dd-wrt.com
==========================================================
BusyBox v1.24.1 (2016-03-07 05:09:22 CET) built-in shell (ash)
root@DD-WRT:~# arp
android-17af243062d3eb6b (192.168.1.144) at 00:ae:fa:4a:3a:4c [ether] on br0
所以目前我正在运行的脚本 (getmac.sh) 会查看这个并且只得到这个:
{ 00:ae:fa:4a:3a:4c, }
鉴于此,我如何修改脚本以在正确的 JSON 布局中获取更多我正在寻找的信息?
更新 2
好的,我这里有这段代码:
arp | awk 'BEGIN { print "{" } { print "MAC:" $4 ", IP:" $2 ", HOST:" $1} END { print "}" }'
输出如下:
{
MAC:00:ae:fa:4a:3a:4c, IP:(192.168.1.144), HOST:android-17af243062d3eb6b
}
鉴于上述情况,如何从 IP 中删除 ( 和 ) 并将其格式化为正确的 JSON 格式?我试过 awk -F'(' 但这似乎不起作用。
【问题讨论】:
-
呃,你为什么每次都生成脚本?无论如何,如果您只是发布生成的脚本,您的问题会更容易回答。
-
如果您取消注释这些行,来自站点的脚本将生成一个文件
/tmp/www/wlan.html。 -
... 虽然临时文件有点笨拙。如果没有来自
wl assoclist和getleases的示例输出,则不清楚它在做什么。 -
@tripleee Running wl assoclist 没有输出...
标签: linux bash shell cron dd-wrt