我正在尝试用 C# 做一个“脚本化的 telnet”项目(有点类似于 Tcl Expect 包),为此我需要启动一个 telnet 进程并重定向(和处理)它的 stdin/stdout 流。
问题是,生成的 telnet 进程在启动后立即退出(但是它确实启动了)。
如果我尝试对 -say- cmd.exe 执行相同的操作,它会起作用。
telnet 实现是否需要交互式标准输入? Windows 中甚至存在这个概念吗?
我运行的是 Win7 64 位,这会导致描述的行为吗?
这是我使用的片段:
Process m_process = new Process();
m_process.StartInfo = new ProcessStartInfo(
@"C:\Windows\system32\telnet.exe", "towel.blinkenlights.nl");
//required to redirect stdXXX
m_process.StartInfo.UseShellExecute = false;
m_process.StartInfo.ErrorDialog = false;
m_process.StartInfo.RedirectStandardInput = true;
m_process.StartInfo.RedirectStandardOutput = true;
m_process.StartInfo.RedirectStandardError = true;
Thread.Sleep(50);
Console.WriteLine(" exited? " + m_process.HasExited);
输出是:
exited? True
如果我使用这个:
...
m_process.StartInfo = new ProcessStartInfo(
@"C:\Windows\system32\cmd.exe", "");
...
输出是
exited? False
最佳答案
无法重定向 Telnet.exe。可能的原因是它就像一个终端,模拟 VT100。当你启动它时,你会看到一个提示,它会清除屏幕。这只有在程序直接写入控制台屏幕缓冲区时才有可能。没有已知的方法让它表现得不一样,set term 命令没有非终端模式选项。
关于带重定向标准流的 C# + telnet 进程立即退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2212763/