To integrate a Shell script into LuCI (the web interface for OpenWrt), you can create a custom LuCI application. This application will provide a web interface to run your Shell script. Here are the main steps to complete the integration:
Step 1: Create Custom LuCI Application Directory
- Create a controller file in the
/usr/lib/lua/luci/controller
directory, for example,myapp.lua
.
mkdir -p /usr/lib/lua/luci/controller/myapp
touch /usr/lib/lua/luci/controller/myapp.lua
- Create a model file directory for handling web forms in
/usr/lib/lua/luci/model/cbi/myapp
.
mkdir -p /usr/lib/lua/luci/model/cbi/myapp
- Create a view file directory for storing HTML templates in
/usr/lib/lua/luci/view/myapp
.
mkdir -p /usr/lib/lua/luci/view/myapp
Step 2: Write the Controller File
Define a controller in the /usr/lib/lua/luci/controller/myapp.lua
file that handles rendering the interface and processing requests.
module("luci.controller.myapp", package.seeall)
function index()
entry({"admin", "services", "myapp"}, firstchild(), _("My App"), 30).dependent = true
entry({"admin", "services", "myapp", "config"}, cbi("myapp/config"), _("Configuration"), 10)
entry({"admin", "services", "myapp", "run_script"}, call("action_run_script"), _("Run Script"), 20)
end
function action_run_script()
local result = luci.sys.exec("/etc/myapp/myscript.sh") -- Execute the script
luci.http.prepare_content("text/plain")
luci.http.write(result) -- Output the script result
end
Step 3: Write the Model File
Create the model file at /usr/lib/lua/luci/model/cbi/myapp/config.lua
for the configuration form interface:
local m, s, o
m = Map("myapp", "MyApp Configuration", "Configure your MyApp settings here.")
s = m:section(TypedSection, "settings", "Settings")
s.addremove = false
s.anonymous = true
o = s:option(Value, "option1", "Option 1")
o.default = "default_value"
return m
Step 4: Create the View Template (Optional)
In /usr/lib/lua/luci/view/myapp/
, create a template file run_script.htm
to display the status of the script execution.
<h2>Script Execution</h2>
<p>Running the script...</p>
<pre><%= result %></pre>
Step 5: Create the Script File
Place your Shell script in /etc/myapp/myscript.sh
and ensure it is executable.
#!/bin/sh
echo "Hello from my script!"
# Add your script logic here
Make sure to set the permissions:
chmod +x /etc/myapp/myscript.sh
Step 6: Configuration File (Optional)
Create a custom configuration file at /etc/config/myapp
if you need to read configurations in the script or model file:
config settings 'config'
option option1 'default_value'
Step 7: Reload LuCI
Restart LuCI on your OpenWrt router to apply the changes:
/etc/init.d/uhttpd restart
Verification
- Navigate to
Services > My App
in the LuCI web interface. - Configure parameters under the
Configuration
section (optional). - Click on
Run Script
to execute the script and view the result.