Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3. using Renci.SshNet;
  4. using System;
  5. using UnityEngine.Events;
  6. using System.IO;
  7.  
  8. namespace SSH
  9. {
  10. public class SuccessConnect : UnityEvent<string> { }
  11. public class FailureConnect : UnityEvent<string> { }
  12.  
  13. public class SSH
  14. {
  15. SshClient client = null;
  16.  
  17. #region CONNECT
  18. public SuccessConnect successEvent;
  19. public FailureConnect failureEvent;
  20.  
  21. public bool isConnect { get { return client == null ? false : client.IsConnected; } }
  22.  
  23. public SSH()
  24. {
  25. client = null;
  26. successEvent = new SuccessConnect();
  27. failureEvent = new FailureConnect();
  28.  
  29. }
  30.  
  31. ~SSH()
  32. {
  33. if (client != null) {
  34. DisConnected();
  35. }
  36. }
  37.  
  38. /// <summary>
  39. /// パスワードを使用して接続を行う
  40. /// </summary>
  41. /// <param name="host">ホスト名</param>
  42. /// <param name="port">ポート番号</param>
  43. /// <param name="userName">ユーザ名</param>
  44. /// <param name="passWord">パスワード</param>
  45. public void ConnectedToPassword(string host, int port, string userName, string passWord)
  46. {
  47. if (client != null) {
  48. Debug.LogWarning("[Warn] Connected Server!! \n -- Auto DisConnection");
  49. DisConnected();
  50. }
  51. try {
  52. // 接続ポイントの作成
  53. ConnectionInfo connection = new PasswordConnectionInfo(host, port, userName, System.Text.Encoding.UTF8.GetBytes(passWord));
  54. ConnectedClient(connection);
  55. } catch (Exception e) {
  56. failureEvent.Invoke ("[NG] " + e.Message);
  57. }
  58. }
  59.  
  60. private void Connection_AuthenticationBanner(object sender, Renci.SshNet.Common.AuthenticationBannerEventArgs e)
  61. {
  62. Debug.Log(sender);
  63. }
  64.  
  65. /// <summary>
  66. /// Keyファイルを使用して接続を行う
  67. /// </summary>
  68. /// <param name="host">ホスト名</param>
  69. /// <param name="port">ポート番号</param>
  70. /// <param name="userName">ユーザ名</param>
  71. /// <param name="keyPath">接続に使用するKeyファイルのパス</param>
  72. public void ConnectedToKey(string host, int port, string userName, string keyPath)
  73. {
  74. try {
  75. // 接続ポイントの作成
  76. ConnectionInfo connection = new ConnectionInfo(host, port, userName, new AuthenticationMethod[] {
  77. //new PasswordAuthenticationMethod(userName, passWord)
  78. new PrivateKeyAuthenticationMethod(userName, new PrivateKeyFile[]{ new PrivateKeyFile(keyPath) })
  79. /* PrivateKeyAuthenticationMethod("キーの場所")を指定することでssh-key認証にも対応しています */
  80. }
  81. );
  82. ConnectedClient(connection);
  83. } catch (Exception e) {
  84. Debug.Log(e.Message);
  85. }
  86. }
  87.  
  88. /// <summary>
  89. /// SSH接続を行う
  90. /// </summary>
  91. /// <param name="connection"></param>
  92. private void ConnectedClient(ConnectionInfo connection)
  93. {
  94. // コネクションの作成
  95. client = new SshClient(connection);
  96.  
  97. // コネクションの確立
  98. client.Connect();
  99.  
  100. // 接続に成功したかどうか
  101. if (client.IsConnected) {
  102. successEvent.Invoke("[OK] SSH Connection succeeded!!");
  103. }
  104. else {
  105. failureEvent.Invoke("[NG] SSH Connection failed!!");
  106. client = null;
  107. }
  108.  
  109. }
  110.  
  111. /// <summary>
  112. /// 接続を切断する
  113. /// </summary>
  114. public void DisConnected()
  115. {
  116. // nullの場合、接続していない
  117. if (client == null) { return; }
  118.  
  119. client.Disconnect();
  120. client.Dispose();
  121.  
  122. client = null;
  123. }
  124. #endregion
  125.  
  126. #region COMMAND
  127.  
  128. /// <summary>
  129. /// コマンドを実行する
  130. /// </summary>
  131. /// <param name="command"></param>
  132. public void Command(string command)
  133. {
  134. if (client == null) {
  135. Debug.LogError("NOT Connected Server");
  136. return;
  137. }
  138.  
  139. try {
  140. // コマンドの作成
  141. SshCommand cmd = client.CreateCommand(command);
  142.  
  143. // コマンドの実行
  144. cmd.Execute();
  145.  
  146. if (cmd.Result != string.Empty) {
  147. successEvent.Invoke(cmd.Result);
  148. }
  149. if (cmd.ExitStatus != 0 && cmd.Error != string.Empty) {
  150. failureEvent.Invoke(cmd.Error);
  151. }
  152. } catch (Exception e) {
  153. Debug.Log(e.Message);
  154. }
  155. }
  156.  
  157. public IEnumerator IECommand(string command)
  158. {
  159. if (client == null) {
  160. Debug.LogError("NOT Connected Server");
  161. yield break;
  162. }
  163.  
  164. Debug.Log("Start IEnumerator");
  165.  
  166. SshCommand cmd = client.CreateCommand(command);
  167.  
  168. var asyncCmd = cmd.BeginExecute();
  169. var stdoutReader = new StreamReader(cmd.OutputStream);
  170. var stderrReader = new StreamReader(cmd.ExtendedOutputStream);
  171.  
  172. while (!asyncCmd.IsCompleted) {
  173. string line = stdoutReader.ReadToEnd();
  174. if (!string.IsNullOrEmpty(line)) {
  175. successEvent.Invoke(line);
  176. }
  177. line = stderrReader.ReadToEnd();
  178. if (!string.IsNullOrEmpty(line)) {
  179. failureEvent.Invoke(line);
  180. }
  181.  
  182. yield return null;
  183. }
  184. cmd.EndExecute(asyncCmd);
  185.  
  186. stdoutReader.Dispose();
  187. stderrReader.Dispose();
  188.  
  189. Debug.Log("End IEnumerator");
  190. }
  191. #endregion
  192. }
  193.  
  194. public class ScriptOutputLine
  195. {
  196. public string Line { get; private set; }
  197. public bool IsErrorLine { get; private set; }
  198.  
  199. public ScriptOutputLine(string line, bool isErrorLine)
  200. {
  201. Line = line;
  202. IsErrorLine = isErrorLine;
  203. }
  204. }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement