发送命令到stdin和发送结束(Ctrl + D) [英] Sending commands to stdin and sending end of transmit (Ctrl+D)

查看:427
本文介绍了发送命令到stdin和发送结束(Ctrl + D)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行以下代码。

我调用的应用程序需要像User-Name = albert下一行,另一个命令等命令,

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

如果您要从命令行手动输入,请输入命令,按回车,键入命令按回车键。在最后一个命令之后按下ENTER键,你将按Ctrl + D结束它,运行程序,并回来说明发生了什么。

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.

如果你键入一个命令错误,应用程序说expecting =意味着您发送的格式不正确的行...

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

因此,我在下面做的是模拟手动键入,一个命令,新行,一个命令,新行等,直到结束,然后我只是在最后一个命令之后追加Ctrl + D。这不工作,程序说expecting =

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 ="

任何明显的问题?

时间。
Albert

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



我也尝试过单引号, \\ x04),也没有什么,所以我试着写应该发送到程序到一个文本文件。
然后我使用相同的命令行参数运行程序如下。复制整个文本文件中的内容,并将其粘贴到命令提示符,这很好。不确定为什么它不能正确发送到stdin ..

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


推荐答案

\x04,带有反斜杠( \

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

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

这篇关于发送命令到stdin和发送结束(Ctrl + D)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆