How to include pipe character in an argument to a batch file from a bash script? - batch-file

I have a shell script that I want to execute this line:
qtvars.bat vsstart "qt.sln" /BUILD "Debug|Win32"
This works fine (though I had to modify qtvars.bat, but that's beside the point). The problem is that I want the command to execute to be in a variable:
EDIT: This doesn't work either, if I type it into bash. Previously I was typing it into cmd.exe, which hardly made for a fair comparison.
command="qtvars.bat"
args="vsstart"
$command $args "qt.sln" /BUILD "Debug|Win32"
Now it chokes on the pipe! I get this message:
'Win32' is not recognized as an internal or external command,
operable program or batch file.
I've tried a bunch of forms of escaping the quotes and/or pipe, all to no avail. Interestingly, it works when it's an executable rather than a batch file, e.g.:
command="devenv.exe"
args=""
$command $args "qt.sln" /BUILD "Debug|Win32"
Thanks for any ideas.

I know you "escape" the pipe character in a batch file with the ^ character, so...
echo ^| Some text here ^|
Would display...
| Some text here |
I don't know whether that would help you in this instance? Maybe try prepending each pipe character with a ^ and see what happens? :-)

This is a classic case of double-escaping, where both bash and CMD.EXE need to be instructed to ignore the special | (pipe) character.
Try the following:
$command $args "qt.sln" /BUILD '"Debug|Win32"'
This will be the equivalent of you typing, at a CMD.EXE prompt:
qtvars.bat vsstart qt.sln /BUILD "Debug|Win32"
Using the above, you are essentially forcing the passing of the double-quotes on to CMD.EXE (instead of bash eating them away.) The outermost single quotes instruct bash not to interpret or touch in any way what's inside them; the inner double-quotes instruct CMD.EXE to ignore any special characters (the pipe in this case) within.
Alternatively, you can also try:
$command $args "qt.sln" /BUILD 'Debug\|Win32'
This should be the equivalent of you typing, at a CMD.EXE prompt:
qtvars.bat vsstart qt.sln /BUILD Debug\|Win32
Note the use of single quotes (!), which ensure that bash will not interpret the \ (and, instead, will pass it as-is to CMD.EXE.)

Here's another solution (workaround?) I've found:
first, ensure an environment variable defines the pipe character, for example:
set PIPE="|"
later, run the command specifying the above defined environment variable name:
"c:\(...)\devenv.com" foo.sln /build Debug%PIPE%Win32
That does the job even if there are multiple wrappers between the caller and the callee. I'm now using it with a very long chain of wrappers:
Python/Linux -> VirtualBox guest's executeProcess -> Cmd/Windows -> devenv.com
(cross posted to: How to pass a quoted pipe character to cmd.exe?)

Escaping a piping character in the Windows scripting language is done with a caret (^). I just had to do this the other day. I know this is old, but I thought I would post what I found in case others ran across this, like I did.

I'd consider going the easy route, and passing a placeholder-token instead - "$P", and then replace it within the CMD/Batch file; e.g. using the 'UnxUtils' SEd command to do the replacement:
For /F "usebackq delims=" %%r in (`Echo %Cmd% ^| sed -e "s/$P/|/g"`) do #Set Cmd2=%%r
REM Now run the command, with the proper pipe symbol in place
%Cmd2%
So having passed the command arg/CMD script args - "git status $P wc -l".

Interesting! What does escaping the | do?
Do these work?
echo "Debug|Win32"
echo "qt.sln" /BUILD 'Debug|Win32'

Related

directly quoting %~dp0 does not ignore spaces in file path

i have a script which 3 arguments, two of which are paths. I would like to call it from a .bat file.
python myscript.py "%~dp0inputs/" "%~dp0outputs/" "foo"
If i call a dummy script that simply prints argv using the above line, i get the expected results, even for paths containing spaces:
myscript.py
C:\path\containing spaces\inputs/
C:\path\containing spaces\outputs/
foo
however if i use this (ie %~dp0 for argument 1 with nothing else between the quotes):
python myscript.py "%~dp0" "%~dp0outputs/" "foo"
then it behaves oddly when the path contains spaces:
myscript.py
C:\path\containing spaces" C:\path\containing
spaces\outputs/ foo
it seems that the quotes have not been processed properly - what have i done wrong?
It is because the quotes are passed into the python script.
And %~dp0 ends with a \ character.
Python figures that \ escapes the double quote - fun.
So, "%~dp0" is passed to the script as
"C:\temp\containing spaces\"
Python treats that trailing \ as escaping the quote, so it merges the arguments.
It shouldn't do that - but it does.
You can remove the trailing \, or add another.
Either of these works:
python myscript.py "%~dp0/" "%~dp0outputs/" "foo"
python myscript.py "%~dp0\" "%~dp0outputs/" "foo"
try to use Set to call your paths within batch. (example below)
#echo off
set myscrpt="C:\path\containing spaces\mypythonscript.py"
set mypath1="C:\path\containing spaces\inputs"
set mypath2="C:\path\containing spaces\outputs"
pushd C:\Python27
Python %myscrpt% %mypath1% %mypath2%
pause>NUL
exit

Error passing multiple commands to Cisco CLI via plink

I've gotten some help with an earlier part of this batch file, but now I'm having trouble with the final component.
I've tried a few things with no success. I tried changing the CRLF to LF which did nothing. I also tried rephrasing the commands a few ways but I am still not getting anywhere. The following is my main batch file.
#echo on
REM delete deauth command file
SET OutFile="C:\temp\Out2.txt"
IF EXIST "%OutFile%" DEL "%OutFile%"
plink -v -ssh *#x.x.x.x -pw PW -m "c:\temp\WirelessDump.txt" > "C:\temp\output.txt"
setlocal
for /f %%a in (C:\temp\output.txt) do >> "Out2.txt" echo wir cli mac-address %%a deauth forced
REM Use commands in out2 to deauth
plink -v -ssh *#x.x.x.x -pw PW -m "c:\temp\Out2.txt"
pause
Below this sentence is the command found in Out2 which I think is giving the actual trouble. The number of lines varies but they are all this particular command just with differing MACs.
wir cli mac-address xxxx.xxxx.xxxx deauth forced
If Out2 has only a single line it runs fine, no issues. But when there are multiple lines, it fails with an error stating that the Line has an invalid autocommand. It's almost as if it was reading it as one contiguous command. As I mentioned above I changed from CRLF to LF hoping IOS would like it better, but that failed. I've tried adding extra lines between the commands, and I've tried calling the login every time from that file.
I am hoping that there is a way to tailor the commands to pass all lines one at a time to keep this down to a minimum of files.
I had another thought but it is kinda/very clunky. If there was a way to output each of those MAC deauth commands to their own file in a saperate folder (out1, out2, out3), and have the BAT able to run all the randomly generated files in that folder so that each one is a separated plink session.
Let me know if I need to change/add/elaborate on anything. Thanks in advance for anything you guys are willing to help with. I appreciate it.
EDIT: Martin has pointed out what the limitation actually is. It appears to be a limitation on Cisco to accept blocks of commands through SSH. So I still have the same question really, I just need some help figuring a workaround to this issue. I'm thinking the multiple file solution I mentioned above may have some possibility. But I'm too much of a noob to know how to make that work. I'll update if I have any breakthroughs though. Thanks for any contributions!
It's actually a known limitation of Cisco, that it does not support multiple commands in an SSH "exec" channel command.
Quoting section 3.8.3.6 -m: read a remote command or script from a file of PuTTY/Plink manual:
With some servers (particularly Unix systems), you can even put multiple lines in this file and execute more than one command in sequence, or a whole shell script; but this is arguably an abuse, and cannot be expected to work on all servers. In particular, it is known not to work with certain ‘embedded’ servers, such as Cisco routers.
Though you can probably still feed multiple commands to Plink input:
(
echo command 1
echo command 2
echo command 3
echo exit
) | plink -v -ssh user#host -pw password > output.txt
Or you can simply use an input file:
plink -v -ssh user#host -pw password < input.txt > output.txt
Similar question: A way of typing multiple commands in cmd.txt file using PuTTY batch against Cisco
This works without cmd.exe and using files:
function Invoke-PlinkCommandsIOS {
param (
[Parameter(Mandatory=$true)][string] $Host,
[Parameter(Mandatory=$true)][System.Management.Automation.PSCredential] $Credential,
[Parameter(Mandatory=$true)][string] $Commands,
[Switch] $ConnectOnceToAcceptHostKey = $false
)
$PlinkPath="$PSScriptRoot\plink.exe"
$commands | & "$PSScriptRoot\plink.exe" -ssh -2 -l $Credential.GetNetworkCredential().username -pw "$($Credential.GetNetworkCredential().password)" $Host -batch
}
Usage: dont forget your exit's and terminal length 0 or it will hang
PS C:\> $Command = "terminal lenght 0
>> show running-config
>> exit
>> "
>>
PS C:\> Invoke-PlinkCommandsIOS -Host ace-dc1 -Credential $cred -Commands $Command
....
Sounds like your file 'Out2.txt' has only LF at end of line. Simple way to convert that to CRLF is to use MORE command and redirect output to a new file and then use the new file.
more Out2.txt > Out2CRLF.txt
I ran into the same issue when trying to pull the full list of ACLs on an ASA via plink in powershell.
Essentially, due to the abuse issue referenced in the documentation: https://the.earth.li/~sgtatham/putty/0.72/htmldoc/Chapter3.html#using-cmdline-m, I was getting inconsistent results in pulling the ACLs. Sometimes I would get 0, sometimes only 1 or 2, and sometimes I would get all of them. (I personally, had about a 1 in 5 success rate).
As I would occasionally be successful I used a while loop that would catch the unsuccessful attempts and retry. Just be sure to put some timing on the while loop to prevent it from spamming ssh connections too much.
It is not a good solution, but it worked as a last resort.

How can I use nested quotes when calling a powershell script within a batch file?

I have a DOS batch file that has a line that executes a powershell script. First I tried a very simple script with this line in the batch file:
powershell -command "get-date" < nul
That worked great. But the script has nested double-quote characters, which can sometimes be escaped with a backtick (`) character. So then I tried this:
powershell -command "Write-Host `"hello world`"" < nul
That also worked great. However, the script I need to run is pretty complicated and has more than one level of nested double-quote characters. I have taken the complicated script and simplified it to an example that has the same principles here:
[string]$Source = " `"hello world`" ";
Write-Host $Source;
If I save this script inside a PS script file and run it, it works fine, printing out “hello world” including the double quotes, but I need to embed it in the line in the batch file. So I take the script and put it all on one line, and try to insert it into the batch file line, but it doesn’t work. I try to escape the double-quotes, but it still doesn’t work, like this:
powershell -command "[string]$Source = `" `"hello world`" `";Write-Host $Source;" < nul
Is there a way to do what I want? You might ask why I am doing this, but it’s a long story, so I won’t go into the details.
thanks
You'll have to use a combination of batch's escape character and PowerShell's escape character.
In batch, when escaping quotes, you use the common shell backslash (\) to escape those quotes. In Powershell, you use the backtick `.
So if you wanted to use batch to print out a quoted string with Powershell, you need to first batch escape the quote to declare the variable in Powershell, then to ensure the string is quoted you need batch and Powershell escape another quote, and then your add your desired string, ensuring you batch escape first.
For your example, this will work:
powershell -command "[string]$Source = \"`\"hello world`\"\"; Write-Host $Source;"
Here's a break down of the declaration of the $Source variable:
"[string]$Source = # open quote to begin -command parameter declaration
\" # batch escape to begin the string portion
`\" # Powershell+Batch escape
hello world # Your content
`\" # Posh+Batch again
\"; # Close out the batch and continue
more commands " # Close quote on -command parameter
This renders the string like this in batch:
`"hello world`"
One note, you don't need to explicitly cast $Source as a string since you are building it as a literal string from scratch.
$Source = "string stuff" will work as intended.

Batch file: <part of command line> was unexpected at this time

Trying to execute a command inside a batch file and drop the result into a variable.
Here's what I would execute interactively:
curl -d "username=someUser" http://someServer/trusted
The result is a 15-20 character alpha-numeric string.
Here's my attempt at doing same in a batch file:
FOR /f %AA IN('curl -d \"username=someUser\" http://someServer/trusted') DO ECHO %AA
Error returned is:
//someServer/Trusted') was unexpected at this time
I thought I was dealing with some sort of escaping issue, so I added the \ symbols in front of my quotes. From what I've read, the : symobol in http doesn't need to be escaped, but it's interesting to me that's where the failure appears to "start".
Any ideas on this? I'm on Win8.1, FYI
for-variables have one-char-variables (%A instead of %AA)
you missed a space between IN and (
in a batch-file you have to double the percent-signs for the for-variable (%%A instead of %A)
echo handles only one line, blanks brake it, you would need to pipe it maybe?

Open Cygwin on a path passed as the first parameter to bash.exe

I'm going mad trying to achieve this. I read a lot of questions over Stack Overflow, but they didn't work. I'm trying to open a tab on console2 that opens Cygwin on the path passed as a parameter to it.
For cmd.exe, is quite easy:
cmd.exe %1
For Cygwin, this looks really hard:
bash --login -i -c 'cd `cygpath \'D:\Program Files\'`;exec bash'
The problem here is that with path without spaces it works well, with spaces, it doesn't work. Also, I don't know how to pass a param to it, maybe $1 or %1?
Edit 1:
I'm almost there, I created this batch file that should be run instead of bash.exe directly:
#echo off
set CHERE_INVOKES=%CD%
set TORUN="D:\Program Files\Cygwin\bin\bash.exe" --login -i -c 'cd "%CHERE_INVOKES%"; exec bash'
echo %TORUN%
call %TORUN%
PAUSE
This works with all paths except C: (and D:), the reason? Windows is stupid, and instead of having a path called C:, it has a path called C:!!! So, while all paths ends without a backslash, the first path ends with it, driving me mad!
The following command works for me:
c:\cygwin\bin\bash --login -i -c "cd '%~1'; exec /bin/bash.exe"
Where %~1 expands %1 removing any surrounding quotes (") — see help for in command prompt.
See also: chere package in Cygwin, and ConEmu terminal :)
Here is the solution:
#echo off
set CHERE_INVOKES=%CD%
::Remove trailing slash if required
IF %CHERE_INVOKES:~-1%==\ SET CHERE_INVOKES=%CHERE_INVOKES:~0,-1%
set TORUN="D:\Program Files\Cygwin\bin\bash.exe" --login -i -c 'cd "%CHERE_INVOKES%"; exec bash'
call %TORUN%
I added this code from this question: Remove Trailing Slash From Batch File Input
::Remove trailing slash if required
IF %CHERE_INVOKES:~-1%==\ SET CHERE_INVOKES=%CHERE_INVOKES:~0,-1%
In this way I can use this batch file to open Console2 Cygwin on the current path.

Resources