Executing shell commands through beanshell in jmeter - jmeter

Executing shell commands through beanshell in jmeter. I want to execute shell commands in beanshell preprocessor in jmeter
Can any one tell how to do so.

Beanshell is JAVA (scripting language).
Below statement should help.
Runtime.getRuntime().exec("COMMAND");

Based on #vins answer I cloud create my ping test to verify my email server is available.
Additionally if you want to log the output of the Runtime.getRuntime().exec("COMMAND"); use something similar to this in your jmeter BeanShell Sampler:
// ********
// Ping the email server to verify it's accessible from the execution server
//
// Preconditions:
// custom variable is available named "emailServer" which contains the IP-Adress of the machine to ping
//
// ********
log.info(Thread.currentThread().getName()+": "+SampleLabel+": Ping email server: " + vars.get("emailServer"));
// Select the ping command depending on your machine type windows or Unix machine.
//String command = "ping -n 2 " + vars.get("emailServer"); // for windows
String command = "ping -c2 " + vars.get("emailServer"); // for Unix
// Print the generated ping command
log.info(command);
// Create a process object and let this object execute the ping command
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
log.info("Execution complete.");
// Read the output of the ping command and log it
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder logCommandOutput = new StringBuilder();
String line;
while( (line = in.readLine()) != null) {
logCommandOutput.append(line);
}
in.close();
log.info("Output: " + logCommandOutput.toString());

Related

jcraft execute shell command returns exit status 127

I am trying to run shell script
(/sasdata/sasconfig/Lev1/Applications/SASEnterpriseGRCAdminTools/6.1/dbscripts/addxlsdata.sh -t /sasdata/Data/Loaders/IFT_tst_loader2.xls)
on remote server with java, using jcraft, but in answer I have exit status 127. I tried to run simple command "date" with my method - and all was good. Then I tried to run this script by entering command in terminal, and all was ok too. Also I tried to execute "cd" to path with addxlsdata.sh and there run
./addxlsdata.sh -t /sasdata/Data/Loaders/IFT_tst_loader2.xls
and again there was 127 exit status.
What the problem it can be?
Here is my method:
public static void executeShell(String shellScriptCommand, String userName, String password, String server) {
List<String> result = new ArrayList<String>();
try {
LOG.info("Creating session, user: " + userName);
JSch jSch = new JSch();
Session session = jSch.getSession(userName, server, sshPort);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
LOG.info("Opening exec channel");
ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
LOG.info("Creating InputStream");
InputStream in = channelExec.getInputStream();
channelExec.setCommand("sh " + shellScriptCommand);
LOG.info("Executing the shell script: " + shellScriptCommand);
channelExec.connect();
LOG.info("Reading the output from the input stream");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
CommonFunctions.freeze(3);
while (reader.ready()) {
String line = reader.readLine();
result.add(line);
}
reader.close();
LOG.info("OUTPUT is " + result);
LOG.info("Getting exit status");
int exitStatus = channelExec.getExitStatus();
LOG.info("Exit status is [" + exitStatus + "]");
channelExec.disconnect();
session.disconnect();
} catch (JSchException | IOException e) {
LOG.error(Arrays.toString(e.getStackTrace()));
throw new AutotestError("Ошибка при выпонении shell скрипта", e);
}
}
I find out one thing. This *.sh file have another script inside "./runjava.sh". Maybe here is the problem
It seems that it is my fault in connect to wrong server. Despite that I use the same address in java and winscp, when I use ls in directory with the script in java and in terminal, I have different results
I think that I have found what the problem. When I execute "cd /" in java and then "pwd", the output is "/home/username". I can't exit from Home directory to root in java. In terminal I can do this with the same user
Your java application has not permission to execute the script. Add the account (which run your java application) to the group which has ownership to script ./addxlsdata.sh. Also provide necessary execute permission as well.
First go to the directory and execute ls -ltr and provide the detail information to your Unix admin and also provide the user id detail which run the java application to add that user to the proper group. It will work once you short out this.
It was my terrible fault. I invoked my method several times with different commands. I was running executeShell "cd /" and then executeShell "pwd". And of course second time it was a new session.

Jmeter: Running the nongui command using java

I am trying to run the jmeter nongui command using java as follows:
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("C:\\apache-jmeter-2.13\\bin\\jmeter.bat -t \"C:\\jmeter scripts\\test.jmx\" -n -l \"C:\\jmeter scripts\\nonGUI.csv\"");
It runs perfectly fine, until I add the argument:
-Jusers=15 inside the command mentioned above in the next run.
The property set for the number of threads is: ${__P(users,10)}
The result file does not seem to fill up and the process seems to run forever under the CPU Resource monitor.
P.S.: Please do not suggest me to run the jmeter file using the steps given in the blazemeter website. It has used one of the deprecated method and there is no resolution given for the plausible runtime errors in that website.
I was not able to reproduce your error, but here is a complete example with JMX File. I removed the need for the "".
// OSX exmaple
public class r {
public static void main(String[] args) throws Exception {
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("/usr/local/bin/jmeter -t /Users/rfriedman/jmeter/SimpleUrl.jmx -Jusers=15 -n -l /Users/rfriedman/jmeter/nonGUI.csv");
}
}
Just to make sure I ran modified on Windows as well
// Windows Example
public class r {
public static void main(String[] args) throws Exception {
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("C:\\Users\\rfriedman\\Desktop\\apache-jmeter-2.13\\bin\\jmeter.bat -t C:\\Users\\rfriedman\\Desktop\\SimpleUrl.jmx -Jusers=20 -n -l C:\\Users\\rfriedman\\Desktop\\nonGUI.csv");
}
}
JMeter Test Plan
It works after I add the property value for Synchronization timer similar to Thread count.
Also, if I have to pass the value of -Jusers in the form of variable, how to do it? I am trying to do the following. But it's not getting executed.
eg:
int value=10;
Process pr = rt.exec("C:\Users\rfriedman\Desktop\apache-jmeter-2.13\bin\jmeter.bat -t C:\Users\rfriedman\Desktop\SimpleUrl.jmx -Jusers=value -n -l C:\Users\rfriedman\Desktop\nonGUI.csv");
Update:
I tried with String value="10"; as well. Still the jmeter logs says:
"jmeter.reporters.Summariser: summary = 0 in 0s = ******/s Avg: 0 Min: 9223372036854775807 Max: -9223372036854775808 Err: 0 (0.00%)"
Use this snippet.
int value = 10;
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("C:\\apache-jmeter-2.13\\bin\\jmeter.bat" +
" -t \"C:\\jmeter scripts\\test.jmx\" -Jusers=" + value + " -Jsync=" + value +
" -n -l \"C:\\jmeter scripts\\nonGUI.csv\" -j \"C:\\jmeter scripts\\jmeterLogs.log\"");

D: executeShell on Windows to run another program not returning immediately

I'm using D as a scripting language for Windows 7 console stuff to automate boring tasks. One of my scripts (open.exe) is supposed to allow me to open stuff from the command line without me having to specify which program I use (I have a configuration file with this stuff). Now, I use executeShell to do this, and call something like start [name of program I want to use] [name of input file]. If I do this directly from the shell, it returns immediately, but if I do it using my D script, it doesn't return until the program that it opens is closed. What should I do to allow it to return immediately?
For reference purposes, this is the business logic of my script (the main method just does some argument parsing for piping purposes):
immutable path = "some//path//going//to//config//file.conf";
void process(string input) {
string extension = split(input,".")[1]; //get file extension from input
auto config = File(path,"r"); auto found = false;
while (!config.eof()){
auto line = chomp(config.readln());
if (line[0]!='#') { //skip comment lines
auto divided = split(line, ":");
if (divided[0] == extension) {
found = true;
auto command = "start " ~ divided[1] ~ " " ~ input;
auto result = executeShell(command);
//test for error code and output if necessary
writeln(result.output);
}
}
}
if (!found)
writeln("ERROR: Don't know how to open " ~ input);
}
From the top of the std.process documentation:
Execute and wait for completion, collect output - executeShell
The Windows start program spawns a process and exits immediately. D's executeShell does something else. If you'd like to spawn another program, use the appropriate functions: spawnProcess or spawnShell.

How to run FFMPEG at my web host server

I want to perform some video process at my web host server. I don't think the web host server will allow me to execute an exe file for security reasons.
Should I use SharpFFMpeg?
I have downloaded SharpFFMpeg. But it's lacking a proper documentation.
Can someone give one example how to perform a conversion from one video format to another?
I have written my execution program, but the compiler says it cannot file the file specified. What's wrong with it?
string command = #"D:\Recorded TV\ffmpeg.exe -i ""Australia's Toughest Police_QUEST_2013_04_17_21_57_00.wtv"" -s 800x400 throughCS.mp4";
try
{
ProcessStartInfo psi = new ProcessStartInfo("\"" + command + "\"");
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
Process proc = new Process();
proc.StartInfo = psi;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
tb1.Text = result;
Debug.WriteLine(result);
}

Read Windows Command Prompt STDOUT

I have a command line application that runs on a windows server. The command prompt remains open when the program is running, and log messages are output to the command prompt window as the program functions.
My need is to read the messages that appear on the command prompt as the program runs, and then run particular commands if a specific set of words appear in the messages.
What's the easiest way to do this on a windows machine? (without modifying the app)
Reading those two posts will give you the solution:
ProcessStartInfo
Capturing console output.
The idea is to to run your app (not modifying it) from your new app (written in C#) and redirect its input-output here, reading and writing as you please.
An example could be:
Process proc;
void RunApp()
{
proc = new Process();
proc.StartInfo.FileName = "your_app.exe";
proc.StartInfo.Arguments = ""; // If needed
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.OutputDataReceived += new DataReceivedEventHandler(InterProcOutputHandler);
proc.Start();
proc.WaitForExit();
}
void InterProcOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
// Read data here
...
// Send command if necessary
proc.StandardInput.WriteLine("your_command");
}

Resources