如何在cmd中传递带有特殊字符的密码?

如何在cmd中传递带有特殊字符的密码?,cmd,escaping,special-characters,Cmd,Escaping,Special Characters,我正在使用plink通过ssh连接到许多服务器并执行命令。但是,当密码具有特殊字符时,无法在控制台上执行命令 用法:plink.exe-ssh-pw“密码”user@myhost“要执行的命令” apof~!@$%^&*():“sdfsdfs!df-使用\进行转义时成功” apof~!@$%^&*():“sdfsdfs!df-使用\进行转义时成功” apof~!@$%^&*():“sdfsdfs!df-失败。使用\转义”。失败消息:>此时意外出现 apof~!@$%^&*():“>sdfsdfs

我正在使用
plink
通过ssh连接到许多服务器并执行命令。但是,当密码具有特殊字符时,无法在控制台上执行命令

用法:
plink.exe-ssh-pw“密码”user@myhost“要执行的命令”

apof~!@$%^&*():“sdfsdfs!df
-使用
\
进行转义时成功”

apof~!@$%^&*():“sdfsdfs!df
-使用
\
进行转义时成功”

apof~!@$%^&*():“sdfsdfs!df
-失败。使用
\
转义
。失败消息:>此时意外出现

apof~!@$%^&*():“>sdfsdfs!df
失败。使用
\
转义
。失败消息:系统找不到指定的路径


看起来像是
出现在
,转义无效。许多我知道如何处理这个问题的人意识到问题不在于plink如何转义特殊字符,而在于cmd如何处理特殊字符。为此,我创建了一个简单的Java类来获取命令行参数并打印它:

public class MyTest {
    public static void main(String args[]) {
        System.out.println(args[0]);
    }
}
我将带有特殊字符的输入传递到此应用程序,以下是输出:

    C:\>java MyTest "apof~!@#$%^&*()_+{}|:"<>?-=`[]\;',./sss"
    > was unexpected at this time.

    C:\>java MyTest "apof~!@#$%^&*()_+{}|:\"<>?-=`[]\;',./sss"
    > was unexpected at this time.

    C:\>java MyTest "apof~!@#$%^&*()_+{}|:\"^<^>?-=`[]\;',./sss"
    apof~!@#$%^&*()_+{}|:"<>?-=`[]\;',./sss

    C:\>java MyTest "apof~!@#$%^&*()_+{}|:\"^<^>?-=`[]\;',./s\"sasfafds"
    apof~!@#$%^&*()_+{}|:"<>?-=`[]\;',./s"sasfafds

    C:\>java MyTest "apof~!@#$%^&*()_+{}|:\"^<^>?-=`[]\;',./s\"sasf<>afds"
    apof~!@#$%^&*()_+{}|:"<>?-=`[]\;',./s"sasf<>afds

    C:\>java MyTest "apof~!@#$%^&*()_+{}|:\"^<^>?-=`[]\;',./s\"sasf<>af\"ds"
    apof~!@#$%^&*()_+{}|:"<>?-=`[]\;',./s"sasf<>af"ds

    C:\>java MyTest "apof~!@#$%^&*()_+{}|:\"^<^>?-=`[]\;',./s\"sasf<>af\"dasd<>fs"
    > was unexpected at this time.

    C:\>java MyTest "apof~!@#$%^&*()_+{}|:\"^<^>?-=`[]\;',./s\"sasf<>af\"dasd^<^>fs"

    apof~!@#$%^&*()_+{}|:"<>?-=`[]\;',./s"sasf<>af"dasd<>fs

    C:\>java MyTest "apof~!@<>#$%^&*()_+{}|:\"^<^>?-=`[]\;',./s\"sasf<>af\"dasd^<^>fs"
    apof~!@<>#$%^&*()_+{}|:"<>?-=`[]\;',./s"sasf<>af"dasd<>fs
我仍然对使用转义参数调用可执行文件有问题。我使用以下方式执行命令:

subprocess.Popen('plink.exe -ssh -batch -pw' + escapepassword(password) + ' ' + username + '@' + ipaddress + ' "' + command + '"', stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
或者说在Java中,它将是:

Runtime.getRuntime().exec("plink.exe -ssh -batch -pw" + escapepassword(password) + " " + username + "@" + ipaddress + " \"" + command + "\"");
这给我们带来了很多麻烦。python或Java执行流程的方式不同。特殊字符被转义并传递给进程。因此,它产生了更多的问题。为了处理此问题,我将上述代码更改为:

commandtoexecute = ['plink.exe', '-ssh', '-batch', '-pw', password, username + '@' + ipaddress, command]
subprocess.Popen(commandtoexecute, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
在Java中,可以写成:

Runtime.getRuntime().exec(new String [] {"plink.exe", "-ssh", "-batch", "-pw", password, username + "@" + ipaddress, command);
在这里,命令之间有空格,例如
ls-al
,但这是传递参数以启动进程的方法。当我尝试在
之间传递命令时,它给出了一个错误,表示找不到命令,因为
也被传递到远程系统上执行

注意:
我使用命令
WMIC path win32_process get name,Commandline | find“plink.exe”
查看命令行参数。输出是html转义的,但是,它有助于找到传递的参数。

Percentage%在cmd脚本中也需要转义,因为cmd认为您传递的是一个变量-可以使用double%%。更多-
Runtime.getRuntime().exec(new String [] {"plink.exe", "-ssh", "-batch", "-pw", password, username + "@" + ipaddress, command);