I am trying a little ucode -just for fun.
I would like to execute ping -c 3 127.0.0.1 and capture the output to an variable.
I cannot use system(command, timeoutopt) → {number} since it only outputs exit code.
You need to use a ubus.call
to execute external programs. Something like this:
#!/usr/bin/ucode -S
import * as mod_ubus from "ubus";
let ubus = mod_ubus.connect();
if (! ubus) {
warn(`Unable to connect to ubus: ${mod_ubus.error()}\n`);
exit(1);
}
let cmd = {
command: "/bin/ping",
params: [ "-c4", "google.com" ],
};
let data = ubus.call("file", "exec", cmd);
if (data?.code == 0) {
printf("stdout = %s\n", data.stdout);
}
else {
printf("Ping failed: %s\n", data);
}
ubus.disconnect();
exit(0);
Thank you. Just what I needed.
Where do I find the documentation for ubus? It is not in https://ucode.mein.io/
Oh, now you're asking too much... Documentation?
I don't think there is any for the ubus
module. I learned from seeing a ubus call somewhere (fw4
code???), then dug into the ucode source, starting around here: https://github.com/jow-/ucode/blob/master/lib/ubus.c#L1994
I listed various ubus calls and translated them into the ucode version. There's some pretty useful stuff just in the ubus file
object.
$ ubus -v list file
'file' @b0e6aa85
"read":{"path":"String","base64":"Boolean","ubus_rpc_session":"String"}
"write":{"path":"String","data":"String","append":"Boolean","mode":"Integer","base64":"Boolean","ubus_rpc_session":"String"}
"list":{"path":"String","ubus_rpc_session":"String"}
"stat":{"path":"String","ubus_rpc_session":"String"}
"md5":{"path":"String","ubus_rpc_session":"String"}
"remove":{"path":"String","ubus_rpc_session":"String"}
"exec":{"command":"String","params":"Array","env":"Table","ubus_rpc_session":"String"}
Another good (undocumented) one is the ucode-mod-uclient
for doing wget-like stuff, which is how I do web queries in owut
, although it's new for 24.10 and not available in earlier versions.
Thank you. Now I have an idea of what I could find in ubus.
You might also poke through owut, it uses ubus in several places: