Guest User

Untitled

a guest
Jun 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Renci.SshNet;
  7.  
  8. namespace ConsoleApp2
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. try
  15. {
  16. // 接続先のホスト名またはIPアドレス
  17. var hostNameOrIpAddr = "xxx.xxx.x.x";
  18.  
  19. // 接続先のポート番号
  20. var portNo = 22;
  21.  
  22. // ログインユーザー名
  23. var userName = "pi";
  24.  
  25. // ログインパスワード
  26. var passWord = "xxxxxxxx";
  27.  
  28. // コネクション情報
  29. ConnectionInfo info = new ConnectionInfo(hostNameOrIpAddr, portNo, userName,
  30. new AuthenticationMethod[] {
  31. new PasswordAuthenticationMethod(userName, passWord)
  32. /* PrivateKeyAuthenticationMethod("キーの場所")を指定することでssh-key認証にも対応しています */
  33. }
  34. );
  35.  
  36. // クライアント作成
  37. SshClient ssh = new SshClient(info);
  38.  
  39. // 接続開始
  40. ssh.Connect();
  41.  
  42. if (ssh.IsConnected)
  43. {
  44. // 接続に成功した(接続状態である)
  45. Console.WriteLine("[OK] SSH Connection succeeded!!");
  46. }
  47. else
  48. {
  49. // 接続に失敗した(未接続状態である)
  50. Console.WriteLine("[NG] SSH Connection failed!!");
  51. return;
  52. }
  53.  
  54. // 接続終了
  55. ssh.Disconnect();
  56. }
  57. catch (Exception ex)
  58. {
  59. // エラー発生時
  60. Console.WriteLine(ex);
  61. throw ex;
  62. }
  63. }
  64. }
  65. }
Add Comment
Please, Sign In to add comment