How to get acknowlegement in Execute Command(SSHLibrary) - linux

If i give Linux command as a argument to Execute Command(robot framework keyword, for communicate with remote server), how can i verify given Linux command is run correctly or not?

The best way I have found to do this is to return the return code alongside stdout (and stderr if you require) from the "Execute Command" keyword. e.g.:
SSHLibrary.Open Connection hostname port
SSHLibrary.Login username password
${stdout} ${return_code}= SSHLibrary.Execute Command valid_command return_rc=True
${stdout} ${return_code}= SSHLibrary.Execute Command invalid_command return_rc=True
In this example the first call to execute command would return a ${return_code} value of 0 and the second a non-zero status code. So you could just perform a "Should Be Equals" or similar assertion on the return code.

I can't quite see the keyword you mentioned. Is that part of the standard library?
A quick check yields some keywords under OperatingSystem, which has some keywords that runs a command and returns the return code.
http://robotframework.googlecode.com/hg/doc/libraries/OperatingSystem.html?r=2.8.1#Run And Return Rc
This should tell you whether your command executed properly because many Linux commands are silent on success.

Related

How to store result of cmd using Popen with option (input=password) in syntax as mandatory

Part of script in the code, below, is work partial to me, in another words, executed samba-tool command and bring me result on the screen (python3 script.py). BUT, the value of process (variable) is all none.
I tried/read documentations, but without success to store and fix it.
from subprocess import Popen,PIPE, STDOUT
def run_command():
process = Popen(['samba-tool', 'dns','query','chaps.xpto.local','xpto.local','#','ALL','-U','auditor'], stdin=PIPE).communicate(input=b'#!3202#otpX')
print(process)
run_command()
~
I tried:
outs, errs = Popen(['****')
print (outs)
or
print (process.stdout)
I tried to use StringIO to capturing, but sysout of command Popen doesn't record tool
Thank you Ryanwebjackson.
After days suffer with it, I notice that problem is only for syntax samba-tools to query dns information, because the arguments need to inform login/pwd to query and if try capture
output doesn´t works, the utility not understands a valid credetials/bad password.
If i try the syntax to use with other utility or shell commands work well. I decided change the approach and replace :
process = Popen(['samba-tool', 'dns','query','chaps.xpto.local','xpto.local','#','ALL','-U','auditor'], stdin=PIPE).communicate(input=b'#!3202#otpX')
For :
process = subprocess.getoutput("(($(dig +noall +answer "+host+"."+domain+" |wc -c)>0)) && echo exist")
I had the list of host to test if there's records on DNS and zone and voila! fixed.

Shell Script: How to read standard output of a program from console

I am trying to write a shell script which gives different inputs to a program and checks the outputs whether they are expected results or not. In conclusion of these tests, I decide whether there is a bug in my executable program.
I run my program over shell script with ./my_program arg1 arg2 (arg1 and arg2 are command line arguments of my program). After that, the script shell constantly gives different inputs to my_program in order to test it and in controlling terminal (or console) standard outputs are iteratively writen like this:
Connection established.
Intermediate result is expected_intermediate_result1
Final result is expected_result1
Connection established.
Intermediate result is expected_intermediate_result2
Final result is expected_result2
And it goes on. For each input, its output is known. So they are matched before.
When connection fails: it is writen Error in connection!
Or result may be wrong:
Connection established.
Intermediate result is result1
Final result is wrong_result1
Apart from giving input, the script has another purpose: check the result.
So I want to read outputs from console and compare them with expected result in order to determine the case in which there is an inconsistency.
I want your assistance to edit this code:
while read console line-by-line
if the line is other than expected result
store this case to text file
done
Some cautions:
I don't want to use expect. I just want to read outputs of the program which is writen in console. I don't use log file so search in a file (grep) will not be used.
Thanks for assistence!
Is this what you're trying to do?
./my_program arg1 arg2 |
grep -Fxq "Final result is expected_result1" || { printf 'Failed: "arg1 arg2" -> "expected_result1"\n'; exit 1; }
If not then edit your question to clarify your requirements and provide a more concrete example.

Throws error when passing argument with space in JAVA_OPTS in Linux

I am passing command line parameters to gatling script.
This works and executes my test in Windows operating system:
set JAVA_OPTS="-DuserCount=2 -DflowRepeatCount=3 -DdefinitionId=102168 -DtestServerUrl=https://someURL -DenvAuthenticationHeaderFromPostman="Basic UWRZm9aGwsxFsB1V7RXK0OlB5cmZvcm1hbmNldGVzdDE="
It works and takes input which is passed
**********************INPUT*************************************
User Count ====>> 2
Repeat Count ====>> 3
Definition ID ====>> 102168
Environment URL ====>> https://someURL
Authentication Header ====>> Basic UWRZm9aGwsxFsB1V7RXK0OlB5cmZvcm1hbmNldGVzdDE=
***********************************************************
I want to do this same thing on Linux System.
While if I use this command in Linux then it throws error or takes Null or Binary values as input
(Passing arguments with ./gatling.sh)
JAVA_OPTS="-DuserCount=2 -DflowRepeatCount=3 -DdefinitionId=102168 -DtestServerUrl='https://someURL' -DenvAuthenticationHeaderFromPostman='Basic UWRZm9aGwsxFsB1V7RXK0OlB5cmZvcm1hbmNldGVzdDE='" ./gatling.sh
Gives this error,
GATLING_HOME is set to /opt/gatling-charts-highcharts-2.0.3 Error:
Could not find or load main class
UWRZm9aGwsxFsB1V7RXK0OlB5cmZvcm1hbmNldGVzdDE='
Here the problem is the space given in argument of -DenvAuthenticationHeaderFromPostman='Basic UWRZm9aGwsxFsB1V7RXK0OlB5cmZvcm1hbm='.
What is the solution?
The problem is that the $JAVA_OPTS variable is probably not surrounded by quotes. See this question: Passing a space-separated System Property via a shell script doesn't work
The gatling guys clearly forgot to do that.
I would file a bug and/or just edit gatling.sh.
Ideally though you might just want to consider seeing if Gatling takes a properties file or some other way to configure.

How to access terminal history in node.js?

I like to process piped commands from within my Node.js commandline app like myapp.babel app.es6 | mynodecmdlineapp. To build a refresh mechanism I need to access the previous terminal commandline text. The place where terminal history is saved varies depending on the shell. Is there a node way to read this out?
Maybe a silly answer, but, if you are constructing the entire chain of commands, you could construct it to also pass the first command that you execute as a first argument to mynodecmdlineapp.
You could then access argv in your mynodecmdlineapp. See,
https://nodejs.org/docs/latest/api/process.html#process_process_argv
An example call would look like this:
myapp.babel app.es6 | mynodecmdlineapp "myapp.babel app.es6"
process.argv[2] would then contain "myapp.babel app.es6" but this assumes that you are constructing the command and able to call it this way. Otherwise, I thought you might be able to leverage "!!" to send the last command, but that would send the last command in history -- not in that command's pipe sequence.

Collecting return code and stdout string from running SAS program in Linux KornShell script

Some developers and I are using KornShell (ksh) to run SAS programs in a Linux environment. The script invokes a SAS command line and I wish to collect the stdout from the SAS execution (a string defined and written by SAS) as well as the Linux return code (0/1).
My Code (collects stdout into envar, but return_code is always 0 because the envar assignment was successful):
envar=$(./sas XXXX/filename.sas -log $LOG_FILE)
return_code=$?
Is there a way to collect both the return code and the std out without having to submit this command twice?
SAS does not write anything to STDOUT when it is run as a non-interactive process. The log file contains the record of statements executed and step statistics; "printed" output (such as from proc print) is written to a "listing" file. By default, that file will be created using the name of your source file appended with ".lst" (in your case, filename.lst).
You are providing a file to accept the log output using the -log system option. The related option to define the listing file is the -print option. Of course, if the program does not create any listing output, such an option isn't needed.
And as you've discovered, the value returned by $? is the execution return code from SAS. Any non-zero value will indicate some sort of error occurred during program execution.
If you want to influence the return code, you can use the ABORT data step statement in your SAS program. That will immediately halt the SAS program as set the return code to something meaningful to you. For example, suppose you want to terminate further processing if a particular PROC SQL step fails:
data _null_;
rc = symgetn('SQLRC');
put rc=;
if rc > 0 then ABORT RETURN 10;
run;
This would set the return code to 10 and you could use your outer script to send an email to the appropriate person. Such a custom return code value must be greater than 6 and less than 976; other values are reserved for SAS. Here is the SAS doc link.

Resources