Running multiple commands in cmd via psexec - ant

I'm working on creating a single command that will run mulitple things on the command line of another machine. Here is what I'm looking to do.
Use psexec to access remote machine
travel to proper directory and file
execute ant task
exit cmd
run together in one line
I can run the below command from Run to complete what I need accomplished but can't seem to get the format correct for psexec to understand it.
cmd /K cd /d D:\directory & ant & exit
I've tried appling this to the psexec example below:
psexec \\machine cmd /K cd /d D:\directory & ant & exit
When executing this it will activate the command line and travel to D:\directory but won't execute the remaining commands. Adding "" just creates more issues.
Can anyone guide me to the correct format? Or something other than psexec I can use to complete this (free options only)?

Figured it out finally after some more internet searching and trial and error. psexec needs /c to run multiple commands, but that syntax doesn't work with the setup I wrote above. I've gotten the below command to run what I need.
psexec \\machine cmd /c (^d:^ ^& cd directory^ ^& ant^)
I don't need to exit because psexec will exit itself upon completion. You can also use && to require success to continue on to the next command. Found this forum helpful
http://forum.sysinternals.com/psexec_topic318.html
And this for running psexec commands
http://ss64.com/nt/psexec.html

This works:
psexec \ComputerName cmd /c "echo hey1 & echo hey2"

For simple cases I use:
PsExec \\machine <options> CMD /C "command_1 & command_2 & ... & command_N"
For more complex cases, using a batch file with PsExec's -c switch may be more suitable:
The -c switch directs PsExec to copy the specified executable to the remote system for execution and delete the executable from the remote system when the program has finished running.
PsExec \\machine <options> -c PSEXEC_COMMANDS.cmd <arguments>

Since you asked about other options and this has tag configuration managment-- I guess you may be interested in Jenkins (or Hudson). It provide very good way of creating master-slave mechanism which may help in simplifying the build process.

I always use like this way :) and works properly
psexec \\COMPUTER -e cmd /c (COMMAND1 ^& COMMAND2 ^& COMMAND3)

Related

Unable to DOS commands using Power shell on Windows 10

In my process document I run below mentioned commands from command prompt and it works fine from command prompt but it does not work when I run these commands from Power Shell.
c:\temp\sig.csv sig.csv has input file.
I want this command to be executed silently and it takes input from sig.csv file located in temp folder.
Below is the link which I am following but using powershell.
https://www.paloaltonetworks.com/documentation/40/endpoint/endpoint-admin-guide/manage-traps-in-a-vdi-environment/configure-the-master-policy/traps-vdi-tool-cli.html
Dos Command:
TrapsVdiTool -i:c:\temp\sig.csv -e:192.168.70.100 -ssl -to:1
I run it using cmd.exe also tried executing using using &
& TrapsVdiTool -i:c:\temp\sig.csv -e:192.168.70.100 -ssl -to:1
cmd.exe /c "TrapsVdiTool -i:c:\temp\sig.csv -e:192.168.70.100 -ssl -to:1"
In the Powershell console it should run this way:
& TrapsVdiTool '-i:c:\temp\sig.csv' '-e:192.168.70.100' '-ssl' '-to:1'
Powershell: Running Executables

How to add to slave's PATH using Slave SetupPlugin?

I have 2 RHEL machines setup in a Master/Slave configuration using Jenkins ver. 1.609.2
The slave is being launched via SSH Slaves Plugin 1.10.
I'm trying to use the Slave Setup Plugin v 1.9 to install the tools that will be necessary for my slave machine to run builds. In particular I am installing sqlplus.
Here is the script that I am running in order to try installing sqlplus:
if command -v sqlplus >/dev/null; then
echo "sqlplus already setup. Nothing to do."
else
#Create directory for sqlplus and unzip it there.
mkdir /jenkins/tools/sqlplus
tar -xvf sqlplussetup/instantclient-basiclite-linux.x64-12.1.0.2.0.tar.gz -C /jenkins/tools/sqlplus || { echo 'unzip failed' ; exit 1; }
tar -xvf sqlplussetup/instantclient-sqlplus-linux.x64-12.1.0.2.0.tar.gz -C /jenkins/tools/sqlplus || { echo 'unzip failed' ; exit 1; }
cd /jenkins/tools/sqlplus/instantclient_12_1
#Create links for the Oracle libs
ln -s libclntsh.so.12.1 libclntsh.so || { echo 'Could not create link' ; exit 1; }
ln -s libocci.so.12.1 libocci.so || { echo 'Could not create link' ; exit 1; }
#Add two lines to .bashrc only if they don't already exist. Export LD_LIBRARY_PATH and add sqlplus to PATH.
grep -q -F 'export LD_LIBRARY_PATH=/jenkins/tools/sqlplus/instantclient_12_1:$LD_LIBRARY_PATH' /home/jenkins/.bashrc || echo 'export LD_LIBRARY_PATH=/jenkins/tools/sqlplus/instantclient_12_1:$LD_LIBRARY_PATH' >> /home/jenkins/.bashrc
grep -q -F 'export PATH=$PATH:/jenkins/tools/sqlplus/instantclient_12_1' /home/jenkins/.bashrc || echo 'export PATH=$PATH:/jenkins/tools/sqlplus/instantclient_12_1' >> /home/jenkins/.bashrc
#Export variables so they can be used right away
export LD_LIBRARY_PATH=/jenkins/tools/sqlplus/instantclient_12_1:$LD_LIBRARY_PATH
export PATH=$PATH:/jenkins/tools/sqlplus/instantclient_12_1
echo "sqlplus has been setup."
fi
This script runs successfully and everything appears to work until I try to run a build and execute the sqlplus command. The build fails because sqlplus is not a recognized command.
My main question is this:
What is the proper way to automatically add an environment variable when launching a slave?
Please note I am looking for an automated way of doing this. I don't want to go into the configuration screen for my slave, tick a checkbox and specify an environment variable. That is counter-productive to what I am trying to achieve which is a slave that is immediately usable for builds once connected.
I pretty much understand why my script doesn't work. When Jenkins is launching the slave it first makes an SSH connection and then it runs my setup script using the command
/bin/sh -xe /jenkins/tmp/hudson8035138410767957141.sh
Where the contents of hudson8035138410767957141.sh is my script from above. So obviously, the export isn't going to work. I was hoping adding the exports to the .bashrc file would get around this but it does not work. I think this is because this script is executed after the ssh connection is established and therefore the .bashrc has already been read.
Problem is I can't figure out any way to work around this limitation.
Bash does not read any of its startup files (.bashrc, .profile etc) for non-interative shells that don't have the --login option set explicitly -- that's why the exports don't work.
So, solution "A" is to keep the bashrc magic that you suggest above, and to add the --login option by changing the first line in your build step to
#!/bin/bash --login
<your script here>
The explicit shebang at on the first line will also prevent excessive debug output that you get from the default's -x option (see your console snippet above).
Alternative solution "B" uses the fact that bash will source any script whose name is given in $BASH_ENV (if that variable is defined and the file exists). Define that variable globally in your slave properties (e.g., set to /jenkins/tools/setup.sh) and add exports as needed during slave setup. Every bash shell build step will read the settings then.
With solution "B" you don't need to use the --login option and you don't have to mess up the .bashrc. However, the "BASH_ENV" feature is only active when bash runs in "bash mode". As Jenkins starts the shell via sh, bash tries to emulate historic sh, which does not have that feature. So, also for B, you need a shebang:
#!/bin/bash
<your script here>
But that you'd need anyway to get rid of the tracing output that's usually too much in production setups.

Powershell why popup window, how to stop it

Not sure why when I execute a Python tools like pip or nosetests inside powershell, a separate popup command line windows will show, execute my command, then disappeared. This is annoying because I can hardly see the executable output, especially the last few lines before the popup close.
I assume there are some setting I can change to stop the popup?
I am using Powershell 2.0 in Windows 7.
Powershell is not cmd.exe, and it has a different console interface. More than likely, your py tools are writing to a non-existent shell window. You may be able to get around this by using the following syntax:
cmd /c script.py
What you do when you execute the python scripts directly from the PS prompt is fire-off a DOS shell for the period of time it takes for the command to complete. Since there's no 'pause' implemented, the shell window closes when the command completes.
A test script
# tester.py, just a test
print "This is a test script, that is all."
Output in PS:
C:\src\python
{powem} [36] --> .\tester.py
C:\src\python
{powem} [37] --> cmd /c .\tester.py
This is a test script, that is all.
mp
For someone has similar problem, please have a look at this answer, I think this solution eventually solved my problem. and in my case, I have to restart my computer to get it all working.

Running Fitnesse Tests from Command Line

When running the Fitnesse tests via command Line I am getting a ClassNotFound Exception
caused by Java.lang.ClassNotFoundException:fitnesse.runner.testRunner
could not find the main class:fitnesse.runner.TestRunner.Program will exit
Kindly help
Thanks
TestRunner is no longer supported. Use http://fitnesse.org/FitNesse.UserGuide.CommandLineRestCommands
Update: link has changed - http://www.fitnesse.org/FitNesse.UserGuide.ControllingFitNesseFromTheCommandLine
Update: link has changed again - http://fitnesse.org/FitNesse.UserGuide.AdministeringFitNesse.ControllingFitNesseFromTheCommandLine
Excerpt from Fitnesse docs:
You can run any REST command (See Restful Services) from the command
line by using the -c command line option as follows:
java -jar fitnesse-standalone.jar -c
This starts up fitnesse, runs the command, emits the output to
standard out, and then exits.
Most often you will use this to run tests and/or suites from the
command line as follows:
java -jar fitnesse-standalone.jar -c "MyTestPage?test&format=text"

Analysing a shell script

This would be part of a reverse-engineering project.
To determine and document what a shell script (ksh, bash, sh) does, it is comfortable, if you have information about what other programs/scripts it calls.
How could one automate this task? Do you know any program or framework that can parse a shell script? This way for instance, I could recognize external command calls -- a step to the right direction.
For bash/sh/ksh, I think you can easily modify their source to log what has been executed. That would be a solution.
How about:
Get a list of distinct words in that script
Search $PATH to find a hit for each
?
bash -v script.sh ?
Bash's xtrace is your friend.
You can invoke it with:
set -x at the top of your script,
by calling your script with bash -x (or even bash --debugger -x),
or recursively by doing (set -x; export SHELLOPTS; your-script; )
If you can't actually run the script, try loading it into a text editor that supports syntax highlighting for Bash. It will color-code all of the text and should help indicate what is a reserved word, variable, external command, etc.

Resources