Advertisement
asdfgh123123

Untitled

May 17th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.29 KB | None | 0 0
  1. /*
  2. * Created by Ranorex
  3. * User:
  4. * Date: 07-May-19
  5. * Time: 10:49 AM
  6. *
  7. * To change this template use Tools > Options > Coding > Edit standard headers.
  8. */
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Text;
  12. using System.Text.RegularExpressions;
  13. using System.Drawing;
  14. using System.Threading;
  15. using WinForms = System.Windows.Forms;
  16. using System.IO;
  17. using System.Diagnostics;
  18.  
  19. using Ranorex;
  20. using Ranorex.Core;
  21. using Ranorex.Core.Testing;
  22.  
  23. namespace W4KNew.Libraries
  24. {
  25. /// <summary>
  26. /// Creates a Ranorex user code collection. A collection is used to publish user code methods to the user code library.
  27. /// </summary>
  28. [UserCodeCollection]
  29. public class ControllerShell
  30. {
  31. // You can use the "Insert New User Code Method" functionality from the context menu,
  32. // to add a new method with the attribute [UserCodeMethod].
  33.  
  34.  
  35. /// <summary>
  36. /// This is a placeholder text. Please describe the purpose of the
  37. /// user code method here. The method is published to the user code library
  38. /// within a user code collection.
  39. /// </summary>
  40. [UserCodeMethod]
  41. public static string cmd(string ip, string cmd)
  42. {
  43. return cmd1(ip, cmd);
  44.  
  45. Report.Info("Creating PuTTy proccess for: " + ip);
  46. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo{
  47. RedirectStandardInput = true,
  48. RedirectStandardOutput = true,
  49. RedirectStandardError = true,
  50. UseShellExecute = false
  51. };
  52.  
  53. Report.Info("Constructor");
  54. System.Diagnostics.Process process = new System.Diagnostics.Process();
  55.  
  56.  
  57.  
  58. Report.Info("Login into shell of " + ip);
  59.  
  60. //Delay.SpeedFactor = 1;
  61. //Delay.Seconds(5);
  62.  
  63. string filename = @"C:\Program Files\PuTTY\plink.exe";
  64. string arguments = "root@" + ip + " -sshlog \"C:\\WORK\\SSH\\sshlog.txt\" -load " + "DefaultHost" + " \"" + cmd + "\"";
  65.  
  66. startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  67. startInfo.FileName = filename;
  68.  
  69. Report.Info("Cmd: " + " " + filename + " " + arguments);
  70.  
  71. startInfo.Arguments = arguments;
  72. process.StartInfo = startInfo;
  73. process.Start();
  74.  
  75.  
  76. Report.Info("Logged");
  77.  
  78. //processess[ip] = process;
  79.  
  80. Thread.Sleep(250);
  81.  
  82. Report.Info("Responding: " + process.Responding.ToString());
  83. Report.Info("HasExited: " + process.HasExited.ToString());
  84. //Report.Info("StandardOutput.EndOfStream: " + process.StandardOutput.EndOfStream);
  85. //Report.Info("StandardError.EndOfStream: " + process.StandardError.EndOfStream);
  86.  
  87.  
  88.  
  89. string output = process.StandardOutput.ReadToEnd();
  90. string error = process.StandardError.ReadToEnd();
  91.  
  92.  
  93.  
  94. process.WaitForExit();
  95. process.Dispose();
  96.  
  97.  
  98. //string output = process.StandardOutput.ReadToEnd();
  99.  
  100. Report.Info("Output of cmd: '" + output + "'");
  101. Report.Info("Error output of cmd: '" + error + "'");
  102.  
  103. return output;
  104. }
  105.  
  106. public static string cmd1(string ip, string cmd){
  107. int iTry = 0;
  108.  
  109. string outputText = "";
  110. string errorText = "";
  111. string privKeyPath = TestSuite.Current.Parameters["PRIVATE_KEY_PATH"];
  112. while(true){
  113. Report.Info("Creating PuTTy proccess for: " + ip);
  114. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo{
  115. RedirectStandardInput = true,
  116. RedirectStandardOutput = true,
  117. RedirectStandardError = true,
  118. UseShellExecute = false,
  119. CreateNoWindow = true
  120. };
  121.  
  122. Report.Info("Constructor");
  123. System.Diagnostics.Process process = new System.Diagnostics.Process();
  124.  
  125.  
  126.  
  127. Report.Info("Login into shell of " + ip);
  128.  
  129. //Delay.SpeedFactor = 1;
  130. //Delay.Seconds(5);
  131.  
  132. string logFileName = "sshlog_" + System.DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".txt";
  133. string filename = @"C:\Program Files\PuTTY\plink.exe";
  134. string arguments = "root@" + ip + " -sshlog \"C:\\RanorexDownload\\SSHLog\\" + logFileName + "\" -i \"" + privKeyPath + "\" \"" + cmd + "\"";
  135.  
  136. startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  137. startInfo.FileName = filename;
  138.  
  139. Report.Info("Cmd: " + " " + filename + " " + arguments);
  140.  
  141. startInfo.Arguments = arguments;
  142. process.StartInfo = startInfo;
  143.  
  144. process.EnableRaisingEvents = true;
  145.  
  146. process.Exited += (sender, args) =>
  147. {
  148. Report.Info("process exited with code {0}", process.ExitCode.ToString());
  149. };
  150.  
  151. process.Start();
  152.  
  153. var output = new MemoryStream();
  154. var readOutputThread = new Thread(() =>
  155. {
  156. ReadStream(process.StandardOutput.BaseStream, output);
  157. })
  158. { IsBackground = true };
  159. readOutputThread.Start();
  160.  
  161. var error = new MemoryStream();
  162. var readErrorThread = new Thread(() =>
  163. {
  164. ReadStream(process.StandardError.BaseStream, error);
  165. })
  166. { IsBackground = true };
  167. readErrorThread.Start();
  168.  
  169. /*foreach(System.Diagnostics.ProcessThread thread in process.Threads)
  170. if (thread.ThreadState == System.Diagnostics.ThreadState.Wait
  171. && thread.WaitReason == ThreadWaitReason.UserRequest){
  172. process.Kill();
  173. }*/
  174.  
  175. int max = 0;
  176. while(max < 10){
  177. if(process.HasExited){
  178. break;
  179. }
  180.  
  181. //output.Position = 0;
  182. //outputText = new StreamReader(output).ReadToEnd();
  183.  
  184. error.Position = 0;
  185. errorText = new StreamReader(error).ReadToEnd();
  186.  
  187. if(errorText.Contains("WARNING - POTENTIAL SECURITY BREACH")){
  188. process.StandardInput.WriteLine("y");
  189. Report.Info("Output (sending 'y'): " + errorText);
  190. }else{
  191. if(errorText.Length > 0){
  192. Report.Warn("Unknown output: " + errorText);
  193. }
  194. }
  195.  
  196. Thread.Sleep(250);
  197. max++;
  198. }
  199.  
  200.  
  201. process.WaitForExit();
  202.  
  203.  
  204. output.Position = 0;
  205. error.Position = 0;
  206. outputText = new StreamReader(output).ReadToEnd();
  207. errorText = new StreamReader(error).ReadToEnd();
  208.  
  209. if(errorText.Length > 0){
  210. Report.Warn("error: " + errorText);
  211. }
  212.  
  213. Report.Info("output: " + outputText);
  214.  
  215.  
  216. if(errorText != ""){
  217. iTry++;
  218.  
  219.  
  220. if(iTry >= 5){
  221. Report.Error("SSH error: " + errorText);
  222. Validate.Fail();
  223. break;
  224. }else{
  225. Report.Warn("SSH error(" + iTry.ToString() + "): " + errorText);
  226. continue;
  227. }
  228. }
  229.  
  230.  
  231. Report.Info(readOutputThread.ThreadState.ToString());
  232. Report.Info(readErrorThread.ThreadState.ToString());
  233. break;
  234. }
  235.  
  236. return outputText;
  237. }
  238.  
  239. public static void ReadStream(Stream src, Stream dst)
  240. {
  241. var buffer = new byte[1024 * 4];
  242. while (true)
  243. {
  244. var task = src.ReadAsync(buffer, 0, buffer.Length);
  245. var count = task.Result;
  246. if (count == 0)
  247. {
  248. //Report.Info("reach the end of the stream");
  249. return;
  250. }
  251. if (task.Exception != null)
  252. {
  253. Report.Info(task.Exception.ToString());
  254. return;
  255. }
  256. dst.Write(buffer, 0, count);
  257. }
  258. }
  259.  
  260. }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement