Guest User

Untitled

a guest
Jun 19th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. // 接続先のポート番号
  2. var portNo = 22;
  3.  
  4. // ログインユーザー名
  5. var userName = "pi";
  6.  
  7. // ログインパスワード
  8. var passWord = "xxxxxxxx";
  9.  
  10. // コネクション情報
  11. ConnectionInfo info = new ConnectionInfo(hostNameOrIpAddr, portNo, userName,
  12. new AuthenticationMethod[] {
  13. new PasswordAuthenticationMethod(userName, passWord)
  14. /* PrivateKeyAuthenticationMethod("キーの場所")を指定することでssh-key認証にも対応しています */
  15. }
  16. );
  17.  
  18. // クライアント作成
  19. SshClient ssh = new SshClient(info);
  20.  
  21. // 接続開始
  22. ssh.Connect();
  23.  
  24. if (ssh.IsConnected)
  25. {
  26. // 接続に成功した(接続状態である)
  27. Console.WriteLine("[OK] SSH Connection succeeded!!");
  28. }
  29. else
  30. {
  31. // 接続に失敗した(未接続状態である)
  32. Console.WriteLine("[NG] SSH Connection failed!!");
  33. return;
  34. }
  35.  
  36. // 接続終了
  37. ssh.Disconnect();
  38. }
  39. catch (Exception ex)
  40. {
  41. // エラー発生時
  42. Console.WriteLine(ex);
  43. throw ex;
  44. }
  45. }
  46. }
Add Comment
Please, Sign In to add comment