Skip to content

Instantly share code, notes, and snippets.

@piccaso
Last active Aug 20, 2021
Embed
What would you like to do?
ssh.net Example - Keybased Authentication, File Upload, Shell Commands
/*
get SSH.NET (BSD License: http://sshnet.codeplex.com/license)
with NuGet:
>Install-Package SSH.NET -Version 2013.4.7
or just get the dll from here: http://j.mp/sshNet
*/
using System;
using System.Collections.Generic;
using Renci.SshNet; /* reference needed: Renci.SshNet.dll */
class Program
{
static void Main(string[] args){
// Setup Credentials and Server Information
ConnectionInfo ConnNfo = new ConnectionInfo("hostOrIP",22,"username",
new AuthenticationMethod[]{
// Pasword based Authentication
new PasswordAuthenticationMethod("username","password"),
// Key Based Authentication (using keys in OpenSSH Format)
new PrivateKeyAuthenticationMethod("username",new PrivateKeyFile[]{
new PrivateKeyFile(@"..\openssh.key","passphrase")
}),
}
);
// Execute a (SHELL) Command - prepare upload directory
using (var sshclient = new SshClient(ConnNfo)){
sshclient.Connect();
using(var cmd = sshclient.CreateCommand("mkdir -p /tmp/uploadtest && chmod +rw /tmp/uploadtest")){
cmd.Execute();
Console.WriteLine("Command>" + cmd.CommandText);
Console.WriteLine("Return Value = {0}", cmd.ExitStatus);
}
sshclient.Disconnect();
}
// Upload A File
using (var sftp = new SftpClient(ConnNfo)){
string uploadfn = "Renci.SshNet.dll";
sftp.Connect();
sftp.ChangeDirectory("/tmp/uploadtest");
using (var uplfileStream = System.IO.File.OpenRead(uploadfn)){
sftp.UploadFile(uplfileStream, uploadfn, true);
}
sftp.Disconnect();
}
// Execute (SHELL) Commands
using (var sshclient = new SshClient(ConnNfo)){
sshclient.Connect();
// quick way to use ist, but not best practice - SshCommand is not Disposed, ExitStatus not checked...
Console.WriteLine(sshclient.CreateCommand("cd /tmp && ls -lah").Execute());
Console.WriteLine(sshclient.CreateCommand("pwd").Execute());
Console.WriteLine(sshclient.CreateCommand("cd /tmp/uploadtest && ls -lah").Execute());
sshclient.Disconnect();
}
Console.ReadKey();
}
}
@netzulo

This comment has been minimized.

Copy link

@netzulo netzulo commented Sep 26, 2015

wow, really ty for this bro!! im going to use from a webpage ^^

@giang-hai-nguyen

This comment has been minimized.

Copy link

@giang-hai-nguyen giang-hai-nguyen commented Sep 6, 2016

is there anyway to download files pro?

@bartread

This comment has been minimized.

Copy link

@bartread bartread commented Nov 9, 2016

This is great - thanks for posting. Been digging around in the API looking for a way to verify the host key fingerprint but not found anything so far. Is there a way to do this with SSH.NET? Thanks!

@GailBowen

This comment has been minimized.

Copy link

@GailBowen GailBowen commented Jun 16, 2017

Thank you so much - I owe you one.

@mickeyperlstein

This comment has been minimized.

Copy link

@mickeyperlstein mickeyperlstein commented Aug 2, 2017

tnx. great example! KUDOS

@donnyv

This comment has been minimized.

Copy link

@donnyv donnyv commented Aug 12, 2017

You rock!! Thanks!

@appssiddu

This comment has been minimized.

Copy link

@appssiddu appssiddu commented Nov 9, 2017

Many thanks for developing this. I am able to connect in one go.

@vitaliiTolmachov

This comment has been minimized.

Copy link

@vitaliiTolmachov vitaliiTolmachov commented Jan 10, 2018

It rocks! thnx for examples

@Sykimnice

This comment has been minimized.

Copy link

@Sykimnice Sykimnice commented Jul 8, 2018

Thank you. It's great example. :)

@niwsa

This comment has been minimized.

Copy link

@niwsa niwsa commented Aug 1, 2018

Any idea how to provide a option like StrictHostKeyChecking=no to the ssl client?

@hgonzalezv

This comment has been minimized.

Copy link

@hgonzalezv hgonzalezv commented Apr 8, 2019

Very helpfull. Thx!

@SaddamBInSyed

This comment has been minimized.

Copy link

@SaddamBInSyed SaddamBInSyed commented Apr 21, 2019

Any idea how to provide a option like StrictHostKeyChecking=no to the ssl client?

please help

@MWinnGitHub

This comment has been minimized.

Copy link

@MWinnGitHub MWinnGitHub commented Apr 8, 2021

Hi All,
I've been struggling with just running a basic command on a unix host.
Example what I have come up with
`Public Function CreateConnectionInfo() as ConnectionInfo
Dim connNfo as New PasswordConnectionInfo("host", "user", "pw")

Using sshClient = New sshClient(connNfo)
sshClient.Connect()

Using cmd = sshClient.CreateCommand("ls -ltr")
    cmd.Execute()
    MsgBox("command>" & cmd.CommandText)
    MsgBox("Return Value = {0}, cmd.ExitStatus)
End Using

End Using
`
When I check the values being send I never see the PW and the host refuses the connection. I had to have my pw reset about 20 times today..lol

My wish is to use public/private keys instead of a user/pw. Which i have used/created with putty and use it everyday.

Any suggestions would be greatly appreciated.

WannaBe Programmer
MW

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment