In the past couple of weeks I was working on Server Side execution of
Batch files while working on one of my projects. Since I am a QA
Automation Engineer, I have never worked on such task so it was the
first time I was working on some Server Side processing (I like
Challenges !!). In this post, we'll define the problem statement and the
approach I used (apart from others), to tackle and solve the problems I
faced during implementation.
Problem Statement :
We have a HTML page containing some text fields, the values of which are supposed to be passed to server, which will further execute batch files taking these passed values from client as parameters and return the output (HTML file generated) to the client.
Approach (Local Machine) :
Initially, the approach I followed was to create a static HTML page on local machine (before the requirement of Server popped up). I used ActiveXObject implementation on IE to execute the batch files on button click.
Works only on Internet Explorer :
3 parameters in WshShell.Run() :
Approach (Server) :
Since execution of Batch files using ActiveXObject will not work on Server, we need to use some Server Side Programming Language to execute Batch Files with required parameters passed using POST from client.
One option is to use C# program to take the input from client (using POST), convert the input string to required parameters to pass to Batch Files, convert this C# program to .exe (executable) file and specify this .exe filename in the action attribute of POST on client FORM (Web Page).
C# Program (On Server) : (filename - Console1.cs)
Conversion of C# Program to executable file (.exe) : (Execute on cmd)
HTML Source Program :
That's it !! Our HTML page is now ready to take required inputs and send to the server for further processing which will return some response in HTML format using Console1.exe
Notice the form tag attributes in the HTML source code. You can also specify the client-side validation using "onSubmit" attribute in FORM tag.
Note :
You need to configure your Web Server to allow the execution of CGI scripts before using the above code snippets, so make sure that CGI scripts execution is enabled on your Web Server.
Feel free to comment in case of any queries.
Happy Learning !!
Problem Statement :
We have a HTML page containing some text fields, the values of which are supposed to be passed to server, which will further execute batch files taking these passed values from client as parameters and return the output (HTML file generated) to the client.
Approach (Local Machine) :
Initially, the approach I followed was to create a static HTML page on local machine (before the requirement of Server popped up). I used ActiveXObject implementation on IE to execute the batch files on button click.
Works only on Internet Explorer :
WshShell = new ActiveXObject("WScript.Shell");
WshShell.Run("\"" + file + "\"" + "\"" + script + "\"," + "\"" + users + "\"," + "\"" + jtlReport + "\"," + "\"" + ramp + "\"," + "\"" + loop + "\"", 1, true);
WshShell.Run("\"" + file + "\"" + "\"" + script + "\"," + "\"" + users + "\"," + "\"" + jtlReport + "\"," + "\"" + ramp + "\"," + "\"" + loop + "\"", 1, true);
3 parameters in WshShell.Run() :
- file - Batch File
- script, users, jtlReport, ramp, loop - parameters to batch file which will be accessed using %1, %2, etc. in Batch file.
- true (last parameter) - wait for the complete execution of Batch File
Approach (Server) :
Since execution of Batch files using ActiveXObject will not work on Server, we need to use some Server Side Programming Language to execute Batch Files with required parameters passed using POST from client.
One option is to use C# program to take the input from client (using POST), convert the input string to required parameters to pass to Batch Files, convert this C# program to .exe (executable) file and specify this .exe filename in the action attribute of POST on client FORM (Web Page).
C# Program (On Server) : (filename - Console1.cs)
using System.Web;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System;
using System.Collections.Generic;
using System.Text;
namespace Console1 {
class Console1 {
static void ExecuteCommand(string command)
{
int exitCode;
ProcessStartInfo processInfo;
Process process;
processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
// *** Redirect the output ***
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
process = Process.Start(processInfo);
process.WaitForExit();
// *** Read the streams ***
// Warning: This approach can lead to deadlocks, see Edit #2
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
exitCode = process.ExitCode;
Console.WriteLine("output>>" + (String.IsNullOrEmpty(output) ? "(none)" : output));
Console.WriteLine("error>>" + (String.IsNullOrEmpty(error) ? "(none)" : error));
Console.WriteLine("ExitCode: " + exitCode.ToString(), "ExecuteCommand");
process.Close();
}
static void Main()
{
if (System.Environment.GetEnvironmentVariable("REQUEST_METHOD").Equals("POST")) {
string PostedData = "";
int PostedDataLength = Convert.ToInt32(System.Environment.GetEnvironmentVariable("CONTENT_LENGTH"));
if (PostedDataLength > 2048) PostedDataLength = 2048; // Max length for POST data
for (int i = 0; i < PostedDataLength; i++)
PostedData += Convert.ToChar(Console.Read()).ToString();
//Batch Files execution with required parameters
string command = @"C:\test.bat param1 param2 param3"
ExecuteCommand(command);
command = @"C:\test1.bat param1 param2"
ExecuteCommand(command);
}
}}
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System;
using System.Collections.Generic;
using System.Text;
namespace Console1 {
class Console1 {
static void ExecuteCommand(string command)
{
int exitCode;
ProcessStartInfo processInfo;
Process process;
processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
// *** Redirect the output ***
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
process = Process.Start(processInfo);
process.WaitForExit();
// *** Read the streams ***
// Warning: This approach can lead to deadlocks, see Edit #2
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
exitCode = process.ExitCode;
Console.WriteLine("output>>" + (String.IsNullOrEmpty(output) ? "(none)" : output));
Console.WriteLine("error>>" + (String.IsNullOrEmpty(error) ? "(none)" : error));
Console.WriteLine("ExitCode: " + exitCode.ToString(), "ExecuteCommand");
process.Close();
}
static void Main()
{
if (System.Environment.GetEnvironmentVariable("REQUEST_METHOD").Equals("POST")) {
string PostedData = "";
int PostedDataLength = Convert.ToInt32(System.Environment.GetEnvironmentVariable("CONTENT_LENGTH"));
if (PostedDataLength > 2048) PostedDataLength = 2048; // Max length for POST data
for (int i = 0; i < PostedDataLength; i++)
PostedData += Convert.ToChar(Console.Read()).ToString();
//Batch Files execution with required parameters
string command = @"C:\test.bat param1 param2 param3"
ExecuteCommand(command);
command = @"C:\test1.bat param1 param2"
ExecuteCommand(command);
}
}}
Conversion of C# Program to executable file (.exe) : (Execute on cmd)
%windir%\Microsoft.NET\Framework\v2.0.50727\csc.exe Console1.cs
HTML Source Program :
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>Test CGI in IIS7 using C#</title>
</head>
<body>
<h1>Simple Form - Test CGI Application using POST</h1>
<form action="Console1.exe" method="POST">
Enter your First Name: <INPUT TYPE="text" NAME="fname" SIZE="30" />
<br />Enter your Last Name: <INPUT TYPE="text" NAME="lname" SIZE="30" /><br />
<br />Enter your Last Name: <INPUT TYPE="text" NAME="test" SIZE="30" /><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>
<html>
<head><meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>Test CGI in IIS7 using C#</title>
</head>
<body>
<h1>Simple Form - Test CGI Application using POST</h1>
<form action="Console1.exe" method="POST">
Enter your First Name: <INPUT TYPE="text" NAME="fname" SIZE="30" />
<br />Enter your Last Name: <INPUT TYPE="text" NAME="lname" SIZE="30" /><br />
<br />Enter your Last Name: <INPUT TYPE="text" NAME="test" SIZE="30" /><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>
That's it !! Our HTML page is now ready to take required inputs and send to the server for further processing which will return some response in HTML format using Console1.exe
Notice the form tag attributes in the HTML source code. You can also specify the client-side validation using "onSubmit" attribute in FORM tag.
Note :
You need to configure your Web Server to allow the execution of CGI scripts before using the above code snippets, so make sure that CGI scripts execution is enabled on your Web Server.
Feel free to comment in case of any queries.
Happy Learning !!
No comments:
Post a Comment