0

I am using C# with SSH.NET.

I want to issue a PWD command but could not find any documentation or help. I do not know how to use 'SshClient' class.

Update: I also tried experimenting with SshClient class using the below code but it does nothing, neither any error nor any exception.

ConnectionInfo ConnNfo = new ConnectionInfo("FTPHost", 22, "FTPUser",
new AuthenticationMethod[]{

   // Pasword based Authentication
   new PasswordAuthenticationMethod("FTPUser","FTPPass")
   }                
   );

using (var ssh = new SshClient(ConnNfo))
{
    ssh.Connect();                
    if (ssh.IsConnected)
    {                    
         string comm = "pwd";
         using (var cmd = ssh.CreateCommand(comm))
         {
            var returned = cmd.Execute();
            var output = cmd.Result;
            var err = cmd.Error;
            var stat = cmd.ExitStatus;
         }
     }
   ssh.Disconnect();
}

Nothing happens. Neither an error nor an exception. On Visual Studio console, i get the below output.

*SshNet.Logging Verbose: 1 : SendMessage to server 'ChannelRequestMessage': 'SSH_MSG_CHANNEL_REQUEST : #152199'.

SshNet.Logging Verbose: 1 : ReceiveMessage from server: 'ChannelFailureMessage': 'SSH_MSG_CHANNEL_FAILURE : #0'.*

At ssh.RunCommand method call the program goes in some sleep state (or waits for around 1 minute). sshCommand.Result and sshCommand.Error variables are empty.

user1451111
  • 1,735
  • 3
  • 18
  • 30

2 Answers2

1

Here's a quick example - one way to do it.

string host = "myhost";
string user = "root";
string pwd = "#secret#!"; // Don't use hardcoded plain-text passwords if possible - for demonstration only.

using (PasswordAuthenticationMethod auth = new PasswordAuthenticationMethod(user, pwd))
{
    ConnectionInfo connection = new ConnectionInfo(host, user, auth);
    using (var ssh = new SshClient(connection))
    {
        ssh.Connect();
        SshCommand sshCommand = ssh.RunCommand("pwd");
        Console.WriteLine("Command execution result: {0}", sshCommand.Result);
    }
}

Note that if you specify an invalid command (e.g. "pwdxxxx"), you won't get an exception, but an error that will be stored in the SshCommand.Error string.

Note also that this uses SSH PasswordAuthentication, which may not be enabled in your SSH config.

Community
  • 1
  • 1
w128
  • 4,680
  • 7
  • 42
  • 65
  • I tried it but nothing happens. I get the below two messages on Visual Studio Output window: ..............SshNet.Logging Verbose: 1 : SendMessage to server 'ChannelRequestMessage': 'SSH_MSG_CHANNEL_REQUEST : #152823'. SshNet.Logging Verbose: 1 : ReceiveMessage from server: 'ChannelFailureMessage': 'SSH_MSG_CHANNEL_FAILURE : #0'. The thread 0x1618 has exited with code 259 (0x103)................... ..........At ssh.RunCommand method call the program goes in some sleep state (or waits for around 1 minute). sshCommand.Result and sshCommand.Error variables are empty. – user1451111 Dec 11 '15 at 14:24
  • The code works for me. Is this an exception of some type that you are getting or just a message? Can you check your credentials and your host settings? Can you use the same credentials to execute a command using e.g. putty? Is your host configured to allow connections such as these, and does it have a SSH server running? – w128 Dec 11 '15 at 14:33
  • Also, are you running your code in a clean project, or have you tried it in some previously-existing project where you would happen to be using threads? – w128 Dec 11 '15 at 14:39
  • I am running the code provided by you, in a clean project. The server is accessible using WinSCP utility and i can download and upload files using that. Credentials are fine becasue using the same ConnectionInfo object I can create a 'SftpClient' object and use that to download and upload files. The output I shared with you is from the output window of visual studio and is being written by the SSH.NET project itself as i have added the entire source code of it in my solution. – user1451111 Dec 11 '15 at 14:53
  • Can you use putty to successfully execute the same command on the same host? Is password authentication enabled in your SSH config? If not, does the code work if you use [KeyboardInteractiveAuthenticationMethod](http://stackoverflow.com/questions/15686276/unable-to-connect-to-aixunix-box-with-ssh-net-library-error-value-cannot-b)? – w128 Dec 11 '15 at 14:56
0

Try looking into the documentation of SSH.NET:

SSH.NET at CodePlex

Code sample for executing a command (recommended)

Help file.CHM

jan-marten
  • 66
  • 5