lsliupei 2014-12-13 09:30 采纳率: 100%
浏览 3791
已采纳

C# telnet登陆到远端 获取键入命令的返回值

用C#通过telnet登陆到远端交换机上,键入命令后并获取返回值到本地,需要如何操作?
是自己写个telnet类吗?求具体方法

  • 写回答

3条回答 默认 最新

  • 微wx笑 Java领域优质创作者 2014-12-13 10:56
    关注

    参考以下代码:

     // Define the namespaces used by this sample.
    using System;
    using System.Text;
    using System.IO;
    using System.Diagnostics;
    using System.Threading;
    using System.ComponentModel;
    
    namespace ProcessAsyncStreamSamples
    {
        class SortOutputRedirection
        {
            // Define static variables shared by class methods.
            private static StringBuilder sortOutput = null;
            private static int numOutputLines = 0;
    
            public static void SortInputListText()
            {
                // Initialize the process and its StartInfo properties.
                // The sort command is a console application that
                // reads and sorts text input.
    
                Process sortProcess;
                sortProcess = new Process();
                sortProcess.StartInfo.FileName = "Sort.exe";
    
                // Set UseShellExecute to false for redirection.
                sortProcess.StartInfo.UseShellExecute = false;
    
                // Redirect the standard output of the sort command.  
                // This stream is read asynchronously using an event handler.
                sortProcess.StartInfo.RedirectStandardOutput = true;
                sortOutput = new StringBuilder("");
    
                // Set our event handler to asynchronously read the sort output.
                sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
    
                // Redirect standard input as well.  This stream
                // is used synchronously.
                sortProcess.StartInfo.RedirectStandardInput = true;
    
                // Start the process.
                sortProcess.Start();
    
                // Use a stream writer to synchronously write the sort input.
                StreamWriter sortStreamWriter = sortProcess.StandardInput;
    
                // Start the asynchronous read of the sort output stream.
                sortProcess.BeginOutputReadLine();
    
                // Prompt the user for input text lines.  Write each 
                // line to the redirected input stream of the sort command.
                Console.WriteLine("Ready to sort up to 50 lines of text");
    
                String inputText;
                int numInputLines = 0;
                do 
                {
                    Console.WriteLine("Enter a text line (or press the Enter key to stop):");
    
                    inputText = Console.ReadLine();
                    if (!String.IsNullOrEmpty(inputText))
                    {
                        numInputLines ++;
                        sortStreamWriter.WriteLine(inputText);
                    }
                }
                while (!String.IsNullOrEmpty(inputText) && (numInputLines < 50));
                Console.WriteLine("<end of input stream>");
                Console.WriteLine();
    
                // End the input stream to the sort command.
                sortStreamWriter.Close();
    
                // Wait for the sort process to write the sorted text lines.
                sortProcess.WaitForExit();
    
                if (numOutputLines > 0)
                {
                    // Write the formatted and sorted output to the console.
                    Console.WriteLine(" Sort results = {0} sorted text line(s) ", 
                        numOutputLines);
                    Console.WriteLine("----------");
                    Console.WriteLine(sortOutput);
                }
                else 
                {
                    Console.WriteLine(" No input lines were sorted.");
                }
    
                sortProcess.Close();
            }
    
            private static void SortOutputHandler(object sendingProcess, 
                DataReceivedEventArgs outLine)
            {
                // Collect the sort command output.
                if (!String.IsNullOrEmpty(outLine.Data))
                {
                    numOutputLines++;
    
                    // Add the text to the collected output.
                    sortOutput.Append(Environment.NewLine + 
                        "[" + numOutputLines.ToString() + "] - " + outLine.Data);
                }
            }
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿