📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
| (同じ利用者による、間の5版が非表示) | |||
| 33行目: | 33行目: | ||
*: NuGet | *: NuGet | ||
*: https://www.nuget.org/packages/SSH.NET | *: https://www.nuget.org/packages/SSH.NET | ||
<br> | |||
libSSH / libSSH2ライブラリを使用する場合は、C/C++で記述されているため、マーシャリングして使用する必要がある。<br> | |||
ただし、これらの操作は煩雑であるため、SSH.NETライブラリを使用することを推奨する。<br> | |||
<br><br> | <br><br> | ||
| 68行目: | 71行目: | ||
string secretKeyPath = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".ssh", | string secretKeyPath = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".ssh", | ||
"secret_key.pem"); | "secret_key.pem"); | ||
// | // 公開鍵にパスフレーズが存在する場合 | ||
//var authMethod = new PasswordAuthenticationMethod(" | var authMethod = new PrivateKeyAuthenticationMethod("<リモートPCのユーザ名>", | ||
new PrivateKeyFile(secretKeyPath, "<パスフレーズ>")); | |||
// 公開鍵にパスフレーズが無い場合 | |||
//var authMethod = new PrivateKeyAuthenticationMethod("<リモートPCのユーザ名>", | |||
// new PrivateKeyFile(secretKeyPath, "")); | |||
// パスワード認証の場合 | |||
//var authMethod = new PasswordAuthenticationMethod("<リモートPCのユーザ名>", | |||
// "<リモートPCのユーザ名のパスワード>"); | |||
// 接続情報のインスタンス生成 | // 接続情報のインスタンス生成 | ||
var connectionInfo = new ConnectionInfo(" | var connectionInfo = new ConnectionInfo("<リモートPCのIPアドレス または ホスト名>", | ||
<ポート番号>, | |||
"<リモートPCのユーザ名>", | |||
authMethod); | |||
using (var client = new SftpClient(connectionInfo)) | using (var client = new SftpClient(connectionInfo)) | ||
| 122行目: | 136行目: | ||
<br> | <br> | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
using System; | |||
using System.IO; | |||
using Renci.SshNet; | |||
class SftpDownloadMultipleFiles | |||
{ | { | ||
static void Main(string[] args) | |||
{ | { | ||
client.DownloadFile(file.FullName, fs); | // 公開鍵認証の場合 | ||
// 例としてユーザディレクトリ配下の.sshディレクトリ内のsecret_key.pemを秘密鍵として使用 | |||
string secretKeyPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".ssh", "secret_key.pem"); | |||
var authMethod = new PrivateKeyAuthenticationMethod("username", new PrivateKeyFile(secretKeyPath, "finger_print")); | |||
// 公開鍵にパスフレーズが存在する場合 | |||
var authMethod = new PrivateKeyAuthenticationMethod("<リモートPCのユーザ名>", | |||
new PrivateKeyFile(secretKeyPath, "<パスフレーズ>")); | |||
// 公開鍵にパスフレーズが無い場合 | |||
//var authMethod = new PrivateKeyAuthenticationMethod("<リモートPCのユーザ名>", | |||
// new PrivateKeyFile(secretKeyPath, "")); | |||
// パスワード認証の場合 | |||
//var authMethod = new PasswordAuthenticationMethod("<リモートPCのユーザ名>", | |||
// "<リモートPCのユーザ名のパスワード>") | |||
// 接続情報のインスタンス生成 | |||
var connectionInfo = new ConnectionInfo("<リモートPCのIPアドレス または ホスト名>", | |||
<ポート番号 例: 22>, | |||
"<リモートPCのユーザ名>", | |||
authMethod); | |||
using (var client = new SftpClient(connectionInfo)) | |||
{ | |||
// SSH接続を行う | |||
client.Connect(); | |||
// リモートPCのユーザディレクトリに移動して、 | |||
// 任意のディレクトリ (ここでは、/home/username/testディレクトリ) にある全てのファイルをダウンロードする | |||
var files = client.ListDirectory("/home/username/test").Where(x => x.Name != "." && x.Name != ".."); | |||
foreach (var file in files) | |||
{ | |||
// ファイル群を個別に書き込み専用で開く | |||
using (var fs = System.IO.File.OpenWrite(file.Name)) | |||
{ | |||
// ファイル群を個別にダウンロード | |||
client.DownloadFile(file.FullName, fs); | |||
} | |||
} | |||
// SSH接続を切断 | |||
client.Disconnect(); | |||
} | |||
} | } | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br><br> | <br><br> | ||
| 159行目: | 211行目: | ||
var authMethod = new PrivateKeyAuthenticationMethod("username", new PrivateKeyFile(secretKeyPath, "finger_print")); | var authMethod = new PrivateKeyAuthenticationMethod("username", new PrivateKeyFile(secretKeyPath, "finger_print")); | ||
// | // 公開鍵にパスフレーズが存在する場合 | ||
//var authMethod = new PasswordAuthenticationMethod("<リモートPCのユーザ名>", "<リモートPCのユーザ名のパスワード>") | var authMethod = new PrivateKeyAuthenticationMethod("<リモートPCのユーザ名>", | ||
new PrivateKeyFile(secretKeyPath, "<パスフレーズ>")); | |||
// 公開鍵にパスフレーズが無い場合 | |||
//var authMethod = new PrivateKeyAuthenticationMethod("<リモートPCのユーザ名>", | |||
// new PrivateKeyFile(secretKeyPath, "")); | |||
// パスワード認証の場合 | |||
//var authMethod = new PasswordAuthenticationMethod("<リモートPCのユーザ名>", | |||
// "<リモートPCのユーザ名のパスワード>") | |||
// 接続情報のインスタンス生成 | // 接続情報のインスタンス生成 | ||