sshnet / sshnet/SSH.NET

Getting "SshConnectionException: Client not connected" on RunCommand despite being inside "if (client.IsConnected)"

Open
#996 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
4.4k
Forks
993
Avg merge
9d 21h
Merged PRs (30d)
1

Description

Here is my code:

private void connectToMacSSH()
{
    using (var client = new SshClient(sshIp,
                                        22,
                                        sshUsername,
                                        new PrivateKeyFile(rsaKeyFilePath,
                                        sshRsaPrivateKeyPassphrase)))
        {

            client.Connect();

            if (client.IsConnected)
            {
                new Thread(() =>
                {
                    client.RunCommand("caffeinate -u -t 2");
                }).Start();
            }
            else
            {
                ...
            }
        }
    }
}

This is how it's called (in a main method):

sshThread = new Thread(() => { connectToMacSSH(); });
sshThread.Start();

This code was working 100% fine just minutes ago. I have no idea what I changed, but for some reason, even though client.IsConnected returns true, running the client.RunCommand line will throw "SshConnectionException: Client not connected", every time.

The command itself executes fine on another SSH client, using all of the credentials and login details on another SSH client works fine too. I have physical access to the host I am trying to SSH into, it is on (logged in and screen on), connected on the right static local IP, and I can access it via other SSH clients.

I've rewritten the code about 3 times now with no success - I even tried removing the try catch block (around RunCommand) and the command itself from a new thread and running it synchronously instead - it had no effect.

All help widely appreciated, completely unsure of what to try next...

Someone else had this issue... only answer was a dead link https://stackoverflow.com/questions/25522894/client-not-connected-exception-with-ssh-net

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied connectToMacSSH entry point and the RunCommand call, then inspect the SSH client connection lifecycle around the threaded and synchronous variants. Reproduce the reported exception using the provided credentials and command, and determine whether the behavior is a library defect or an issue-specific lifecycle condition before defining a regression test or documentation update.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.