我在寫一個需要先連接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();
}
}
}
}
這已經是我見過的了。 – nuriselcuk