Sending commands to stdin and sending end of transmit (Ctrl+D)

孤者浪人 提交于 2019-12-24 00:52:18

问题


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

回答1:


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

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




回答2:


sb.Append("/x4");

this isn't a control-D

Perhaps you meant

sb.Append("\x04");


来源:https://stackoverflow.com/questions/2374645/sending-commands-to-stdin-and-sending-end-of-transmit-ctrld

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!