在cmd中发送多个TELNET命令

时间:2012-05-14 16:48:02

标签: c# cmd telnet

class Program
{
    public static void ExecuteCommand(string Command)
    {
        ProcessStartInfo ProcessInfo;
        Process Process;

        ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + Command);
        ProcessInfo.CreateNoWindow = false;
        ProcessInfo.UseShellExecute = false;
        Process = Process.Start(ProcessInfo);
        Process.WaitForExit();

        Process.Close();


    }



    static void Main()
    {

        ExecuteCommand("telnet 169.254.0.1");


    }
}

它运行cmd并通过telnet连接到特定设备。接下来,一旦连接,它会提示我要发送的登录名和密码,以及之后的几个特定命令。我该怎么办?

非常感谢!

1 个答案:

答案 0 :(得分:3)

只需写入StandardInput流即可。

Process process = Process.Start("cmd");

if (process != null)
{
   process.StandardInput.WriteLine("dir");
   process.StandardInput.WriteLine("ping");
   process.StandardInput.WriteLine("something");
}