Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Renci.SshNet;
- namespace ConsoleApp2
- {
- class Program
- {
- static void Main(string[] args)
- {
- try
- {
- // 接続先のホスト名またはIPアドレス
- var hostNameOrIpAddr = "xxx.xxx.x.x";
- // 接続先のポート番号
- var portNo = 22;
- // ログインユーザー名
- var userName = "pi";
- // ログインパスワード
- var passWord = "xxxxxxxx";
- // コネクション情報
- ConnectionInfo info = new ConnectionInfo(hostNameOrIpAddr, portNo, userName,
- new AuthenticationMethod[] {
- new PasswordAuthenticationMethod(userName, passWord)
- /* PrivateKeyAuthenticationMethod("キーの場所")を指定することでssh-key認証にも対応しています */
- }
- );
- // クライアント作成
- SshClient ssh = new SshClient(info);
- // 接続開始
- ssh.Connect();
- if (ssh.IsConnected)
- {
- // 接続に成功した(接続状態である)
- Console.WriteLine("[OK] SSH Connection succeeded!!");
- }
- else
- {
- // 接続に失敗した(未接続状態である)
- Console.WriteLine("[NG] SSH Connection failed!!");
- return;
- }
- // 接続終了
- ssh.Disconnect();
- }
- catch (Exception ex)
- {
- // エラー発生時
- Console.WriteLine(ex);
- throw ex;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment