SSH.NETを使用してみることに。マイクロソフトはやはり老舗(ブランド)だな。安心感が違うかも。
参考(多謝)※一通りの(sftpなど)掲載ある。ありがたや!
https://kd2.jp/memo/csharp/ssh_net_1.php
/*
* SSH接続テスト : パスワード認証
*
* DLLはNuGetパッケージマネージャから「Install-Package SSH.NET」で取得する。
* または、http://j.mp/sshNet から直接ダウンロード。
*
* VisualStudio2015で実行。
*
* VisualStudio2013からNuGetすると
* Install-Package : 'SSH.NET' already has a dependency defined for 'SshNet.Security.Cryptography'.
* とエラーがでるので注意。
*
* DLLのランタイムバージョンは.NETv4だが、.NETv4.5(VisualStudio2015デフォルト)でも特に問題なく動作する。
* ランタイムの調整が必要であればソースコードをダウンロードしてビルドを行うこと。
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// ここまではデフォルト
using System.Net;
using Renci.SshNet;
// 以上の2つは追加
namespace ConsoleSSHTest1
{
class Program
{
static void Main(string[] args)
{
// サーバURL (IPアドレスも可"
string url = "hoge.kd2.jp";
//string url = "xxx.xxx.xxx.xxx";
// サーバポート
int port = 22;
// ユーザ
string username = "sshuser";
// パスワード
string password = "sshuser_password";
ConnectionInfo CInfo = new ConnectionInfo(url, port, username,
new AuthenticationMethod[] {
new PasswordAuthenticationMethod(username, password)
}
);
string command = "ls -la";
// サンプルではvarを使用しているが、SshClientクラスでもOK
// using (var sshClient = new SshClient(CInfo))
// Renci.SshNet.SshClient.cs -> Dispose
// ConnectionInfoを使用しなくても接続可能
// using (SshClient sshClient = new SshClient(url, port, username, password))
using (SshClient sshClient = new SshClient(CInfo))
{
sshClient.Connect();
// サンプルではvarを使用しているが、SshCommandでもOK
//using(var cmd = sshClient.CreateCommand(command))
// Renci.SshNet.SshCommand.cs -> Dispose
using (SshCommand cmd = sshClient.CreateCommand(command))
{
cmd.Execute();
Console.WriteLine(username + "@" + url + " > " + cmd.CommandText);
Console.WriteLine(cmd.Result);
Console.WriteLine("ExitStatus = {0}", cmd.ExitStatus);
}
sshClient.Disconnect();
}
}
}
}

