How to execute terminal commands from a webpage
Now, let’s say you can’t access the terminal or simply you just rather execute it from a web interface. In this article, I will show you how to execute terminal commands from a simple web page.
For example, in the image below you see how we execute $zv on a webpage:
This is mostly possible due to the XECUTE command in Caché ObjectSCript. This command takes a string as a parameter and tries to execute it. This is the command used from the webpage, to execute what is being passed from the left navigation menu.
This article contains three snippets of code. Each of which corresponds with a separate file. In total, there are three files:
1. Index.csp (A sample page with two frames):
<HTML>
<HEAD><TITLE> Sample App </TITLE></HEAD>
<FRAMESET border=1 frameSpacing=0 rows=* frameBorder=no cols=250,900*>
<FRAME name=Navigation src="leftMenu.csp" noResize>
<FRAME name=mainFrame src="execute.csp">
</FRAMESET>
</HTML>
2. leftMenu.csp (self-explanatory):
<HTML>
<HEAD><TITLE>Left Menu </TITLE><LINK REL="stylesheet" TYPE="text/css" HREF="" TITLE="Standard Style" ></HEAD>
<BODY>
<BR> <FONT SIZE=+1>Sample App</FONT><BR> Execute <BR><BR><BR>
<FORM ACTION="execute.csp" TARGET="mainFrame" METHOD="GET">
<INPUT TYPE=SUBMIT VALUE="Nothing" STYLE="width:200;" onclick="document.forms[0].command.value = '';" title="()"> <BR>
<INPUT TYPE=SUBMIT VALUE="Server Version" STYLE="width:200;" onclick="document.forms[0].command.value = 'w $zv';"> <BR>
<INPUT TYPE=SUBMIT VALUE="License Server" STYLE="width:200;" onclick="document.forms[0].command.value = 'Do $System.License.ShowServer()';"> <BR>
<INPUT TYPE=SUBMIT VALUE="w $System.Util.GetEnviron(p1)" STYLE="width:200;" onclick="document.forms[0].command.value = 'w $System.Util.GetEnviron';"> <BR>
<INPUT TYPE=HIDDEN NAME="command" VALUE="w $zv">
<BR><BR> Parameters (if needed)<BR>
P1 <INPUT TYPE=TEXT NAME="param1" VALUE=""><BR>
P2 <INPUT TYPE=TEXT NAME="param2" VALUE=""><BR>
P3 <INPUT TYPE=TEXT NAME="param3" VALUE=""><BR>
</FORM></BODY></HTML>
3. execute.csp (right-hand frame where we execute the code and redirect the output:
<HTML><HEAD><LINK REL="stylesheet" TYPE="text/css" HREF="" TITLE="Standard Style" ><TITLE>Execute </TITLE></HEAD>
<BODY>
<SCRIPT LANGUAGE=CACHE RUNAT=SERVER>set t1 = $piece($h,2)</SCRIPT>
<TABLE>
<TR><TD></TD><TD><BR>#($zdt($h,2))#</TD></TR>
<TR><TD></TD><TD>
<TEXTAREA COLS="110" ROWS="45">
<SCRIPT LANGUAGE=CACHE RUNAT=SERVER>
if %request.Get("command") '= "" {
set parens = 0
set cmd = %request.Get("command")
if %request.Get("param1") '= "" set cmd = cmd_"("_%request.Get("param1"), parens=1
if %request.Get("param2") '= "" set cmd = cmd_","_%request.Get("param2")
if %request.Get("param3") '= "" set cmd = cmd_","_%request.Get("param2")
if (parens=1) set cmd = cmd_")"
w "> "_cmd,!
}
if %request.Get("command") '= "" {
xecute cmd
}
</SCRIPT>
</TEXTAREA></TD></TR>
<TR><TD></TD><TD></TD></TR>
</TABLE></BODY></HTML>
You can also pass parameters if you need to. See example below.
Here is the source code.
Hope you find it useful !