子进程调用以执行复杂命令

时间:2017-03-30 10:05:11

标签: python-3.x subprocess

我需要在我的linux shell中启动以下命令。

./bin/winexe -U ladmin //135.249.24.214  --interactive=0 --system "cmd.exe /c powershell -c (new-object System.Net.WebClient).DownloadFile('http://135.249.21.229/fr_query/base64.tcl','C:\Users\ladmin\Desktop\successshibani456.csv')"

但是,使用subprocess.call()时,括号和引号不能正确匹配。

转义双引号无济于事。

subprocess.call("./bin/winexe -U ladmin //135.249.24.214  --interactive=0 --system \"cmd.exe /c powershell -c (new-object System.Net.WebClient).DownloadFile('http://135.249.21.229/fr_query/base64.tcl','C:\Users\ladmin\Desktop\successshibani456.csv')"\")

命令执行错误。

2 个答案:

答案 0 :(得分:0)

看起来错误可能出现在您的报价转义中 - 您的命令为:

subprocess.call("./bin/winexe -U ladmin //135.249.24.214  --interactive=0 --system \"cmd.exe /c powershell -c (new-object System.Net.WebClient).DownloadFile('http://135.249.21.229/fr_query/base64.tcl','C:\Users\ladmin\Desktop\successshibani456.csv')"\")

但也许它应该是:

subprocess.call("./bin/winexe -U ladmin //135.249.24.214  --interactive=0 --system \"cmd.exe /c powershell -c (new-object System.Net.WebClient).DownloadFile('http://135.249.21.229/fr_query/base64.tcl','C:\Users\ladmin\Desktop\successshibani456.csv')\"")

不是如何将最后一个双引号上的转义斜杠移动到在字符串内平衡的实际双引号。

您也可能发现需要转义字符串内部的反斜杠:

subprocess.call("./bin/winexe -U ladmin //135.249.24.214  --interactive=0 --system \"cmd.exe /c powershell -c (new-object System.Net.WebClient).DownloadFile('http://135.249.21.229/fr_query/base64.tcl','C:\\Users\\ladmin\\Desktop\\successshibani456.csv')\"")

答案 1 :(得分:0)

您可以让# ---------------------------------------------------------- # Problem Information # ---------------------------------------------------------- Problem: - Name: unknown Lower bound: 0.666666666667 Upper bound: 0.666666666667 Number of objectives: 1 Number of constraints: 2 Number of variables: 3 Number of nonzeros: 3 Sense: minimize # ---------------------------------------------------------- # Solver Information # ---------------------------------------------------------- Solver: - Status: ok Termination condition: optimal Statistics: Branch and bound: Number of bounded subproblems: 0 Number of created subproblems: 0 Error rc: 0 Time: 0.018000125885 # ---------------------------------------------------------- # Solution Information # ---------------------------------------------------------- Solution: - number of solutions: 1 number of solutions displayed: 1 - Gap: 0.0 Status: feasible Message: None Objective: OBJ: Value: 0.666666666667 Variable: x[1]: Value: 0.333333333333 Constraint: No values 模块为您完成大部分转义操作。您可以运行以下命令并验证它是否有效吗?该命令已分离为其组件参数。因为反斜杠字符位于最后一个参数中,所以字符串被写为raw。

var facebookOptions = new FacebookAuthenticationOptions()
{
    AppId = "xxxxx",
    AppSecret = "xxxxx",
};

// Set requested scope
facebookOptions.Scope.Add("email");
facebookOptions.Scope.Add("public_profile");

// Set requested fields
facebookOptions.Fields.Add("email");
facebookOptions.Fields.Add("first_name");
facebookOptions.Fields.Add("last_name");

facebookOptions.Provider = new FacebookAuthenticationProvider()
{
    OnAuthenticated = (context) =>
        {
            // Attach the access token if you need it later on for calls on behalf of the user
            context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken));

            foreach (var claim in context.User)
            {
                //var claimType = string.Format("urn:facebook:{0}", claim.Key);
                var claimType = string.Format("{0}", claim.Key);
                string claimValue = claim.Value.ToString();

                    if (!context.Identity.HasClaim(claimType, claimValue))
                        context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, "XmlSchemaString", "Facebook"));
            }

            return Task.FromResult(0);
       }
};

app.UseFacebookAuthentication(facebookOptions);