7951 questions

9404 answers

15104 comments

12979 members

0 votes
81 views 2 comments
by

I have a TRB140 running latest firmware TRB1_R_00.07.00, reset to factory default settings.  I need to configure it automatically via UBUS JSON-RPC.

I have tried the following Python script:

#!/usr/bin/env python3

import requests
import pprint

payload = {"jsonrpc":"2.0","id":1,"method":"call","params":["00000000000000000000000000000000","session","login",{"username":"admin","password":"admin01"}]}
url = "http://192.168.2.1/ubus"

returnMsg = requests.post(url,json = payload)
read = returnMsg.json()['result'][1]['ubus_rpc_session']

calls = {
    'payloadSimGet': {"jsonrpc":"2.0","id":1,"method":"call","params":[read,"uci","get",{"config":"simcard"}]},
    'payloadSimSet': {"jsonrpc":"2.0","id":1,"method":"call","params":[read,"uci","set",{"config":"simcard","type":"sim","values":{"auto_apn":"0","force_apn":"-1"}}]},
    'payloadSimVerify': {"jsonrpc":"2.0","id":1,"method":"call","params":[read,"uci","get",{"config":"simcard"}]},
    'commitSim': {"jsonrpc":"2.0","id":1,"method":"call","params":[read,"uci","commit",{"config":"simcard"}]},
    'reload': {"jsonrpc": "2.0", "id": 1, "method": "call", "params":[read, "file", "exec", {"command": "luci-reload"}]},
}

# From https://lxr.openwrt.org/source/ubus/ubusmsg.h#L118
ubus_msg_status = [
    'UBUS_STATUS_OK',
    'UBUS_STATUS_INVALID_COMMAND',
    'UBUS_STATUS_INVALID_ARGUMENT',
    'UBUS_STATUS_METHOD_NOT_FOUND',
    'UBUS_STATUS_NOT_FOUND',
    'UBUS_STATUS_NO_DATA',
    'UBUS_STATUS_PERMISSION_DENIED',
    'UBUS_STATUS_TIMEOUT',
    'UBUS_STATUS_NOT_SUPPORTED',
    'UBUS_STATUS_UNKNOWN_ERROR',
    'UBUS_STATUS_CONNECTION_FAILED',
]

for callName, callJson in calls.items():
    returnMsg = requests.post(url, json=callJson)
    print(f'-----{callName}-----')
    print(f'post: {callJson}')
    respJson = returnMsg.json()
    print(f'{returnMsg}: '+pprint.pformat(respJson))
    if 'result' in respJson:
        print(ubus_msg_status[respJson['result'][0]])

The output from the script looks like:

-----payloadSimGet-----
post: {'jsonrpc': '2.0', 'id': 1, 'method': 'call', 'params': ['277c05562036ec646542c1b52e562788', 'uci', 'get', {'config': 'simcard'}]}
<Response [200]>: {'id': 1,
 'jsonrpc': '2.0',
 'result': [0,
            {'values': {'cfg01aa0e': {'.anonymous': True,
                                      '.index': 0,
                                      '.name': 'cfg01aa0e',
                                      '.type': 'sim',
                                      'auto_apn': '1',
                                      'force_apn': '2540',
                                      'modem': '3-1',
                                      'position': '1',
                                      'primary': '1'}}}]}
UBUS_STATUS_OK
-----payloadSimSet-----
post: {'jsonrpc': '2.0', 'id': 1, 'method': 'call', 'params': ['277c05562036ec646542c1b52e562788', 'uci', 'set', {'config': 'simcard', 'type': 'sim', 'values': {'auto_apn': '0', 'force_apn': '-1'}}]}
<Response [200]>: {'id': 1, 'jsonrpc': '2.0', 'result': [0]}
UBUS_STATUS_OK
-----payloadSimVerify-----
post: {'jsonrpc': '2.0', 'id': 1, 'method': 'call', 'params': ['277c05562036ec646542c1b52e562788', 'uci', 'get', {'config': 'simcard'}]}
<Response [200]>: {'id': 1,
 'jsonrpc': '2.0',
 'result': [0,
            {'values': {'cfg01aa0e': {'.anonymous': True,
                                      '.index': 0,
                                      '.name': 'cfg01aa0e',
                                      '.type': 'sim',
                                      'auto_apn': '0',
                                      'force_apn': '-1',
                                      'modem': '3-1',
                                      'position': '1',
                                      'primary': '1'}}}]}
UBUS_STATUS_OK
-----commitSim-----
post: {'jsonrpc': '2.0', 'id': 1, 'method': 'call', 'params': ['277c05562036ec646542c1b52e562788', 'uci', 'commit', {'config': 'simcard'}]}
<Response [200]>: {'error': {'code': -32002, 'message': 'Access denied'},
 'id': 1,
 'jsonrpc': '2.0'}
-----reload-----
post: {'jsonrpc': '2.0', 'id': 1, 'method': 'call', 'params': ['277c05562036ec646542c1b52e562788', 'file', 'exec', {'command': 'luci-reload'}]}
<Response [200]>: {'id': 1, 'jsonrpc': '2.0', 'result': [4]}
UBUS_STATUS_NOT_FOUND

So the login, uci get and uci set calls appear to be working.

But it fails with an Access denied error for the uci commit.

It also fails with a UBUS_STATUS_NOT_FOUND for the luci-reload command.

by

When I try the same thing via ssh it seems to work except for the luci-reload, which is missing:

root@Teltonika-TRB140:~# uci show simcard
simcard.@sim[0]=sim
simcard.@sim[0].modem='3-1'
simcard.@sim[0].position='1'
simcard.@sim[0].primary='1'
simcard.@sim[0].auto_apn='1'
simcard.@sim[0].force_apn='2538'
root@Teltonika-TRB140:~# uci set simcard.@sim[0].auto_apn='0'
root@Teltonika-TRB140:~# uci set simcard.@sim[0].force_apn='-1'
root@Teltonika-TRB140:~# uci show simcard
simcard.@sim[0]=sim
simcard.@sim[0].modem='3-1'
simcard.@sim[0].position='1'
simcard.@sim[0].primary='1'
simcard.@sim[0].auto_apn='0'
simcard.@sim[0].force_apn='-1'
root@Teltonika-TRB140:~# uci commit simcard
root@Teltonika-TRB140:~# luci-reload
-ash: luci-reload: not found

1 Answer

+1 vote
by

Hi,

I'm not sure why is commit not working, but could you try to put it in another syntax like you put Luci-reload command?

And about luci-reload, it was removed since 07.00 as the software is now fully built on vuci. Please use reload_config from now on.

EB.

by

Thanks EB.  I tried your suggestions and got the following:

-----ucicmd-----
post: {'jsonrpc': '2.0', 'id': 1, 'method': 'call', 'params': ['be0f869b1b97f37ce58128b4e19e53cf', 'file', 'exec', {'command': 'uci'}]}
<Response [200]>: {'id': 1, 'jsonrpc': '2.0', 'result': [6]}
UBUS_STATUS_PERMISSION_DENIED
-----commitSim-----
post: {'jsonrpc': '2.0', 'id': 1, 'method': 'call', 'params': ['be0f869b1b97f37ce58128b4e19e53cf', 'file', 'exec', {'command': 'uci commit'}]}
<Response [200]>: {'id': 1, 'jsonrpc': '2.0', 'result': [4]}
UBUS_STATUS_NOT_FOUND
-----commitSim2-----
post: {'jsonrpc': '2.0', 'id': 1, 'method': 'call', 'params': ['be0f869b1b97f37ce58128b4e19e53cf', 'file', 'exec', {'command': ['uci', 'commit']}]}
<Response [200]>: {'id': 1, 'jsonrpc': '2.0', 'result': [2]}
UBUS_STATUS_INVALID_ARGUMENT
-----reload-----
post: {'jsonrpc': '2.0', 'id': 1, 'method': 'call', 'params': ['be0f869b1b97f37ce58128b4e19e53cf', 'file', 'exec', {'command': 'reload_config'}]}
<Response [200]>: {'id': 1, 'jsonrpc': '2.0', 'result': [6]}
UBUS_STATUS_PERMISSION_DENIED

Update: my local supplier has confirmed this is a firmware bug and has raised it with Teltonkia.