Ash how to parse such output

root@AP3:~# ubus call hostapd.wlan2 rrm_nr_get_own
{
        "value": [
                "e2:e1:a9:7d:f8:aa",
                "newone",
                "e2e1a97df8aaef090000510b070603000b00"
        ]

i need to make it like this string "e2:e1:a9:7d:f8:aa","newone","e2e1a97df8aaef090000510b070603000b00"

i know how to grep and awk but this is diffrent

Try ubus -S

-S: Use simplified output (for scripts)

2 Likes

root@AP3:~# ubus -S call hostapd.wlan2 rrm_nr_get_own
{"value":["e2:e1:a9:7d:f8:aa","newone","e2e1a97df8aaef090000510b070603000b00"]}
root@AP3:~#

and then awk ?

That depends on what you want to achieve at the end. You may need to redirect the output to jsonfilter if you're interested in specific value(s).

nah , i just need output make as a string "e2:e1:a9:7d:f8:aa","newone","e2e1a97df8aaef090000510b070603000b00"
with quotas

ubus -S call hostapd.wlan2 rrm_nr_get_own | awk -F"[][]" {'print $2'}

3 Likes

sed, but for some reason I can't get it to strip using only one command, but then again, I suck at sed.

1st line removes 1st 10 chars, 2nd last two.

sed -r 's/.{10}//'
sed -r 's/.{2}$//'
2 Likes

Thanks a lot all works good
btw maybe you know how to scp with login and password ?

scp -r  /root/mykikamanagment/AP1/lists  root@192.168.222.101:/bin/mykika/

but it always ask for password and confirmation y/n

ubus -S call hostapd.wlan2 rrm_nr_get_own | sed -e 's/^.*:\[//' -e 's/\]}//'

2 Likes

i can understand awk but this sed looks like hell to me

Actually two sed expressions the first removes anything upto and including :[ the second just removes ]}

But I agree awk is simpler :slight_smile:

1 Like

Use key-based authentication instead.

2 Likes

It is already there

~# ll /usr/bin/awk
lrwxrwxrwx    1 root     root            17 Jun 21 22:32 /usr/bin/awk -> ../../bin/busybox*
3 Likes

Ah, the busybox one is already there, cool ...

Changed my reply.

2 Likes

what about sshpass ? it is only 5Kb

I see the question is already sufficiently answered with some "awk" magic, but when it comes to JSON strings I personally prefer using "jq".

https://manpages.ubuntu.com/manpages/xenial/man1/jq.1.html

My suggestion would be:

ubus -S call hostapd.wlan2 rrm_nr_get_own | jq .value | jq -r '@csv'

One could write it with just one jq call, but that's less readable to me:

ubus -S call hostapd.wlan2 rrm_nr_get_own | jq -r '.value | @csv'

The first jq call accesses the "value" on the root level (which does work with complex levels as well, acessing e.g. only the first element inside of the value property would be ".value[0]").

The second jq call outputs the array in CSV format, where values are usually wrapped in double qoutes and individual columns are separated by the comma sign.

This version doesn't care about ubus using the simplified "-S" result format or the more readable one. Both are valid JSON, so both can be interpreted and queried by jq.

I just find the json query way more understandable than the awk split pattern "[][]".

2 Likes

jsonfilter is installed as one of the standard packages on OpenWrt and is the tool of choice for parsing and presenting parts of json output. Just type jsonfilter -h and you'll get some examples that will show you how use it with ubus calls.

5 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.