ProcessStartinfo处理telnet命令的问题

zhlzl 2006-10-06 03:15:04
我的代码

Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;

p.Start();
p.StandardInput.WriteLine("telnet xxx.xxx.xxx.xxx");

p.StandardInput.WriteLine("exit");

string strRst = p.StandardOutput.ReadToEnd();

为什么在strRst里拿不到telnet命令之后的输出, 或者有什么方法可以周转拿到, 应该在telnet的那台服务器上,我还需要敲入一些命令, 而这些命令也是会有返回值的, 我也希望能拿到这些值, 谢谢
...全文
459 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
股神 2006-10-10
  • 打赏
  • 举报
回复
liangxf0022 2006-10-10
  • 打赏
  • 举报
回复
你直接用telnet,不用cmd.exe看看可以不?
zhlzl 2006-10-09
  • 打赏
  • 举报
回复
或者大家有什么比较好的telnet client library for C#, 推荐一下, 谢谢
zhlzl 2006-10-09
  • 打赏
  • 举报
回复
p.StandardInput.WriteLine("telnet xxx.xxx.xxx.xxx");
关键是这句命令之后, 我还需要去输入user name和password, 但拿不到那个窗口了
lovvver 2006-10-09
  • 打赏
  • 举报
回复
学习,顶~
yx10 2006-10-09
  • 打赏
  • 举报
回复
不知道你问题出在哪里,你可以用我这个例子

例如这个可以:
public static bool TestRun(string cmd,ref string str)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;



try
{
p.Start();
p.StandardInput.Write(cmd);
p.StandardInput.Write(p.StandardInput.NewLine);
p.StandardInput.WriteLine("exit");

str = p.StandardOutput.ReadToEnd();
p.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
liangxf0022 2006-10-09
  • 打赏
  • 举报
回复
上面都没有看清LZ的问题,他已经设定了重定向。我觉得你直接使用Telnet.exe可能还行
xingyaohua 2006-10-09
  • 打赏
  • 举报
回复
zhlzl 2006-10-09
  • 打赏
  • 举报
回复
up
wuhuiITren 2006-10-09
  • 打赏
  • 举报
回复

  • 打赏
  • 举报
回复
用DOS的输出重定向(或曰管道重定向)功能应该可以,把TELNET向屏幕的输出指到向一个文本文件的输出,之后去读那个文本文件.
重定向一般是用>,<,>>等符号,有兴趣可以自己查阅一下.
股神 2006-10-06
  • 打赏
  • 举报
回复
up了.............
Knight94 2006-10-06
  • 打赏
  • 举报
回复
可以参看这个例子
http://www.codeproject.com/cs/miscctrl/shellcontrol.asp

主要参看捕获console输出部分。

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧