DeviceSFTP.cs 5.3 KB

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