本人想实现通过Telnet登陆到交换机,获取交换机MAC地址表后 返回的功能,也就是在CMD中输入telnet IP 到交换机 disp mac-a 并将显示返回 
   在网上搜了很多类似的解决都不成功,不知道有没有高手做过类似项目

解决方案 »

  1.   

    c# 直接用Socket链接IP,发送“disp mac-a\n”,再接收返回信息就行了
      

  2.   

    什么意思啊,用socket调用远程的CMD?这个可以吗,贴哈代码呢
      

  3.   

    static void Connect(String server, String message) 
    {
      try 
      {
        // Create a TcpClient.
        // Note, for this client to work you need to have a TcpServer 
        // connected to the same address as specified by the server, port
        // combination.
        Int32 port = 13000;
        TcpClient client = new TcpClient(server, port);    // Translate the passed message into ASCII and store it as a Byte array.
        Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);             // Get a client stream for reading and writing.
       //  Stream stream = client.GetStream();    NetworkStream stream = client.GetStream();    // Send the message to the connected TcpServer. 
        stream.Write(data, 0, data.Length);    Console.WriteLine("Sent: {0}", message);             // Receive the TcpServer.response.    // Buffer to store the response bytes.
        data = new Byte[256];    // String to store the response ASCII representation.
        String responseData = String.Empty;    // Read the first batch of the TcpServer response bytes.
        Int32 bytes = stream.Read(data, 0, data.Length);
        responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
        Console.WriteLine("Received: {0}", responseData);             // Close everything.
        stream.Close();         
        client.Close();         
      } 
      catch (ArgumentNullException e) 
      {
        Console.WriteLine("ArgumentNullException: {0}", e);
      } 
      catch (SocketException e) 
      {
        Console.WriteLine("SocketException: {0}", e);
      }  Console.WriteLine("\n Press Enter to continue...");
      Console.Read();
    }
      

  4.   

    所有要发送的内容都是这样处理,包括用户名和密码stream.Write(data, 0, data.Length);至于为什么是乱码,可能的原因就多了,有可能是中文也有可能是别的,你就跟踪到那,看看data里面到底是什么吧
      

  5.   

    这个必然可以,不过telnet也不只是简单的显示文本,它也支持很多格式控制符,这些都不会直接显示出来,如果你的交换机返回的是带有格式控制符等等的数据,那就会显示乱码,所以才要你看看那些乱码到底是什么建议你根据人工登录的顺序接受、发送数据,这样可以不用管返回的是什么,比如一般交换机用telnet登录上对方就会返回一些信息,先接受下来不管,然后发送用户名、密码、和你那个命令等等尽量模拟人工登录的过程
      

  6.   

    我现在是这么做的,第一次返回乱码,第二次就会超时,你实现过的吗?我现在是在本地模拟用Telnet登陆局域网网另一台机器,方便加我QQ详细说明下吗(15002653)
    public static void Connect(String server, String message)
            {
                try
                {
                    // Create a TcpClient.
                    // Note, for this client to work you need to have a TcpServer 
                    // connected to the same address as specified by the server, port
                    // combination.
                    Int32 port = 23;
                    TcpClient client = new TcpClient(server, port);                // Translate the passed message into ASCII and store it as a Byte array.
                    //Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);                // Get a client stream for reading and writing.
                    //  Stream stream = client.GetStream();                NetworkStream stream = client.GetStream();                //Send the message to the connected TcpServer. 
                    //Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
                    //stream.Write(data, 0, data.Length);                //Console.WriteLine("Sent: {0}", message);                //// Receive the TcpServer.response.                //// Buffer to store the response bytes.
                    //data = new Byte[256];                //// String to store the response ASCII representation.
                    //String responseData = String.Empty;                //// Read the first batch of the TcpServer response bytes.
                    //Int32 bytes = stream.Read(data, 0, data.Length);
                    //responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                                  //Console.WriteLine("Received: {0}", responseData);                SendCommand(stream, "y");                SendCommand(stream, "administrator");                SendCommand(stream, "1988912");                SendCommand(stream, "ipconfig");
                    
                    // Close everything.
                    stream.Close();
                    client.Close();
                }
                catch (ArgumentNullException e)
                {
                    Console.WriteLine("ArgumentNullException: {0}", e);
                }
                catch (SocketException e)
                {
                    Console.WriteLine("SocketException: {0}", e);
                }            Console.WriteLine("\n Press Enter to continue...");
                Console.Read();
            }        private static void SendCommand(NetworkStream stream,string message)
            {
                Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);            stream.Write(data, 0, data.Length);            Console.WriteLine("Sent: {0}", message);            // Receive the TcpServer.response.            // Buffer to store the response bytes.
                data = new Byte[256];            // String to store the response ASCII representation.
                String responseData = String.Empty;            // Read the first batch of the TcpServer response bytes.
                Int32 bytes = stream.Read(data, 0, data.Length);
                responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                Console.WriteLine("Received: {0}", responseData);
            }
      

  7.   

    结贴了 3楼的思路还是对的 ,但是向telnet发送返回数据都需要按照Telnet协议才行,在网上找了协议改改才可以了。