我的代码 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的那台服务器上,我还需要敲入一些命令, 而这些命令也是会有返回值的, 我也希望能拿到这些值, 谢谢
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的那台服务器上,我还需要敲入一些命令, 而这些命令也是会有返回值的, 我也希望能拿到这些值, 谢谢
http://www.codeproject.com/cs/miscctrl/shellcontrol.asp主要参看捕获console输出部分。
重定向一般是用>,<,>>等符号,有兴趣可以自己查阅一下.
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;
}
}
关键是这句命令之后, 我还需要去输入user name和password, 但拿不到那个窗口了