DeviceSFTP.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. 
  2. using IMCS.CCS.body;
  3. using Renci.SshNet;
  4. using System;
  5. using System.IO;
  6. namespace IMCS.CCS.DeviceProtocol
  7. {
  8. public class DeviceSFTP
  9. {
  10. #region 声明一个ScpClient类型的变量
  11. private ScpClient ssh;
  12. private SftpClient sftp;
  13. private SshClient sshClient;
  14. /// <summary>
  15. /// SCP连接状态
  16. /// </summary>
  17. public bool Connected { get { return ssh.IsConnected; } }
  18. #endregion
  19. #region
  20. /// <summary>
  21. /// 构造方法
  22. /// </summary>
  23. /// <param name="ip">IP</param>
  24. /// <param name="port">端口</param>
  25. /// <param name="user">用户名</param>
  26. /// <param name="pwd">密码</param>
  27. public DeviceSFTP(string ip, string port, string user, string pwd)
  28. {
  29. ssh = new ScpClient(ip, Int32.Parse(port), user, pwd);
  30. }
  31. public DeviceSFTP(SFTPDeviceBody body)
  32. {
  33. ssh = new ScpClient(body.Ip, Int32.Parse(body.Port), body.UserName, body.Password);
  34. }
  35. #endregion
  36. #region
  37. /// <summary>
  38. /// 连接SCP
  39. /// </summary>
  40. /// <returns>true成功</returns>
  41. public bool Connect()
  42. {
  43. try
  44. {
  45. if (!Connected)
  46. {
  47. ssh.Connect();
  48. }
  49. return true;
  50. }
  51. catch (Exception ex)
  52. {
  53. throw new Exception(string.Format("连接SFTP失败,原因:{0}", ex.Message));
  54. }
  55. }
  56. #endregion
  57. #region 断开SCP
  58. /// <summary>
  59. /// 断开SCP
  60. /// </summary>
  61. public bool Disconnect()
  62. {
  63. try
  64. {
  65. if (ssh != null && Connected)
  66. {
  67. ssh.Disconnect();
  68. }
  69. return true;
  70. }
  71. catch (Exception ex)
  72. {
  73. throw new Exception(string.Format("断开SFTP失败,原因:{0}", ex.Message));
  74. }
  75. }
  76. #endregion
  77. #region SFTP上传文件
  78. /// <summary>
  79. /// SCP上传文件
  80. /// </summary>
  81. /// <param name="localPath">本地路径</param>
  82. /// <param name="remotePath">远程路径</param>
  83. public int Put(string localPath, string remotePath)
  84. {
  85. try
  86. {
  87. FileInfo file = new FileInfo(localPath);
  88. FileStream fs = new FileStream(localPath, FileMode.Open);
  89. Connect();
  90. ssh.Upload(fs, remotePath);
  91. // ssh.Upload(file, remotePath);
  92. Disconnect();
  93. return 1;
  94. }
  95. catch (Exception ex)
  96. {
  97. throw new Exception(string.Format("SFTP文件上传失败,原因:{0}", ex.Message));
  98. }
  99. }
  100. #endregion
  101. #region SFTP获取文件
  102. /// <summary>
  103. /// SCP获取文件
  104. /// </summary>
  105. /// <param name="remotePath">远程路径</param>
  106. /// <param name="localPath">本地路径</param>
  107. public void Get(string remotePath, DirectoryInfo localPath)
  108. {
  109. try
  110. {
  111. Connect();
  112. ssh.Download(remotePath, localPath);
  113. Disconnect();
  114. }
  115. catch (Exception ex)
  116. {
  117. throw new Exception(string.Format("SFTP文件获取失败,原因:{0}", ex.Message));
  118. }
  119. }
  120. #endregion
  121. public void DeleteFile(SFTPDeviceBody body, string filename)
  122. {
  123. using (SshClient sshClient = new SshClient(body.Ip, Int32.Parse(body.Port), body.UserName, body.Password))
  124. {
  125. //连接
  126. sshClient.Connect();
  127. //执行命令
  128. //rm -rf /var/log/httpd/access
  129. var cmd = sshClient.RunCommand($"rm -f /card/user/sinumerik/data/prog/webApiMESFILE/{filename}");
  130. //ExitStatus == 0 执行成功
  131. //
  132. if (cmd.ExitStatus == 0)
  133. {
  134. Console.WriteLine(cmd.Result);//执行结果
  135. }
  136. else
  137. {
  138. Console.WriteLine(cmd.Error);//错误信息
  139. }
  140. //断开连接
  141. sshClient.Disconnect();
  142. }
  143. }
  144. #region SFTP删除文件
  145. /// <summary>
  146. /// SCP获取文件
  147. /// </summary>
  148. public void test()
  149. {
  150. using (SshClient sshClient = new SshClient("192.168.170.205", Int32.Parse("22"), "manufact", "SUNRISE"))
  151. {
  152. //连接
  153. sshClient.Connect();
  154. //执行命令
  155. var cmd = sshClient.RunCommand("cd /card/user/sinumerik/data/prog/webApiMESFILE && ls");
  156. //ExitStatus == 0 执行成功
  157. //
  158. if (cmd.ExitStatus == 0)
  159. {
  160. Console.WriteLine(cmd.Result);//执行结果
  161. }
  162. else
  163. {
  164. Console.WriteLine(cmd.Error);//错误信息
  165. }
  166. //断开连接
  167. sshClient.Disconnect();
  168. }
  169. }
  170. #endregion
  171. }
  172. }