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

解决方案 »

  1.   

    建议可以参考王咏刚的
    编写自己的IDE 如何在图形界面中实时捕获控制台程序的标准输出
    http://www.contextfree.net/wangyg/b/tech/myide.html

    ShellControl - A console emulation control 
    http://www.codeproject.com/cs/miscctrl/shellcontrol.asp
      

  2.   

    to shrinerain 除了调用telnet,还要调用其它控制台程序。
      

  3.   

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

  4.   

    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
      

  5.   

    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; 
    }
    供樓住參考,著行以後的結果需要自己枚舉判斷的吧.
      

  6.   

    第一个问题无法读到提示符是因为提示符那行还在等待输入,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