2017-02-07 195 views
0

我在寫一個需要先連接SSH的工具,然後用證書打開一個Telnet。手動從膩子我可以連接SSH然後運行命令;通過SSH連接建立Telnet

telnet localhost 21000 

這問我憑據。但是當我在代碼中這樣做時,並沒有發生任何事情。我試過MinimalisticTelnet,但它從我的機器上建立了telnet。我不想要這個。 Telnet連接必須在SSH連接上完成。

這是我的代碼;

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Renci.SshNet; 
using MinimalisticTelnet; 

namespace Ssh_Net 
{ 
    class Program 
    { 

     static void Main(string[] args) 
     { 
      String host = "54.88.81.210"; 
      String username = "root"; 
      String password = "abcd1234"; 
      int port = 22; 

      ForwardedPortDynamic tunnelPort; 

      // Setup SSH with Credentials 
      ConnectionInfo ConnNfo = new ConnectionInfo(host, port, username, 

       new AuthenticationMethod[] { 

         /* Password based authentication */ 
         new PasswordAuthenticationMethod(username, password) 
       } 
      ); 


      // Instantly execute a shell command 

      using (var sshclient = new SshClient(ConnNfo)) 
      { 
       sshclient.Connect(); 
       Console.WriteLine("Connecting"); 

       if (sshclient.IsConnected) 
       { 
        try 
        { 
         Console.WriteLine(sshclient.RunCommand("telnet localhost 21000").Execute()); 

        } 
        catch (Exception e) 
        { 
         Console.Write(e.ToString()); 
        } 
       } 

       Console.ReadLine(); 
       sshclient.Disconnect(); 

      } 


     } 


    } 
} 

回答

0

我有同樣的問題。 這是我做

sshMaster = new SshClient(host, sshUserSSL, sshPwdSSL); 
sshMaster.Connect(); 
ShellStream shell = sshMaster.CreateShellStream("master", 80, 24, 800, 600, 1024); 
shell.WriteLine(" telnet localhost 21000"); 
shell.WriteLine(USERNAME); 
shell.WriteLine(PASSWORD); 

我可以讀telnet提示,用用戶名和密碼的請求,但是當我發送的用戶名和密碼似乎不起作用

+0

這已經是我見過的了。 – nuriselcuk

1

每個shell.writeline後,把

system.threading.sleep(1500) 

給它時間來提示輸入用戶名和密碼。