向标准输入发送命令并发送传输结束 (Ctrl+D)

发布于 2024-08-23 21:13:03 字数 3157 浏览 5 评论 0原文

我想运行以下代码。

我调用的应用程序需要诸如 User-Name=albert 之类的命令,下一行,另一个命令等等,直到完成。

如果您要从命令行手动输入此内容,则需要键入命令,按 Enter 键,然后键入命令按 Enter 键。在最后一个命令后按 ENTER 键后,您可以按 Ctrl + D 结束它,运行程序并返回说明发生了什么。

如果您输入错误的命令,应用程序会显示“expecting =”,这意味着您发送了一行格式不正确的行...

所以我下面要做的是模拟手动输入此命令,一个命令,一个新行,一个命令、新行等,直到最后,然后我只需在最后一个命令后附加 Ctrl+D 即可。这不起作用,程序显示“期望=”

有任何明显的问题吗?

感谢您抽出时间。 艾伯特

        // 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),但什么也没有,所以我尝试将应该发送到程序的内容写入文本文件。 然后我使用与下面相同的命令行参数运行该程序。复制整个文本文件中的内容并将其粘贴到命令提示符中,效果很好。不知道为什么它没有正确发送到标准输入..

        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);

I want to run the following code.

The application that I'm calling expects commands like User-Name=albert next line, another command and so on, until you are done.

If you were to enter this manually from command line, you would type command, press enter, type command press enter. Right at the end AFTER PRESSING ENTER after the last command, you would press Ctrl + D to end it, that runs the program and comes back stating what happened.

If you type a command wrong, the application says "expecting =" meaning that you sent a line that is not formatted right...

So what I'm doing below is simulating typing this in manually, one command, new line, one command, new line etc, until the end, then I just append Ctrl+D after the last command. This doesn't work and the program says "expecting ="

Any obvious issues?

Thanks for your time.
Albert

        // 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 "";
    }

I also tried it in single quotes and with a zero leading the number (\x04) and nothing either, so I tried writing what should be sent to the program to a text file.
I then ran the program using the same command line params as below. Copied what was in the entire text file and pasted it into the command prompt, and that works fine. Not sure why it's not sent correctly to the 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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

成熟稳重的好男人 2024-08-30 21:13:03

您需要编写附加 "\x04",并带有反斜杠 (\)

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

You need to write append "\x04", with a backslah (\)

For more information, see the documentation (String Escape Sequences section)

关于从前 2024-08-30 21:13:03
sb.Append("/x4");

这不是一个 control-D

也许你的意思是

sb.Append("\x04");
sb.Append("/x4");

this isn't a control-D

Perhaps you meant

sb.Append("\x04");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文