change directory command in my script is not being recognize when i run the script using plink - linux

I'm running a batch file (export.bat) in Windows 7 using plink to execute a script in a remote Linux server machine, but I get this error:
./test.sh: line 3: back.sh: command not found
Batch file:
#echo off
cls
plink 1.1.10.11 -l user -pw pass "bash ./test.sh"
Script in the remote server:
#!/bin/sh
cd /path/path/path
script --table filename--filebase /path/path/path/path
exit

I'm assuming script in your remote script is actually back.sh, and that it exists in /path/path/path.
To execute a script back.sh in the directory you cd to (i.e. the current directory), use ./back.sh instead of just back.sh.
PS: You should generally try to avoid sanitizing input and output of things you post on StackOverflow. It often ends up confusing. For example, you say you get the error line 3: back.sh: command not found, but your remote script does not contain the command back.sh on line 3 or anywhere.
Instead, invest 15 minutes in making a runnable test case with no sensitive data, that you can actually execute and copy files and errors from verbatim. The bash tag wiki has tips for this.

Related

Robot Framework: How can I execute a .sh file in a test case?

I want to start a .sh file in a test case but it doesn't work like I would like it to.
I tried it with Run Process /appl/Test1/asd.sh Value1 Value2
and OperatingSystem.Run /Path/Test1/asd.sh Value1 Value2but both don't execute the asd.sh like it's executed in shell.
I used this Keywords for running a .pl Script and there it was no problem.
Do you mean to run .sh script in the same machine you launch ROBOT Framework or running the .sh file in another target unix/linux machine?
Solution for these two(2) scenario as below. You can do many thing with this RUN PROCESS keyword, please refer to this link for RUN PROCESS keyword: http://robotframework.org/robotframework/latest/libraries/Process.html
A. Running the .sh script in the same machine/os which you launch ROBOT Framework:
Run Process /appl/Test1/asd.sh shell=yes --option argument
B. Running the .sh script in another target machine;
In order to run .sh file, you must first write a ROBOT Framework's scripts to login to that unix/linux machine. So, follow the steps below.
Import SSH Library into your RIDE. Follow the steps here: http://robotframework.org/SSHLibrary/
Write ROBOT scripts to login to your unix/linux machine. Please go through all the available keywords under SSH Library first to get the hang of it. There are many ways to write the scripts but below are samples..
See sample login scripts below.
#SAMPLE A
Open Connection ${server_ip} prompt=$
${std_output}= Login ${username} ${password}
Should Contain ${std_output} $
#SAMPLE B
Write sudo -u root -i
${std_output}= Read delay=5s
Should Not Contain ${std_output} Permission denied
Should Not Contain ${std_output} Password:
#
Write cd /${hostname}_folder/Access
${std_output}= Read delay=5s
Log ${std_output}
Should Not Contain ${std_output} Permission denied
#
Write su devowner
${std_output}= Read delay=5s
Log ${std_output}
Should Not Contain ${std_output} Password:
Then, next you can use either WRITE or EXECUTE COMMAND keywords to run your .sh file at target machine/host.
On Unix based systems, the following should work:
Run Process /appl/Test1/asd.sh
On Windows, try
Run Process sh /appl/Test1/asd.sh
or
Run Process bash /appl/Test1/asd.sh

How to make Jmeter run command from CMD - OS sampler - JMETER

I am struggling with jmeter for running cmd command to remote desktop.
I use the OS sampler.
I have a command that if I run it from the command line from my computer it worked, but when I try to make jmeter to run it, it failed - I use the same computer.
the command is:
plink -ssh jenkins#178.27.288.288 -pw passtest sudo /opt/test/test.sh
I added an OS sampler as followed:
the working directory is the cmd.exe directory.
and the command is the full command I want jmeter to run, the same one that working perfectly on cmd direct.
I get this error:
Response message: Exception occurred whilst executing system call: java.io.IOException: Cannot run program "plink -ssh jenkins#178.27.288.288 -pw passtest sudo /opt/test/test.sh" (in directory "c:\windows\system32"): CreateProcess error=2, The system cannot find the file specified
I do not know which file jmeter wants, I just want him to run the command, to connect to remote computer and run the command, the command working perfectly in command line and run the script in the remote computer.
I found this blog in the net:
https://www.blazemeter.com/blog/how-run-external-commands-and-programs-locally-and-remotely-jmeter
that said that it can be done, but since it is easy they not displayed step by step instructions.
when I change the command to dir, it run OK and the results are like I write dir in command line.
How can I make jmeter open the command line and just put the command and press enter, and display the results?
In the command you need to give the name of the programme to execute. In case of command prompt it is cmd.exe and for shell i think it is powershell.exe
Rest need to be pass as command parameters.
Check below for command parameters:-
The OS Process Sampler is a sampler that can be used to execute
commands on the local machine. It should allow execution of any
command that can be run from the command line. Validation of the
return code can be enabled, and the expected return code can be
specified. Note that OS shells generally provide command-line parsing.
This varies between OSes, but generally the shell will split
parameters on white-space. Some shells expand wild-card file names;
some don't. The quoting mechanism also varies between OSes. The
sampler deliberately does not do any parsing or quote handling. The
command and its parameters must be provided in the form expected by
the executable. This means that the sampler settings will not be
portable between OSes.
Many OSes have some built-in commands which are not provided as
separate executables. For example the Windows DIR command is part of
the command interpreter (CMD.EXE). These built-ins cannot be run as
independent programs, but have to be provided as arguments to the
appropriate command interpreter.
For example, the Windows command-line: DIR C:\TEMP needs to be
specified as follows:
Command: CMD Param 1: /C Param 2: DIR Param 3: C:\TEMP
If the commands are remote commands then try with "Remote Commands: Linux/MacOSX" with SSH sampler shown in the blog you shared.
Below screenshot for simple execution of dir command:-
--update--
additional screenshots
to execute a windows commad DIR c:\test use below settings

Errant error output behavior with perl execute bash scripts on a remote machine

I have this line in my perl script that sshes into a remote machine and execute a bash script:
system("ssh -t remotemachine /dir/dir/bashscript");
In my bash script, I used exit code 2 some commands 2 >> error.txt to capture any errors that I may encounter and I want this error.txt to be written in the same folder where the bash script is stored.
My problem is when I ssh into the machine and run the program from the terminal, the error can be captured and written in error.txt but if I run the program from my perl, the program is able to run but the error is not captured.
Any suggestions?
Use the full path for the capture file.
some commands 2 >> /dir/dir/error.txt
Otherwise the file will be created in the users $HOME if it exists.

how to execute shell script from soapui groovy script?

I have shell script which is kept on remote server(linux machine) and I am trying to call that shell script in between the execution of various test cases of SOAPui from windows.
So I have prepared a groovy script:
def command="/usr/bin/ssh -p password username#IP_address bash -s < /home/test.sh"
def proc=command.execute().text
proc.waitFor()
But unfortunately, I am receiving an error:
java.io.IOException: Cannot run program "/usr/bin/ssh": CreateProcess error=2, The system cannot find the file specified error at line: 6
I tried to search more on this, but couldn't get the resolution. Some of the links were:
How to execute shell script using soapUI
http://groovy-lang.org/groovy-dev-kit.html#process-management
If as you comment you've a putty.exe installed on Windows you can try with the follow.
First of all create a file in your Windows local with the commands to execute remotely for example I create the follow C:/temp/commandsToRunRemotely.txt then in this file put the command you want to execute. As a sample I use the follow command:
echo "test remote execution" > /tmp/myfile.txt
Then from groovy script in SOAPUI call putty.exe passing the local file which contains the commands to execute remotely:
def command = "C:/path/to/putty.exe -ssh user#IP -pw pass-m C:/temp/commandsToRunRemotely.text"
def proc = command.execute()
proc.waitFor()
Note that if you've putty.exe in your Windows path, you can simply use putty.exe instead of full path.
This is only an ilustation sample, but if you want to execute a shell script remotely instead of echo "test remote execution" > /tmp/myfile.txt in the commands file use directly the path for your script: /home/test.sh
I get the Putty command line options from this nice answer
Hope it helps,

Can't run a script

I tried to create a script in linux, on a Synology server over SSH
so I wrote a file test.sh
#!/bin/bash
echo "this is a test"
I saved the file.
after that I did
chmod 755 test.sh
the I did
./test.sh
then i got this error
-ash "./test.sh" is not found
the file was created in
/root
I don't understand
Your shell (ash?) is trying to execute your script and is getting an ENOENT (no such file or directory) error code back. This can refer to the script itself, but in this case it refers to the interpreter named in the #! line.
That is, /bin/bash does not exist and that's why the script couldn't be started.
Workaround: Install bash or (if you don't need any bash specific features) change the first line to #!/bin/sh.
This is one of the quirks with hash bang programs. If the interpreter is not found (i.e. the program interpreting the script), you don't get a completely useful error like /bin/bash: no such file, but a completely useless and misleading test.sh: not found.
If this isn't in the Unix Hater's Handbook, it should be. :-)
You can either use #!/bin/sh or #!/path/to/bash or #!/usr/bin/env bash (which searches PATH for bash).

Resources