如何在C#中调用执行执行命令行程序(Telnet.exe)并和程序交互 ?也许这是在公司写的最后一个程序了!

Jarod 2005-10-26 03:12:57
使用程序来代替人工输入。

看了一些例子
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
执行exit后才能得到返回值,但这样程序就退出了。无法继续输入命令了!
-----------------------------------------------------------------------------
目的:登陆远程服务器,并运行远程服务器上的程序,根据程序执行的结果判断继续执行其他命令!

谢谢!

电信行业是java的天下……
...全文
1444 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
www8s88com 2006-03-02
  • 打赏
  • 举报
回复
学习
kagad 2006-01-09
  • 打赏
  • 举报
回复
mark
LGame 2005-11-07
  • 打赏
  • 举报
回复
学习
Jarod 2005-11-07
  • 打赏
  • 举报
回复
第一个问题无法读到提示符是因为提示符那行还在等待输入,proc.StandardOutput.ReadLine()
会处于堵塞状态。
应该用proc.StandardOutput.ReadBlock( buffer, 0, 1);(逐个字符读取)
当判断输出了提示符出发一个事件,在执行下一命令。

第二个问题调用Telnet,这个还没解决不过考虑用socket

非常感谢各位网友的帮助!
也感谢Yippee的热心帮助!
Yippee'blog
《Process.StandardOutput 属性》
http://www.shengfang.org/blog/p/ProcessStandardOutput.php
《VS.NET PROCESS 输入 输出 模拟 CMD》
http://www.shengfang.org/blog/p/vsnetprocesscmdinout.php
s5689412 2005-11-01
  • 打赏
  • 举报
回复
没有进展是什么意思?
那两篇文章没有帮上lz的忙?
dragonfly001 2005-11-01
  • 打赏
  • 举报
回复
private static string CmdPing(string strIp)

{

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;

string pingrst;

p.Start();
// string strRst = p.StandardOutput.ReadToEnd();

p.StandardInput.WriteLine("ping -n 1 "+strIp);

p.StandardInput.WriteLine("exit");

string strRst = p.StandardOutput.ReadToEnd();

if(strRst.IndexOf("(0% loss)")!=-1)

pingrst = "SUCCES";

else if( strRst.IndexOf("Destination host unreachable.")!=-1)

pingrst = "FAIL";

else if(strRst.IndexOf("Request timed out.")!=-1)

pingrst = "TIME OUT";

else if(strRst.IndexOf("Unknown host")!=-1)

pingrst = "UNKNOWN HOST";

else

pingrst = strRst;

p.Close();

return pingrst;
}
供樓住參考,著行以後的結果需要自己枚舉判斷的吧.
Jarod 2005-11-01
  • 打赏
  • 举报
回复
s5689412(华君) 感谢您的回复!没有说明白(这个程序弄的我头都大了)

1.判断命令是否执行结束并获得这个命令执行后返回的结果?(连续执行多个命令)
DOTNET程序获得输出不全.
当直接执行控制台程序,在控制台中输入命令,执行完成后为出现 "XXX>"提示符等待继续输入.
由于程序采用多线程执行,本想用 "XXX>"提示符号, 来判断命令是否执行结束.
如果程序执行后,获得该命令执行得到的返内容,在执行下一命令...
但DOTNET程序却无法获得这一符号.而java写的程序却都能得到.
谢谢?

2.如何正确调用Telnet?
string fileName = @"c:\winnt\system32\telnet.exe";
p.StartInfo.Arguments = "192.168.1.5";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();

但p.HasExited 却为 true
JadyWang 2005-10-31
  • 打赏
  • 举报
回复
Process p = new Process();

p.StartInfo.FileName = "cmd.exe ";

p.StartInfo.Arguments = "/C ping 127.0.0.1 ";

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("ping "+strIp);
//
//p.StandardInput.WriteLine("exit");

string strRst = p.StandardOutput.ReadToEnd();

Console.WriteLine(strRst);

p.StartInfo.FileName = "cmd.exe ";

p.StartInfo.Arguments = " /C dir ";

p.Start();

strRst = p.StandardOutput.ReadToEnd();

Console.WriteLine(strRst);

p.Close();
dragonfly001 2005-10-31
  • 打赏
  • 举报
回复
帮你顶一下!
Jarod 2005-10-31
  • 打赏
  • 举报
回复
没有进展!
asuan 2005-10-31
  • 打赏
  • 举报
回复
mark
Jarod 2005-10-26
  • 打赏
  • 举报
回复
to shrinerain 除了调用telnet,还要调用其它控制台程序。
shrinerain 2005-10-26
  • 打赏
  • 举报
回复
直接socket编程好了,
xiaomatian 2005-10-26
  • 打赏
  • 举报
回复
帮你顶一下!
NWC 2005-10-26
  • 打赏
  • 举报
回复
up
s5689412 2005-10-26
  • 打赏
  • 举报
回复
建议可以参考王咏刚的
编写自己的IDE

如何在图形界面中实时捕获控制台程序的标准输出
http://www.contextfree.net/wangyg/b/tech/myide.html

ShellControl - A console emulation control
http://www.codeproject.com/cs/miscctrl/shellcontrol.asp
jxufewbt 2005-10-26
  • 打赏
  • 举报
回复
?
想跳槽?
ivorstar 2005-10-26
  • 打赏
  • 举报
回复
留名,学习
搂主要转行吗?

111,098

社区成员

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

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

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