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.