OpenWrt Forum Archive

Topic: Accessing uci procedures with ubus over HTTP

The content of this topic has been archived on 30 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hello all,

Does anyone have an example of how to access uci procedures with ubus over HTTP using cURL?

I have the examples working on http://wiki.openwrt.org/doc/techref/ubus:

"$ curl -d '{ "jsonrpc": "2.0", "id": 1, "method": "call", "params": [ "00000000000000000000000000000000", "session", "login", { "username": "root", "password": "secret"  } ] }'  http://your.server.ip/ubus

{"jsonrpc":"2.0","id":1,"result":[0,{"ubus_rpc_session":"c1ed6c7b025d0caca723a816fa61b668","timeout":300,"expires":299,"acls":{"access-group":{"superuser":["read","write"],"unauthenticated":["read"]},"ubus":{"*":["*"],"session":["access","login"]},"uci":{"*":["read","write"]}},"data":{"username":"root"}}]}"

and

$ curl -d '{ "jsonrpc": "2.0", "id": 1, "method": "call", "params": [ "7cba69a942c0e9db1eb7982cd91f3a48", "file", "read", { "path": "/tmp/hello.karl" } ] }'  http://eg-134867.local/ubus
{"jsonrpc":"2.0","id":1,"result":[0,{"data":"this is the contents of a file\n"}]}

so I know everything is working. I'm not sure of the syntax for uci. I have tried using the documentation but no luck.

Thanks
Eric

All examples below assume the frame below:

{ "jsonrpc": "2.0", "id": 1, "method": "call", "params": [ $sessid, "uci", $method, $params ] }

I'll just list the values for $method and $params.

List all existing configs:

$method = "configs"
$params = { }

Get the complete network config:

$method = "get"
$params = { "config": "network" }

Get all sections of type interface:

$method = "get"
$params = { "config": "network", "type": "interface" }

Get all sections of any type that have proto set to dhcp:

$method = "get"
$params = { "config": "network", "match": { "proto": "dhcp" } }

Get a specific option of a named section:

$method = "get"
$params = { "config": "network", "section": "wan", "option": "ifname" }

Get an entire named section:

$method = "get"
$params = { "config": "network", "section": "wan" }

Get named or unnamed sections using extended lookup format (@type[idx] notation):

$method = "get"
$params = { "config": "network", "section": "@interface[0]" }

Add a new anonymous section:

$method = "add"
$params = { "config": "network", "type": "route", "values": { "interface": "wan", "target": "1.2.3.0/24", "gateway": "5.6.7.8" } }

Add a new named section:

$method = "add"
$params = { "config": "network", "name": "wwan", "type": "interface", "values": { "proto": "dhcp" } }

Set value in a named section:

$method = "set"
$params = { "config": "network", "name": "wan", "values": { "proto": "static", "ip6addr": "fdca:1234::1/64" } }

Set values in all sections of given type:

$method = "set"
$params = { "config": "network", "type": "interface", "values": { "auto": 0 } }

Set values in all matching sections (set option auto 0 on all interfaces with proto dhcp):

$method = "set"
$params = { "config": "network", "type": "interface", "match": { "proto": "dhcp" }, "values": { "auto": 0 } }

Delete named section:

$method = "delete"
$params = { "config": "network", "section": "wan" }

Delete two options in a section:

$method = "delete"
$params = { "config": "network", "section": "wan", "options": [ "auto", "ip6addr" ] }

Delete all sections of a given type:

$method = "delete"
$params = { "config": "network", "type": "route" }

Delete all interfaces with proto "dhcpv6":

$method = "delete"
$params = { "config": "network", "type": "interface", "match": { "proto": "dhcpv6" } }

Rename a section (config interface wan -> config interface wan_bak):

$method = "rename"
$params = { "config": "network", "section": "wan", "name": "wan_bak" }

Rename an option (option ifname -> option ifname_bak):

$method = "rename"
$params = { "config": "network", "section": "wan", "option": "ifname", "name": "ifname_bak" }

Reorder sections:

$method = "order"
$params = { "config": "network", "sections": [ "loopback", "lan", "wan", "wan6", "@route[0]", "@route[1]" ] }

Dump all staged changes:

$method = "changes";
$params = { }

Dump all staged changes in network:

$method = "changes";
$params = { "config": "network" }

Same for revert + commit.

See also "ubus -v list uci" for the method signatures. More detailled comments in http://nbd.name/gitweb.cgi?p=luci2/rpcd … =HEAD#l329 downwards.

If you run ubus locally on the cli you can also bypass the extra JSON-RPC frame + login/session id etc., this is useful for trying your commands.

root@jj:~# ubus call uci get '{ "config": "network", "match": { "proto": "dhcp" } }'
{
    "values": {
        "wan": {
            ".anonymous": false,
            ".type": "interface",
            ".name": "wan",
            ".index": 3,
            "ifname": "eth1",
            "proto": "dhcp"
        }
    }
}

Hi jow,

Your examples are exactly what I was looking for. I have spent the last 2 days searching the internet for anything with no luck.

Thank you very much!

ewalbridge

(Last edited by ewalbridge on 23 Sep 2014, 00:39)

Hi all,

I'm just trying this, following the exact steps as detailed in the ubus documentation ("Access to ubus over HTTP").

Yet what I get in return is the following error:

curl -d '{ "jsonrpc": "2.0", "id": 1, "method": "call", "params": [ "00000000000000000000000000000000", "session", "login", { "username": "root", "password": "*MY_PASSWORD*"  } ] }'  hxxp://my.router.ip/ubus
{"jsonrpc":"2.0","id":1,"error":{"code":-32000,"message":"Object not found"}}

What am I doing wrong here?

Any help much appreciated!
Robin

rebser wrote:

What am I doing wrong here?

Seems I was missing the steps
1.) install rpcd
2.) /etc/init.d/rpcd enable
3.) /etc/init.d/rpcd start

Now I'm at least one step further.

When I try to use ubus uci set command and then commit it dont do anything on the device..

curl -d '{ "jsonrpc": "2.0", "id": 1, "method": "call", "params": [ "00000000000000000000000000000000", "session", "login", { "username": "root", "password": secret } ] }'  -g link

curl -s -d '{"jsonrpc":"2.0","id":1,"method":"call","params":
["'$UBUS_SESSION'","uci","set",{"config":"'teste_wireless'","section":"'radio0'",
"values": { "disabled":1}}]}' -g link

curl -s -d '{"jsonrpc":"2.0","id":1,"method":"call","params":
["'$UBUS_SESSION'","uci","commit",{"config":"'teste_wireless'"}]}' -g
link

(Last edited by cmatos on 5 Jun 2017, 10:27)

The discussion might have continued from here.