编译Openwrt jq

上文中提到我要在kk-sp3上解析json发现自己拿shell正则来人肉挫实在不雅,于是搜了一番轮子,嗯,找到这个神器jq 简直相见恨晚

然而在路由器上没有现成的ipk供使用, 所以只能自己撸一发了

1. 下载 openwrt SDK

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

2. 一些依赖

我用的是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

3. 配置menuconfig

好心人在github上共享了一份openwrtfeeds配置, 其实就是个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
make menuconfig

界面如下

 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保存退出

4. 编译之

1
make package/jq/compile V=99 -j4

5. ipk在哪?

ipk在此: sdk/bin/ar71xx/packages

可能会有一点点编译错误, 不过最终ipk是成功编译完成的, 所以可以无视掉

6. 我是好人

嗯, 刚才给的那个githubrepo中已经有我打包好的一个ipk安装包

随便怎么样上传到路由器/kk-sp3上就可以安装了

1
opkg install jq_1.4-1_ar71xx.ipk

7. 测试一下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
root@OpenWrt:~# jq . /www/switches.json
{
  "switches": [
    {
      "DisplayName": "Switch 1",
      "ip": "192.168.137.115"
    }
  ]
}
root@OpenWrt:~# jq '.switches[0].ip' /www/switches.json
"192.168.137.115"
root@OpenWrt:~# jq -r '.switches[0].ip' /www/switches.json
192.168.137.115

8. 一个改IP的脚本

 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

加载评论