This project is read-only.

connection aborted by software on host machine

Mar 13, 2012 at 3:58 PM

question:

I just downloaded this library and have written a quick test driver using some of your samples.  I am trying to execute multiple commands in a connection to a cisco 881W firewall, here is what I am using:

 

using (var client = new SshClient("XX.XXX.XXX.XXX""admin""abc"))
            {
                client.Connect();
                var cmd = client.CreateCommand("show run");
                cmd.Execute();
                Console.WriteLine(cmd.Result);
 
                cmd.Execute("show ver");
                Console.WriteLine(cmd.Result);
                client.Disconnect();
            }

 

This driver connects and executes the first command (show run) without any issues, but when it tries to execute the 2nd command (show ver), an exception occurs:

SocketException:  {"An established connection was aborted by the software in your host machine"}

 

I have added the following line to re-connect:

if (!client.IsConnected) client.Connect();

the connection portion is executed, but on the command (show ver), I get the same exception.

 

any ideas?

 

Mar 13, 2012 at 4:14 PM

I think I 've seen similar issue before and it has to do with router implementation of SSH server.

Some server, for security reasons will not allow you to execute multiple commands on the same connection

so you might want to take a look into shell functionality.

 

Hope it helps,

Thanks,

Oleg

Mar 13, 2012 at 5:06 PM

Thanks for the reply.

We originally were using sharpSSH, connecting to our firewalls and running multiple commands without issues.  We decided to have a look at SSH.net due to the connection timeout functionality, so I don't thinks its security on the firewall.  

It seems odd that after we re-connect and execute that 2nd command, we get that same exception.  I would have guessed the behavior would be:

 - execute 1st command (show ver) = success

 - execute 2nd command (show run) = exception

 - reconnect

 - execute 2nd command (show ver) = success

 - execute 3rd command =  exception

Instead, I am seeing:

- execute 1st command (show ver) = success

 - execute 2nd command (show run) = exception

 - reconnect

 - execute 2nd command (show ver) = exception

 

Mar 13, 2012 at 5:38 PM

Hmm,

it is weird then, why that specific command would not work.

When you was using SharpSSH what code your were using?

Is there a way for me to see this problem my self that you can provide me privately with some test credentials?

We dont have any cisco routers here, I asked already when previous problem with router occurred, so it would be very hard for me to debug.

 

Thanks,

Oleg

Mar 13, 2012 at 7:07 PM
Edited Mar 13, 2012 at 7:07 PM

Oleg,

I stand corrected.  You were right with the security aspect of cisco firewall.  Instead of creating commands, I need to create a shell.  Here is my code (taken from one of your previous posts):

using System;
using System.IO;
using System.Threading;
using Renci.SshNet;
 
namespace ConsoleApplications
{
    public class Program
    {
        private static System.ComponentModel.IContainer components;
 
        private static void Main(string[] args)
        {
            using (var client = new SshClient(host"admin""abc"))
            {
                client.Connect();
 
                using (var stream = client.CreateShellStream("dumb", 80, 24, 800, 600, 1024))
                {
                    var reader = new StreamReader(stream);
                    var writer = new StreamWriter(stream);
                    writer.AutoFlush = true;
 
                    while (stream.Length == 0)
                    {
                        Thread.Sleep(500);
                    }
                    ReadStream(reader);
 
                    WriteStream("show run", writer, stream);
                    ReadStream(reader);
 
                    WriteStream("show ver", writer, stream);
                    ReadStream(reader);
                }
 
                client.Disconnect();
            }
        }
 
        private static void ReadStream(StreamReader reader)
        {
            string line = reader.ReadLine();
            while (line != null)
            {
                Console.WriteLine(line);
                line = reader.ReadLine();
            }
        }
 
        private static void WriteStream(string cmd, StreamWriter writer, ShellStream stream)
        {
            writer.WriteLine(cmd);
            while (stream.Length == 0)
            {
                Thread.Sleep(500);
            }
        }
    }
}

all is well, thanks for you work on this very cool/need .net project

Mar 18, 2012 at 2:28 PM

just to let you know i had the exact same issue as this with a Cisco 2950 switch. Ended up having to use the code above aswell.

 

thanks

Damo

Mar 27, 2012 at 8:17 AM

I know why this "An established connection was aborted by the software in your host machine" happens. Because you do not set the WaitHandle timeout. The default timeout is 30 seconds . You can change it like the below code.

ConnectionInfo connInfo = new PasswordConnectionInfo("host","port","username","pwd");

connInfo.TimeOut = TimeSpan.FromHours(1);

var client = new SshClient(connInfo);

client.Connect();

var cmd = client.CreateCommand("show run");

cmd.Execute();

Console.WriteLine(cmd.Result);

client.Disconnect();