.Net Class to Execute Remotely on Linux Over Ssh

.Net class to execute remotely on Linux over SSH?

There are several options to do SSH in .NET - although you need a library for that since nothing is built-in:

  • http://www.codeproject.com/KB/IP/sharpssh.aspx
  • http://www.rebex.net/ssh-shell.net/ (commercial)
  • https://www.eldos.com/sbb/net-ssh.php (commercial)
  • http://sshnet.codeplex.com/
  • http://granados.sourceforge.net/

Modifying a remote file while connected to a server through SSH using .NET

In general, to modify remote files, use SFTP. In SSH.NET that's what SftpClient is for.


Though as you seem to need to use elevated privileges (su) – an important factor that your question title fails to mention – it's way more difficult. The right solution is to avoid the need for su. See somewhat related:
Allowing automatic command execution as root on Linux using SSH.

Another option would be to try to execute the SFTP server under su. Though that would require modification of SSH.NET code. See related Java question:

Using JSch to SFTP when one must also switch user


If you want to keep your current shell approach with su, you are stuck with simulating shell commands. Note that connecting to SSH server won't make other .NET classes (like the File) magically be able to work with remote files (even if SFTP was possible, let only when it is not, due to the su requirement).

The easiest way to read remote file using shell is using the cat command:

cat /home/user/test.properties

Remote debugging .net core 2.0 console app over ssh

I just tried this and I found that the Find.. button doesn't do anything either.

First you enable SSH connections in your Linux host (in my case, Ubuntu, I had to run sudo ufw allow ssh). Test things out by opening cmd on Windows and doing ssh user@host.

Then, on Visual Studio, in the SSH attach to process window, make sure you hit "refresh" and check the "show processes from all users" box. You should see the "dotnet" process running.

Sample Image

Sample Image

EDIT: you should be prompted for the remote host's password at some point. Here's the dialog shown when I changed the password on the remote host and then attempted to debug.

Sample Image

How to send command to Telnet over SSH connection

Use SSH.NET to forward a localport to the remote Telnet server through the SSH connection.

And then use some C# Telnet library. to connect to the local forwarded port.

You can start with this question:

Connection to MySQL from .NET using SSH.NET Library

Just replace the MySqlConnection with your Telnet client class.

C# send a simple SSH command

I used SSH.Net in a project a while ago and was very happy with it. It also comes with a good documentation with lots of samples on how to use it.

The original package website can be still found here, including the documentation (which currently isn't available on GitHub).

For your case the code would be something like this.

using (var client = new SshClient("hostnameOrIp", "username", "password"))
{
client.Connect();
client.RunCommand("etc/init.d/networking restart");
client.Disconnect();
}


Related Topics



Leave a reply



Submit