记录基于 RPC手机APP接口使用情况,含请求方法与实例。注意:具体支持的方法可能因 OpenWrt 版本和已安装的软件包而异,建议先通过 listMethods
查询可用接口。
1. 认证模块 (auth)
URL: http://<ROUTER_IP>/cgi-bin/luci/rpc/auth
1.1 login
-
功能: 用户登录获取会话 ID (sid)
-
参数:
[username, password]
-
请求示例:
curl -X POST http://192.168.10.1/cgi-bin/luci/rpc/auth \
-H "Content-Type: application/json" \
-d '{
"method": "login",
"params": ["root", "admin"],
"id": 1
}'
-
响应示例:
{ "id": 1, "result": "a1b2c3d4e5", "error": null }
1.2 logout
-
功能: 注销当前会话
-
参数:
[sid]
-
请求示例:
curl -X POST http://192.168.1.1/cgi-bin/luci/rpc/auth \
-H "Content-Type: application/json" \
-d '{
"method": "logout",
"params": ["a1b2c3d4e5"],
"id": 2
}'
-
响应示例:
{ "id": 2, "result": true, "error": null }
2. 系统模块 (sys)
URL: http://<ROUTER_IP>/cgi-bin/luci/rpc/sys
2.1 exec
-
功能: 执行 Shell 命令
-
参数:
[command]
-
请求示例:
curl -X POST http://192.168.1.1/cgi-bin/luci/rpc/sys \
-H "Content-Type: application/json" \
-d '{
"method": "exec",
"params": ["ls /etc/config"],
"id": 3,
"sid": "a1b2c3d4e5"
}' -
响应示例:
{
"id": 3,
"result": "appfilter\nautoreboot\ncloud_manage\nddns\ndhcp\ndropbear\neqos\nfirewall\nfstab\nluci\nmdadm\nmicrosocks\nmtkhqos\nnetwork\nnlbwmon\nntpclient\nphicinet\npppoe\nrpcd\nsmartdns\nsystem\ntimecontrol\nubootenv\nucitrack\nuhttpd\nupnpd\nuser_info\nwireless\n",
"error": null
}
2.2 reboot
-
功能: 重启设备
-
参数:
[]
(无参数) -
请求示例:
curl -X POST http://192.168.1.1/cgi-bin/luci/rpc/sys \
-H "Content-Type: application/json" \
-d '{
"method": "reboot",
"params": [],
"id": 4,
"sid": "a1b2c3d4e5"
}' -
响应示例:
{ "id": 4, "result": true, "error": null }
2.3 getHostname
-
功能: 获取设备主机名
-
参数:
[]
-
请求示例:
curl -X POST http://192.168.1.1/cgi-bin/luci/rpc/sys \
-H "Content-Type: application/json" \
-d '{
"method": "getHostname",
"params": [],
"id": 5,
"sid": "a1b2c3d4e5"
}' -
响应示例:
{ "id": 5, "result": "OpenWrt", "error": null }
3. UCI 配置模块 (uci)
URL: http://<ROUTER_IP>/cgi-bin/luci/rpc/uci
3.1 get
-
功能: 读取配置项
-
参数:
[config, section, option]
-
请求示例 (获取无线 SSID):
curl -X POST http://192.168.1.1/cgi-bin/luci/rpc/uci \
-H "Content-Type: application/json" \
-d '{
"method": "get",
"params": ["wireless", "@wifi-iface[0]", "ssid"],
"id": 6,
"sid": "a1b2c3d4e5"
}' -
响应示例:
{ "id": 6, "result": "MyWiFi", "error": null }
3.2 set
-
功能: 修改配置项
-
参数:
[config, section, option, value]
-
请求示例 (修改 SSID):
curl -X POST http://192.168.1.1/cgi-bin/luci/rpc/uci \
-H "Content-Type: application/json" \
-d '{
"method": "set",
"params": ["wireless", "@wifi-iface[0]", "ssid", "NewSSID"],
"id": 7,
"sid": "a1b2c3d4e5"
}' -
响应示例:
{ "id": 7, "result": true, "error": null }
3.3 commit
-
功能: 提交 UCI 配置更改
-
参数:
[config]
-
请求示例:
curl -X POST http://192.168.1.1/cgi-bin/luci/rpc/uci \
-H "Content-Type: application/json" \
-d '{
"method": "commit",
"params": ["wireless"],
"id": 8,
"sid": "a1b2c3d4e5"
}' -
响应示例:
{ "id": 8, "result": true, "error": null }
4. 文件系统模块 (fs)
URL: http://<ROUTER_IP>/cgi-bin/luci/rpc/fs
4.1 read
-
功能: 读取文件内容
-
参数:
[file_path]
-
请求示例:
curl -X POST http://192.168.1.1/cgi-bin/luci/rpc/fs \
-H "Content-Type: application/json" \
-d '{
"method": "read",
"params": ["/etc/config/network"],
"id": 9,
"sid": "a1b2c3d4e5"
}' -
响应示例:
{ "id": 9, "result": "config interface 'lan'\n\toption proto 'static'...", "error": null }
4.2 write
-
功能: 写入文件内容
-
参数:
[file_path, data]
-
请求示例:
curl -X POST http://192.168.1.1/cgi-bin/luci/rpc/fs \
-H "Content-Type: application/json" \
-d '{
"method": "write",
"params": ["/tmp/test.txt", "Hello, OpenWrt!"],
"id": 10,
"sid": "a1b2c3d4e5"
}' -
响应示例:
{ "id": 10, "result": true, "error": null }
5. 网络模块 (network)
需安装 luci-mod-network
插件
URL: http://<ROUTER_IP>/cgi-bin/luci/rpc/network
5.1 getWanStatus
-
功能: 获取 WAN 口状态
-
参数:
[]
-
请求示例:
curl -X POST http://192.168.1.1/cgi-bin/luci/rpc/network \
-H "Content-Type: application/json" \
-d '{
"method": "getWanStatus",
"params": [],
"id": 11,
"sid": "a1b2c3d4e5"
}' -
响应示例:
{
"id": 11,
"result": {
"ipaddr": "192.168.1.1",
"status": "connected",
"uptime": 3600
}
}
6. 动态方法发现
6.1 listMethods
-
功能: 列出所有可用 RPC 方法
-
参数:
[]
-
请求示例:
curl -X POST http://192.168.1.1/cgi-bin/luci/rpc/sys \
-H "Content-Type: application/json" \
-d '{
"method": "listMethods",
"params": [],
"id": 99,
"sid": "a1b2c3d4e5"
}' -
响应示例:
{ "id": 99, "result": ["login", "exec", "getHostname", "reboot", ...], "error": null }
注意事项
-
权限控制
-
修改配置或执行敏感操作需管理员权限(通过有效
sid
验证)。 -
示例中的
sid
需替换为实际登录后获取的值。
-
-
参数格式
-
字符串参数必须使用双引号。
-
数组参数需用方括号包裹(如
["param1", "param2"]
)。
-
-
错误处理
-
若请求错误,响应将包含
error
字段,例如:{ "id": 1, "result": null, "error": "Access denied" }
-
-
安全性
-
生产环境中建议使用 HTTPS 加密通信。
-
避免在代码中硬编码密码,改用环境变量或配置文件。
-
通过以上示例,您可以系统地调用 OpenWrt 的 LuCI RPC 接口。建议结合官方文档和实际环境测试,确保接口兼容性。