6

I have a deployment script to which i have to pass LDAP password as cmd paramater.

actual password:  foo\$ser"ver\\ 1  (contains three space characters: at the beginning, before 1, and after 1)

e.g

...bin>deployment.bat LDAPPassword= foo\$ser\"ver\\ 1 

Note:There are spaces in the password as shown at the beginning.

The deployment.bat calls a class to which the above parameter is passed as an argument. The problem is that the class receives 2 distinct arguments:

args[0]= foo\$ser"ver\\            //The space after \\ is omitted
args[1]=1                          //The space before and after 1 is omitted

How do I pass this password so that it is received as single string?

I have already tried quoting the password as

...bin>deployment.bat LDAPPassword=" foo\$ser"ver\\ 1 "

however it won't work.

Andriy M
  • 76,112
  • 17
  • 94
  • 154
bluelurker
  • 1,353
  • 3
  • 19
  • 27
  • 1
    please provide the code for more information – Keval Trivedi May 21 '14 at 05:51
  • Perhaps you could elaborate on what is going on inside `deployment.bat`, specifically how the argument is processed and passed to the class you are mentioning. Also, I've edited your post to try and make your issue clearer, please review and feel free to edit/roll back as necessary. – Andriy M May 23 '14 at 07:21
  • try replace the space with `%20` and see if it works. It works for me when running remote scripts and bat files for SAP. – Estevex Nov 24 '16 at 14:50

2 Answers2

3
" foo\$ser\"ver\ 1 "

To quote the full string you need to escape the quote so it is not seen as an ending quote.

And in the cases where the quote is already prefixed with a backslash, then you will also have to escape the backslash. So a password as

this isa\"test

should be written as

"thisisa\\\"test"

For more information, this is the "usual" way of parsing arguments in windows.

EDITED - Just to document tests. Using this batch file (test.cmd)

@echo off
    cls
    echo(--------------------------------------------
    echo [%1][%2][%3][%4][%5][%6][%7][%8][%9]
    echo(--------------------------------------------
    cmdline.exe %1 %2 %3 %4 %5 %6 %7 %8 %9
    echo(--------------------------------------------
    cmdline.exe %*
    echo(--------------------------------------------
    java CmdLine  %1 %2 %3 %4 %5 %6 %7 %8 %9
    echo(--------------------------------------------
    java CmdLine %*
    echo(--------------------------------------------

where cmdline.exe is obtained from this c code

#include "windows.h"
#include "stdio.h"

void main(int argc, char **argv){
    int i;
    printf("cmdline:[%s]\r\n\r\n",GetCommandLine());
    for(i = 0; i < argc ; i++) printf("arg_%03d:[%s]\r\n",i,argv[i]);
};

and CmdLine.java is

public class CmdLine {
    public static void main (String[] args) {
        int i = 0;
        for (String s: args) {
            System.out.println(String.format("arg_%03d:[%s]",++i,s));
        }
    }
}

Running test.cmd " foo\$ser\"ver\ 1 " the results are

--------------------------------------------
[" foo\$ser\"ver\][1]["][][][][][][]
--------------------------------------------
cmdline:[cmdline.exe  " foo\$ser\"ver\ 1 "      ]

arg_000:[cmdline.exe]
arg_001:[ foo\$ser"ver\ 1 ]
--------------------------------------------
cmdline:[cmdline.exe  " foo\$ser\"ver\ 1 "]

arg_000:[cmdline.exe]
arg_001:[ foo\$ser"ver\ 1 ]
--------------------------------------------
arg_001:[ foo\$ser"ver\ 1 ]
--------------------------------------------
arg_001:[ foo\$ser"ver\ 1 ]
--------------------------------------------
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • `... this is the "usual" way of parsing arguments in windows` but not for batch files. The backslash doesn't need any escaping – jeb May 23 '14 at 09:05
  • 1
    @jeb, not in batch. But the problem is to get the indicated password inside the final program (the class being called, as OP states) passing it as a parameter to a batch file. It does not matter what the batch file sees, the question is what is needed to transport a command line value into this class and that the class sees the correct value. And it is the argument parser of the program which requires the different escaping of characters. – MC ND May 23 '14 at 09:19
  • Ok, you are right, I reduced the problem to only tranfer the password to the batch file not to the final program – jeb May 23 '14 at 09:26
3

You can't escape your password in any way.
As some combinations with quotes together with spaces can't be placed into one parameter.

Like this " (<space><quote><space>) even if you add some quotes around, it's not possible, even if you quote the spaces and the quotes itself.

myBat  " 
myBat " " "
myBat ^ " 
myBat ^"^ ^"^ ^"

Here is the best to double all quotes inside your password and enclose your password into quotes.
Then you can use all other characters are without any problems.

deployment.bat " foo\$ser\""ver\ 1 "

And in deployment.bat

@echo off
setlocal DisableDelayedExpansion
set "pwd=%~1"
setlocal EnableDelayedExpansion
set "pwd=!pwd:""="!"
echo pwd='!pwd!'

You should use your password only with delayed expansion to avoid problems with special characterts.

For accessing command line parameters you could also look at
SO: How to receive even the strangest command line parameters?

Community
  • 1
  • 1
jeb
  • 78,592
  • 17
  • 171
  • 225