How to use system commands in LUA

How to use system commands in LUA

Lets assume we have some requirement to call Unix-commands on the click of a button by using LUA .

To achieve this follow these steps :
1. First check your Lua page is working or not ?
 > Run your lua page to ensure that there is no any error in this page.

2. Now we need to configure template file in Lua page.(or you can do it by lua codes also but it has limited CSS )
> For this we need first to create/implement template page in lua file.

Ex : lets assume you have one lua file name (details.lua) .
So first you need to create map in your lua page.
(for detail please refer this blog : http://helicaltech.com/use-lua-web-script/)

Now you configure your template(getData.htm) in lua file.

<%+cbi/valueheader%>
<input type=”button” value=”Set” style=”width:71px;” onclick=”<%-=pcdata(self:getData(section))-%>” />
<%+cbi/valuefooter%>

Now in your lua page (details.lua) , you need to write this function.

test1 = readData:option(Value, “_custom”,translate(“Get Data “),”help text “)
readData.nocreate = true
readData.widget = “checkbox”
readData.template = “cbi/getData”
function readData.getData(self,section)
ptr1 =”/usr/bin/runScript  start”
os.execute(ptr1)
end

By this way we can run any system-command as value of ptr1

like “ls -ltrh /usr/bin >>/tmp/fileDetails.txt” ….etc

and run on the OS indirectly via LUA page.

Pushpraj Kumar.

How to Use LUA as Web Script

Lua is commonly described as a “multi-paradigm” language, providing a small set of general features that can be extended to fit different problem types, rather than providing a more complex and rigid specification to match a single paradigm. Lua, for instance, does not contain explicit support for inheritance, but allows it to be implemented with metatables. Similarly, Lua allows programmers to implement namespaces, classes, and other related features using its single table implementation; first-class functions allow the employment of many techniques from functional programming; and full lexical scoping allows fine-grained information hiding to enforce the principle of least privilege.

lua base language is light – the full reference interpreter is only about 180 kB compiled– and easily adaptable to a broad range of applications.

Lua is a dynamically typed language intended for use as an extension or scripting language, and is compact enough to fit on a variety of host platforms.

It supports only a small number of atomic data structures such as

– Boolean values,

– Numbers (double-precision floating point by default), and

– Strings.

Typical data structures such as

– Arrays,

– Sets,

– Lists, and

– Records

can be represented using Lua’s single native data structure,

  •  the table, which is essentially a heterogeneous associative array.
Ex. a_table = {} -- Creates a new, empty table

NOTES--Lua programs are not interpreted directly from the textual Lua file, but are compiled into byte-code which is then run on the Lua virtual machine. The compilation process is typically transparent to the user and is performed during run-time, but it can be done offline in order to increase loading performance or reduce the memory footprint of the host environment by leaving out the compiler.

How  to Add Buttons in LUA and Handling their  Events…

Button1 = exSection:option(Button, “_button_click_event “,translate(“Button Section”))      Note:- exSection  is defined in a map, and button is defined in that exSection-section

Button1.title = translate(“CLICK  HERE TO PERFORM BUTTON-ACTION”)

Button1.inputtitle=translate(” START “)

Button1.width=80

Function Button1.write(self, section, value)        –Defining  button-click event function

luci.sys.call(‘/etc/init.d/test_daemon  start’)        –Firing a system-command   to start a script

end

How  to call Templates from LUA and calling LUA-function from  template

testTemplate = exSection:option(Value, “_custom”, translate(“Template Testing… “))

testTemplate.template = “Template-Testing”

Template-Testing.htm

<%+cbi/valueheader%>

<%-  …… LUA code which you want to execute in your lua page…..  %>

<input type=”text ” name=”cbi.read.testintvl” id=” cbi.read.testintvl ” value=”<%-=h_testlval-%>”></input>                           ——Your HTML Code  here h_testlval   value comes from lua page.

<%+cbi/valuefooter%>

Note:- In LUA page we called any input-type(EX.Button,TextBox..)From their name.

Function Definitions in lua page

function testTemplate.lastmodifiedtime(self, section)

local mdtime=st.mtime(“/usr/lib/test.txt”)

return os.date(‘%c’, mdtime)

end

How  to create MAP and SECTION in lua page

local m, s

m=Map (“testing”, translate (“TEST-APPLICATION CONFIGURATION “))

s = m: section (TypedSection, “testagent”, translate (“TESTAGENT”))

s.anonymous = true

Predefined Function of lua when using map.

I.            m.on_init=function(self)

———–Things which you want to execute before your lua page load———

End

II.            m.on_before_commit = function(self)

——Before commit———-

End

III.            m.on_after_commit=function(self)

—–After Commit———-

End

LUA Uses by different Organization in their Application.

  1. Adobe Photoshop Lightroom uses LUA for its user interface.
  2. Apache HTTP Server can use LUA anywhere in the request process (since version 2.3, via the core mod_lua module).
  3. Cisco uses LUA to implement Dynamic Access Policies within the Adaptive Security Appliance.
  4. iClone, a 3D real-time animation studio to create animation movies uses Lua in the controls of its new physic simulation.
  5. MySQL Proxy uses LUA for its extensions
  6. VLC media player uses LUA to provide scripting support.

Wikipedia since March 2013 LUA is used as a new templating language, and its template library is growing.

 

Pushpraj Kumar,

Helical IT Solutions