SSH to Linux then SSH to Cisco device
I am dealing with a problem. I am trying to code app for pushing commands to network devices (here I am testing it with cisco routers in GNS3). So, what is a problem here. At work we have a linux jump server on which you need to go at first with ssh. Then from that server you can ssh to network device and push commands. So I started coding C# Windows Form App which will do it. I am struggling with this jump ssh. Correct me If I am wrong but tried to use PortForwarding. Now I am trying to do that with only one router. Code is here:
/*connect to jump*/
using (var client = new SshClient("192.168.29.3", 22, "root", "neviem")) //
{
client.Connect();
if (client.IsConnected)
{
/*loop through all hosts in textbox and execute stream of commands to cisco router */
foreach (string element in words)
{
progressBar1.Value += 1;
Application.DoEvents();
var clean_element = element.Trim();
/*CREATE PORT FORWARDING TO NEXT DEVICE*/
var portForwarded = new ForwardedPortLocal("127.0.0.1", 40000, clean_element, 22);
client.AddForwardedPort(portForwarded);
portForwarded.Start();
try
{
System.Diagnostics.Debug.WriteLine(clean_element);
SshClient cSSH = new SshClient(clean_element, 22, username, password);
cSSH.Connect();
using (ShellStream shell = cSSH.CreateShellStream("Tail", 0, 0, 0, 0, 1024))
{
Console.SetOut(file);
Console.WriteLine("\r ########## RUNNING COMMAND ON HOST *" + clean_element + "* ##########\r\n");
foreach (string execute_command in commands)
{
Console.WriteLine(SendCommand(execute_command, shell));
}
shell.Close();
}
cSSH.Disconnect();
/*output result to output box*/
textBox102.Text += "\r ########## Command successful on host *" + clean_element + "* ##########\r\n";
Console.WriteLine("\r ########## Command successful on host *" + clean_element + "* ##########\r\n");
cSSH.Disconnect();
cSSH.Dispose();
}
catch (Exception ex)
{
//We got disconnected because we just restarted the sshd service
//This is expected
textBox102.Text += string.Format("\r ########## {0} *" + clean_element + "* ##########\r\n", ex.Message);
tofile += "\r ########## Command failed on host *" + clean_element + "* ##########\r\n";
}
}
}
}
@marhyno Did you solve this issue? I think when you are using Local Port Forwarding then you have to use boundHost and boundPort in your new connection?
Use:
SshClient cSSH = new SshClient("127.0.0.1", 40000, username, password);
Instead of:
SshClient cSSH = new SshClient(clean_element, 22, username, password);
Hi kasbst,
well unfortunately I left this solution and used Chilkat SSH library and with big help from their support I managed to get things work. http://www.chilkatforum.com/questions/12072/c-ssh-tunnel-to-cisco-device-through-linux-jump
But I think your answer is right.
SSH.NET has changed a lot. I'm closing the ticket, but if the problem still persists, please feel free to reopen or open a new ticket.