Cannot connect to SshClient in C# - Renci.SshNet error

1

I am working in C# and trying to use Renci.SshNet to create a connection so I can run SSH commands on a device on my network. Everything works fine earlier in the program but when I try creating a connection and connecting it errors out and the try-catch spits out this error message

System.Net.Sockets.SocketException (0x80004005): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
at Renci.SshNet.Abstractions.SocketAbstraction.Connect(IPEndPoint remoteEndpoint, TimeSpan connectTimeout)
at Renci.SshNet.Session.SocketConnect(String host, Int32 port)
at Renci.SshNet.Session.Connect() at Renci.SshNet.BaseClient.Connect()
at SAS.JuniperCTPDriver.RemoveBundle(String IPAddr, Int32 bundleNum) in D:\Devices\Juniper - CTP\Driver Code C#\JuniperCTPDriver.cs:line 1028

Update after attempting to fix it more.

The other error message being thrown is:

System.Net.Sockets.SocketException (0x80004005): The requested address is not valid in its context
at Renci.SshNet.Abstractions.SocketAbstraction.Connect(IPEndPoint remoteEndpoint, TimeSpan connectTimeout)
at Renci.SshNet.Session.SocketConnect(String host, Int32 port)
at Renci.SshNet.Session.Connect()
at Renci.SshNet.BaseClient.Connect()
at SAS.JuniperCTPDriver.tuneJuniper(String destIP, Int32 destBundle, String destName, String Device, String srcIP, Int32 srcBundle, String srcName, String srcInfo, String srcCircIDs, String destCircIDs, String newBundles, Int32 srcOldBndl, Int32 destOldBndl) in D:\Devices\Juniper - CTP\Driver Code C#\JuniperCTPDriver.cs:line 1074

If anyone has any insight into this problem it would be greatly appreciated.

string srcIP = "FD52:A651:105:40::503";
string destIP = "an IPv6 address like the first";
int srcBundle = 1;
int destBundle = 3;
int rSrcCircID = -1;
string srcName = "se-0/0";
string destName = "se-0/1";
//make sure that any already set up connections are removed
if (rSrcCircID <= -1) {
    bool res = RemoveBundle(srcIP, srcBundle);
    if (!res) {
        Console.WriteLine("was not able to remove old connection");
    }                
}


//create the bundles if they do not exist yet
if (newSrcBndl == "y")
{
    CreateBundle(srcIP, srcBundle, srcName);
}

if (newDestBndl == "y")
{
    CreateBundle(destIP, destBundle, destName);
}


using (var destClient = new SshClient(destIP, "usrname", "pw"))
{

    destClient.Connect(); //THIS IS LINE 1074

    //set bundle direction for the destination
    result = ExecuteSSHCommand(destClient, "cmd bndl " + destBundle + " cfg flags simplex_dst");
    if (result == "false")
    {
        LogMsg(3, "-- destination direction flag set failed.");
        return "failed3";
    }

.
.
.

    destClient.Disconnect();
}

For the first error is happening at exactly the client.Connect(); statement.

private static bool RemoveBundle(string IPAddr, int bundleNum) {
    try {
        using (var client = new SshClient(IPAddr, "ctp_cmd", "ctp_cmd")) {
            string result;
            Console.WriteLine("in using section for RemoveBundle");
            client.Connect(); //ERROR IS HERE
            Console.WriteLine("after connect to SSH client");

            //make sure that any already set up connections are removed
            result = ExecuteSSHCommand(client, "cmd bndl " + bundleNum + " disable");
            Console.WriteLine("after first ExecuteSSHCommand");
            result = ExecuteSSHCommand(client, "cmd bndl " + bundleNum + " delete");
            if (result == "false") {
                LogMsg(3, "-- source bundle deletion failed.");
                return false;
            }

            client.Disconnect();
        }

    } catch (Exception ex) {
        Console.WriteLine("RemoveBundle function error: \n" + ex);
        return false;
    }

    return true;
} //end function RemoveBundle

The other function that all SSH commands are sent to is as follows

private static string ExecuteSSHCommand(SshClient s, string scmd) {
    try {
        var command = string.Format(scmd);
        var cmd = s.CreateCommand(command);
        var result = cmd.Execute();

        return result;
    } catch {
        return "false";
    }
}

I have searched all over for someone with the same problem but either none of the answers were helpful, or they weren't really the same problem. Please help if you have the expertise to do so.

c#
ssh
.net-4.0
ipv6
asked on Stack Overflow Jan 10, 2020 by Josiah • edited Jan 31, 2020 by Josiah

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0