I would like to find out
brand
model
openwrt-version
of a given ip-address of an openwrt-device using my linux box.
Actually my pc uses bash, but openwrt ash.
The pc can connect to the openwrt-device with keys and without a password.
I can query this when I connect to the openwrt device via ssh, but I want to do this using a script in my ubuntu-box.
the shell is not important, characteristic openwrt sysinfo is like this
ssh root@openwrt.lan ubus call system board
2 Likes
$ ssh root@192.168.178.22 ubus call system board | grep hostname | cut -f4 -d"\""
WSM20-K1
That's fine, but how can I do it this way:
var=$(ssh root@192.168.178.22 ubus call system board)
and then
$ echo $var
{ "kernel": "6.6.86", "hostname": "WSM20-K1", "system": "MediaTek MT7621 ver:1 eco:3", "model": "Zyxel WSM20", "board_name": "zyxel,wsm20", "rootfs_type": "squashfs", "release": { "distribution": "OpenWrt", "version": "24.10.1", "revision": "r28597-0425664679", "target": "ramips/mt7621", "description": "OpenWrt 24.10.1 r28597-0425664679", "builddate": "1744562312" } }
Obviously it is not possible to select a line with grep.
ubus call system board
output is in JSON format, so you will need to use a tool like jq
to extract the data you need. For example: echo $var | jq .hostname
6 Likes
For comparison:
ssh root@192.168.178.22 ubus call system board
{
"kernel": "6.6.86",
"hostname": "WSM20-K1",
"system": "MediaTek MT7621 ver:1 eco:3",
"model": "Zyxel WSM20",
"board_name": "zyxel,wsm20",
"rootfs_type": "squashfs",
"release": {
"distribution": "OpenWrt",
"version": "24.10.1",
"revision": "r28597-0425664679",
"target": "ramips/mt7621",
"description": "OpenWrt 24.10.1 r28597-0425664679",
"builddate": "1744562312"
}
}
For script:
var=$(ssh root@192.168.178.22 ubus call system board)
openwrt_hostname=$(echo $var | jq .hostname | cut -f2 -d"\"" | tr '[:upper:]' '[:lower:]')
openwrt_version=$(echo $var | jq .release | grep version | cut -f4 -d"\"" |sed -e 's/\./-/g')
uniq_openwrt_dir="$openwrt_hostname""_""$openwrt_version"
$ echo $uniq_openwrt_dir
wsm20-k1_24-10-1
goal is to create a unique directory name for every openwrt device, where the configuration is synced with rsync.
The above code works, but maybe this can be done better.
The ip_address will be a variable in the script.
Most of the time you don't need other tools like grep|cut|sed:
$ echo $var | jq -r .release.version
24.10.1
2 Likes