Piping output in cmd batch file on Azure Batch service error codes not detected - batch-file

I have a .cmd batch file (let's call it RunSQlCmd.cmd) which pipes out output of sqlcmd to 7 zip compressor
sqlcmd -i.\table.sql -S . -E -s "," -I -h -1 -W| "c:\Program Files\7-Zip\7z.exe" a -tbzip2 -si "out.csv.bz2"
I run it from Azure Batch in a task from a C# driver program with the following command line
(1) cmd /c %AZ_BATCH_NODE_SHARED_DIR%\RunSqlCmd.cmd
But what seem to happen is that command line returns almost immediately and commands in RunSqlCmd.cmd are not fully executed and empty compressed archive is created. Azure Batch task exits with success code 0.
If I change task's command line to
(2) cmd /c start /wait %AZ_BATCH_NODE_SHARED_DIR%\RunSqlCmd.cmd
commands in a batch file run successfully but stdout and stderr is lost for Azure Batch as batch file runs in a separate cmd window and task hangs up without getting any error code.
changing tasks's command line to
(3) cmd /c start /B /wait %AZ_BATCH_NODE_SHARED_DIR%\RunSqlCmd.cmd
is similar to (1)
What is the correct way to do it so that Azure Batch detects correctly when task is finished and RunSqlCmd.cmd command finishes completely?
P.S. Real content of RunSqlCmd.cmd file is as
#echo Run SqlCmd
sqlcmd -i "%~dp0%1.sql" -d dbName -S serverName -U userName -P "password" -s "," -I -h -1 -W -b | "%ProgramFiles%\7-Zip\7z.exe" a -tbzip2 -si "%~dp0%1.csv.bz2"
#echo Done SqlCmd
It takes 1 parameter - name of sql file to pull data

I do not quite understand why you want to run cmd /c and start /wait to kick off a program. you do not need to call cmd.exe from start and do not need to call cmd to start a batch file.
if it is a batch file, you can just call the batch file. it will automatically wait internally for the command to complete.
%AZ_BATCH_NODE_SHARED_DIR%\RunSqlCmd.cmd
That should work as is without calling start or cmd again.
The main problem is that you are sending the command with parameters in quotes to external program, so you you call cmd again and it sends the commands to this cmd window and stripping out quotes when it sends to the external. Just calling the script as is will do.

Related

How to trigger shell script in Unix Server using PSCP Commands? [duplicate]

I need to execute a shell script remotely inside the Linux box from Windows
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "Illegal number of parameters"
exit
fi
echo "$1"
Here is the command I ran from Windows command prompt
cmd> plink.exe -ssh username#host -pw gbG32s4D/ -m C:\myscript.sh 5
I am getting output as
"Illegal number of parameters"
Is there any way I can pass command line parameter to shell script which will execute on remote server?
You misunderstand how the -m switch works.
It is just a way to make plink load the commands to send to the server from a local file.
The file is NOT uploaded and executed on the remote server (with arguments).
It's contents is read locally and sent to the server and executed there as if you typed it on a (remote) command line. You cannot give it arguments.
A workaround is to generate the file on the fly locally before running plink from a batch file (say run.bat):
echo echo %1 > script.tmp
plink.exe -ssh username#host -pw gbG32s4D/ -m script.tmp
Then run the batch file with the argument:
run.bat 5
The above will make the script execute echo 5 on the server.
If the script is complex, instead of assembling it locally, have it ready on the server (as #MarcelKuiper suggested) and execute just the script via Plink.
plink.exe -ssh username#host -pw gbG32s4D/ "./myscript.sh %1"
In this case, as we execute just one command, you can pass it on Plink command line, including the arguments. You do not have to use the -m switch with a (temporary) file.
I triggered the Shell script in "commands.txt" from Plink which worked for me like a charm with below method I tried:
You can define your script as an one liner using && in a file (I defined in one liner)
You need to run your command in <
Note: Use first EOF in quote like <<'EOF' but not the last one. Else you will see you code will behave weirdly.
Please see below.
Example:
sudo -i <<'EOF'
<your script here>
EOF
Then, finally run it using Plink:
plink -ssh username#hostname -pw password -m commands.txt
Have you tried putting the command and argument in quotes:
i.e. -m "C:\myscript.sh 5"

Use batch file to insert command in a prompt opened by another batch file

I am using the QGIS software, which includes the OSGeo4W.bat file. This file opens a prompt, rewrite the path variables and include some others, like the python2 enviroment and some sitepackages like the Qt4 installed with QGIS. When opened the .bat file, it opens:
The problem is that i need to insert many commands in here so many times per day, like this one that converts a .ui file made by QtDesigner in .py:
pyuic4 -x C:\Users\Roberto\a.ui -o C:\Users\Roberto\a.py
As this is too much time consuming, i decided to write a batch file, call the OSgeo4W.bat and just add theses commands, but it doesnt work. The commands after the call are not runned. How can i run commands in a batch file inside the prompt created by another batch file? I am using Windows8.1. my batch file
#echo off
call "C:\Program Files\QGIS 2.18\OSGeo4W.bat"
pyuic4 -x C:\Users\Roberto\a.ui -o C:\Users\Roberto\a.py
rem more codes here
pause
You could try the Start command to execute the commands, you could also use timeout to wait before each of the executions.
#echo off
call "C:\Program Files\QGIS 2.18\OSGeo4W.bat"
start pyuic4 -x C:\Users\Roberto\a.ui -o C:\Users\Roberto\a.py
start rem more codes here
//you can use timeout 5 to wait to execute next command
start rem ***
start rem ***
pause

Command Prompt (cmd) is not executing Plink SSH Command

I am trying to execute a batch file (.bat) with the following commands in the batch file.
I have a batch file containing the following;
#ECHO OFF
cmd.exe /K "cd C:\Program Files (x86)\PuTTY && C:"
set PATH=%PATH%;C:\Program Files (x86)\PuTTY
pause
plink.exe -ssh username#firewall1 -pw PassWord! < commands.txt > c:\output_.csv"
pause
the plink.exe command works when entered in manually.
commands.txt is just a simple firewall command for now.
All I see when running the batch file is a cmd window open point at the Putty folder, and that's it.
So how can i get this to run please?
Your batch file at line 2, opens a new cmd window. That's not probably what you want. Also it changes the current directory and the current drive, read HELP CD, and then change that line to just cd /d "C:\Program Files (x86)\PuTTY"
Also, as a bonus recommendation, you are changing the PATH every time you run this command, adding the putty directory that you mage as current anyway, so this command is unnecessary and redundant.
So, I would leave your bat as simple as
#ECHO OFF
CD /D "C:\Program Files (x86)\PuTTY"
plink.exe -ssh username#firewall1 -pw PassWord! <commands.txt >c:\output_.csv
I would suggest making the batch file simpler still:
#Start "" /D "%ProgramFiles(x86)%\PuTTY" plink.exe -ssh username#firewall1 -pw PassWord! <commands.txt >C:\output_.csv
You may consider running other options such as /MIN, type Start /? at the command prompt for usage information

How to execute multiple commands in command prompt using batch file?

I am writing a batch file to execute the command (to execute Bitvise SFTP)
start cmd /k sftpc [SERVERNAME] -pk=1
After which the command prompt ask the following
Accept and (S)ave/(A)ccept for This Session/(C)ancel (S/A/C)?
Now the batch file should execute the next command as "A" to go through process.
I want automate the process of providing input "A" in command prompt. How can a batch file execute commands continuously ?
Updated :
After accepting the session, there are two more commands that needs to be executed,
cd [RemoteLocation]
get *.*
How to automate these two commands after the first two commands using the same batch file ?
You could try this to send an "A" to sftpc:
start cmd /k "echo A | sftpc [SERVERNAME] -pk=1"
I don't have sftpc and am just trying a suggestion to help out...
Updated:
If you have multiple commands to give to sftpc, you may find that one of the following methods works, depending on how sftpc works - I am working blind here - still don't have, or want, sftpc.
start cmd /k "echo A & echo cd somewhere & echo get *.* | sftpc [SERVERNAME] -pk=1"
... change "somewhere" to wherever you want to go to.
Or maybe:
Save the following in a file called ftp.cmd
A
cd somewhere
get *.*
Then try:
start cmd /k "sftpc [SERVERNAME] -pk=1 < ftp.cmd"

Batch file for PuTTY/PSFTP file transfer automation

I have a batch file for moving file from my local PC to server through SFTP. I have PuTTY installed in my system and the batch file code follows.
cd C:\Program Files (x86)\PuTTY
psftp
open <IP>
<user>
<PW>
cd /home/irisuser/iris/integration/dls_dlsblr_dlschnn_in_msg/in
lcd d:\
put log.sh
bye
The above code perfectly works when I type it in command prompt. But when I double click the .bat file and run it, it's not running and asking for username and password to be entered. My aim was to automate the whole thing and I need to run it by simply clicking the .bat file. But am not able to achieve it. Any ideas or snippets will help me.
You need to store the psftp script (lines from open to bye) into a separate file and pass that to psftp using -b switch:
cd "C:\Program Files (x86)\PuTTY"
psftp -b "C:\path\to\script\script.txt"
Reference:
https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter6.html#psftp-option-b
EDIT: For username+password: As you cannot use psftp commands in a batch file, for the same reason, you cannot specify the username and the password as psftp commands. These are inputs to the open command. While you can specify the username with the open command (open <user>#<IP>), you cannot specify the password this way. This can be done on a psftp command line only. Then it's probably cleaner to do all on the command-line:
cd "C:\Program Files (x86)\PuTTY"
psftp -b script.txt <user>#<IP> -pw <PW>
And remove the open, <user> and <PW> lines from your script.txt.
Reference:
https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter6.html#psftp-starting
https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter3.html#using-cmdline-pw
What you are doing atm is that you run psftp without any parameter or commands. Once you exit it (like by typing bye), your batch file continues trying to run open command (and others), what Windows shell obviously does not understand.
If you really want to keep everything in one file (the batch file), you can write commands to psftp standard input, like:
(
echo cd ...
echo lcd ...
echo put log.sh
) | psftp <user>#<IP> -pw <PW>
Though this has side effects. For example, if the host is not known to plink (like if you run it first time on a new machine or under another local account, for example under Task Scheduler), the first line of input will be taken as a response to the host key prompt. Anything except for y/i/Enter is interpreted as as n (connect just once, without adding the key to the cache), so even the cd command. And the rest of the script will fail as the cd does not happen.
set DSKTOPDIR="D:\test"
set IPADDRESS="23.23.3.23"
>%DSKTOPDIR%\script.ftp ECHO cd %PAY_REP%
>>%DSKTOPDIR%\script.ftp ECHO mget *.report
>>%DSKTOPDIR%\script.ftp ECHO bye
:: run PSFTP Commands
psftp <domain>#%IPADDRESS% -b %DSKTOPDIR%\script.ftp
Set values using set commands before above lines.
I believe this helps you.
Referre psfpt setup for below link https://www.ssh.com/ssh/putty/putty-manuals/0.68/Chapter6.html

Resources