具有重定向的标准流的C#+ telnet进程立即退出 [英] C# + telnet process w/ redirected standard streams quits immediately

查看:69
本文介绍了具有重定向的标准流的C#+ telnet进程立即退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#进行脚本化的telnet"项目(有点像 Tcl Expect 包),我需要为其启动telnet进程并重定向(和处理)其stdin/stdout流.

I'm trying to do a 'scripted telnet' project in C# (something vaguely resembling the Tcl Expect package) for which I need to start a telnet process and redirect (and process) its stdin/stdout streams.

问题是,生成的telnet进程在启动后立即退出(但是确实启动了).
如果我尝试使用-say- cmd.exe进行相同操作,那么它将起作用.

The problem is, the spawned telnet process quits immediately after starting (it does start, however).
If I try to do the same with -say- cmd.exe, it works.

telnet实现是否需要交互式stdin?Windows中甚至存在这个概念吗?

Does the telnet implementation require an interactive stdin? Does this concept even exist in Windows?

我正在运行64位Win7,这是否有助于所描述的行为?

I'm running Win7 64 bit, could this contribute to the dscribed behaviour?

这是我使用的代码段:

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

输出为:

退出了吗?是

如果我使用这个:

...
m_process.StartInfo = new ProcessStartInfo(
                @"C:\Windows\system32\cmd.exe", "");
...

输出为

退出了吗?错误

推荐答案

您对 cmd.exe 进行的所有实验证明,您已经成功启动了 cmd.exe .:-)

All that your experiments with cmd.exe prove is that you're successfully launching cmd.exe. :-)

您是否尝试过在命令提示符窗口中执行 C:\ Windows \ system32 \ telnet.exe毛巾.blinkenlights.nl 以确保其正常工作?完成此操作后,您是否尝试过从文件(例如 C:\ Windows \ system32 \ telnet.exe毛巾.blinkenlights.nl<文件名)将输入重定向到该文件?

Have you tried doing C:\Windows\system32\telnet.exe towel.blinkenlights.nl in a command prompt window to make sure that works? Having done that, have you tried redirecting input to it from a file (e.g., C:\Windows\system32\telnet.exe towel.blinkenlights.nl < filename)?

我怀疑Windows的telnet可能没有将stdin用于其输入,以便处理终端仿真.

I suspect that Windows' telnet may not be using stdin for its input, in order to handle terminal emulation.

我刚刚尝试了一个非常简单且可能是幼稚的实验,建议至少在32位XP中是这样.我是这样做的:

I just tried a very simple and possibly naive experiment suggesting that that's the case, at least in XP 32-bit. I did this:

echo GET / | telnet www.google.com 80

...并且它不起作用,而 telnet www.google.com 80 ,然后键入 GET/并按Enter即可.一项类似的实验是使用读取stdin( nslookup 的工具-嘿,这是我必须要处理的)的工具.

...and it didn't work, whereas telnet www.google.com 80 and then typing GET / and pressing enter does. A similar experiment with a tool that does read stdin (nslookup -- hey, it was what I had to hand) worked fine.

这篇关于具有重定向的标准流的C#+ telnet进程立即退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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