上文中提到我要在kk-sp3
上解析json发现自己拿shell正则来人肉挫实在不雅,于是搜了一番轮子,嗯,找到这个神器jq 简直相见恨晚
然而在路由器上没有现成的ipk
供使用, 所以只能自己撸一发了
kk-sp3
芯片是ar71xx
OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2
解压之
1
2
|
tar jxf OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2
mv OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2 sdk
|
我用的是centOS
, 年代久远, 所以需要的依赖基本都满足, 个别不满足的看error log
也很容易搞定, 这里贴个ubuntu
的
1
|
apt-get update && apt-get install gcc g++ binutils patch bzip2 flex bison make autoconf gettext texinfo unzip sharutils libncurses5-dev ncurses-term zlib1g-dev gawk git ccache asciidoc
|
好心人在github
上共享了一份openwrt
的feeds
配置, 其实就是个makefile
文件, 不客气的收下之
1
|
git clone https://github.com/ferstar/openwrt-myfeeds.git hello
|
把里面的jq
文件夹复制到sdk/package
目录下
1
|
cp -r hello/jq sdk/package
|
再把里面的jq-1.4.tar.gz
复制到sdk/dl
目录下
1
|
cp hello/jq-1.4.tar.gz sdk/dl
|
进入配置界面
界面如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
.config - Linux Kernel Configuration
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
lqqqqqqqqqqqqqqqqqqqqqq Linux Kernel Configuration qqqqqqqqqqqqqqqqqqqqqqqk
x Arrow keys navigate the menu. <Enter> selects submenus --->. x
x Highlighted letters are hotkeys. Pressing <Y> includes, <N> excludes, x
x <M> modularizes features. Press <Esc><Esc> to exit, <?> for Help, </> x
x for Search. Legend: [*] built-in [ ] excluded <M> module < > x
x lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk x
x x [*] Image configuration ---> x x
x x Libraries ---> x x
x x Utilities ---> x x
x mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqx x
tqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqu
x <Select> < Exit > < Help > < Save > < Load > x
mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj
|
空格选中jq
,然后tab
键切到Save
保存退出
1
|
make package/jq/compile V=99 -j4
|
ipk在此: sdk/bin/ar71xx/packages
可能会有一点点编译错误, 不过最终ipk是成功编译完成的, 所以可以无视掉
嗯, 刚才给的那个github
repo中已经有我打包好的一个ipk
安装包
随便怎么样上传到路由器/kk-sp3上就可以安装了
1
|
opkg install jq_1.4-1_ar71xx.ipk
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[email protected]:~# jq . /www/switches.json
{
"switches": [
{
"DisplayName": "Switch 1",
"ip": "192.168.137.115"
}
]
}
[email protected]:~# jq '.switches[0].ip' /www/switches.json
"192.168.137.115"
[email protected]:~# jq -r '.switches[0].ip' /www/switches.json
192.168.137.115
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#!/bin/sh
# 把我扔在cron中就可以了
# crontab -e
# */5 * * * * /root/change_ip.sh
# 五分钟检查一次, 别忘了加执行权限
# by ferstar
PATH=/usr/bin:/sbin:/bin
SW_FILE=/www/switches.json
NOW_IP=$(ifconfig wlan0 | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | head -n 1)
OLD_IP=$(jq -r '.switches[0].ip' ${SW_FILE})
if [ ${NOW_IP} != ${OLD_IP} ]; then
echo "ip changes detected, I'll update the ${SW_FILE}"
jq --arg v ${NOW_IP} '.switches[0].ip=$v' ${SW_FILE} > /tmp/tmp.json
mv /tmp/tmp.json ${SW_FILE}
echo "change ip from ${OLD_IP} to ${NOW_IP}"
else
echo "nothing to be done"
fi
|
其中jq --arg v ${NOW_IP} '.switches[0].ip=$v' ${SW_FILE} > /tmp/tmp.json
这一步被坑的有点美, 特别记录下
本来想简单把${NOW_IP}
变量值传进去, 结果每次都传的是${NOW_IP}
变量名, 求助Google
发现人家官网写的很清楚, 传参需要加--arg
参数, 吐血...
--arg name value
:
This option passes a value to the jq program as a predefined variable. If you run jq with --arg foo bar
, then $foo
is available in the program and has the value "bar"
. Note that value
will be treated as a string, so --arg foo 123
will bind $foo
to "123"
.
Named arguments are also available to the jq program as $ARGS.named
.
via https://stedolan.github.io/jq/manual