代码之家  ›  专栏  ›  技术社区  ›  Albert

向stdin发送命令并发送发送结束(ctrl+d)

  •  2
  • Albert  · 技术社区  · 16 年前

    我想运行以下代码。

    我调用的应用程序需要诸如用户名=albert下一行、另一个命令等命令,直到完成为止。

    如果要从命令行手动输入此命令,请键入command,按Enter,键入command,按Enter。在最后一个命令后按Enter键的末尾,您将按Ctrl+D键结束它,运行程序并返回,说明发生了什么。

    如果您输入了错误的命令,应用程序会说“expering=”表示您发送的行格式不正确…

    下面我要做的是模拟手动输入,一个命令,新行,一个命令,新行等,直到结束,然后在最后一个命令后面附加ctrl+d。这不起作用,程序说“expering=”

    有什么明显的问题吗?

    谢谢你的时间。 艾伯特

            // CREATE DISCONNECT FILE
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("User-Name=" + this.userName);
            sb.AppendLine("Acct-Session-Id=" + this.sessionID);
            sb.AppendLine("NAS-IP-Address=" + Setting.Item["EDGE.NASIP"]);
            sb.AppendLine("Framed-IP-Address=" + this.currentIP);
            sb.Append("/x4");
            System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo();
            procStartInfo.WorkingDirectory = radclientPath.Substring(0, radclientPath.LastIndexOf("\\"));
            procStartInfo.FileName = radclientPath;
            procStartInfo.Arguments = @"-r 1 -d ..\etc\raddb -x 192.168.1.240:3799 disconnect password";
            procStartInfo.RedirectStandardInput = true;
            procStartInfo.RedirectStandardError = true;
            procStartInfo.RedirectStandardOutput = true;
            procStartInfo.UseShellExecute = false;
    
            System.Diagnostics.Process proc = System.Diagnostics.Process.Start(procStartInfo);
    
            proc.StandardInput.Write(sb.ToString());
            proc.WaitForExit(5000);
    
            if (proc.HasExited)
            {
                System.IO.File.WriteAllText(@"D:\temp.txt", proc.StandardOutput.ReadToEnd());
                System.IO.File.WriteAllText(@"D:\temp2.txt", proc.StandardError.ReadToEnd());
                string message = proc.StandardOutput.ReadToEnd() + "\n---\nErrors:\n---\n" + proc.StandardError.ReadToEnd();
                return message;
            }
            System.IO.File.WriteAllText(@"D:\temp.txt", proc.StandardOutput.ReadToEnd());
            System.IO.File.WriteAllText(@"D:\temp2.txt", proc.StandardError.ReadToEnd());
            return "";
        }
    

    我还尝试用单引号和零开头的数字(\x04)也没有,所以我尝试写什么应该发送到程序的文本文件。 然后我使用下面相同的命令行参数运行程序。复制了整个文本文件中的内容,并将其粘贴到命令提示符中,这样做很好。不知道为什么它没有正确发送到stdin。

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("User-Name=" + this.userName);
            sb.Append('\x04');
            System.IO.File.WriteAllText(@"D:\input.txt", sb.ToString());
            System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo();
            procStartInfo.WorkingDirectory = radclientPath.Substring(0, radclientPath.LastIndexOf("\\"));
            procStartInfo.FileName = radclientPath;
            procStartInfo.Arguments = @"-r 1 -d ..\etc\raddb -x 192.168.1.240:3799 disconnect password";
            procStartInfo.RedirectStandardInput = true;
            procStartInfo.RedirectStandardError = true;
            procStartInfo.RedirectStandardOutput = true;
            procStartInfo.UseShellExecute = false;
            System.Diagnostics.Process proc = System.Diagnostics.Process.Start(procStartInfo);
            proc.StandardInput.Write(sb.ToString());
            proc.WaitForExit(5000);
    
    2 回复  |  直到 16 年前
        1
  •  2
  •   SLaks    16 年前

    你需要写附加 "\x04" 用一个长柄( \ )

    有关详细信息,请参阅 documentation (字符串转义序列部分)

        2
  •  0
  •   Blair Conrad    16 年前
    sb.Append("/x4");
    

    这不是Control-D

    也许你的意思是

    sb.Append("\x04");