asdfgh123123

Untitled

May 17th, 2019
21
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
  18. using Ranorex;
  19. using Ranorex.Core;
  20. using Ranorex.Core.Testing;
  21.  
  22. namespace W4KNew.Libraries
  23. {
  24. /// <summary>
  25. /// Creates a Ranorex user code collection. A collection is used to publish user code methods to the user code library.
  26. /// </summary>
  27. [UserCodeCollection]
  28. public class ControllerShell
  29. {
  30. // You can use the "Insert New User Code Method" functionality from the context menu,
  31. // to add a new method with the attribute [UserCodeMethod].
  32.  
  33.  
  34. /// <summary>
  35. /// This is a placeholder text. Please describe the purpose of the
  36. /// user code method here. The method is published to the user code library
  37. /// within a user code collection.
  38. /// </summary>
  39. [UserCodeMethod]
  40. public static string cmd(string ip, string cmd)
  41. {
  42. return cmd1(ip, cmd);
  43.  
  44. Report.Info("Creating PuTTy proccess for: " + ip);
  45. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo{
  46. RedirectStandardInput = true,
  47. RedirectStandardOutput = true,
  48. RedirectStandardError = true,
  49. UseShellExecute = false
  50. };
  51.  
  52. Report.Info("Constructor");
  53. System.Diagnostics.Process process = new System.Diagnostics.Process();
  54.  
  55.  
  56.  
  57. Report.Info("Login into shell of " + ip);
  58.  
  59. //Delay.SpeedFactor = 1;
  60. //Delay.Seconds(5);
  61.  
  62. string filename = @"C:\Program Files\PuTTY\plink.exe";
  63. string arguments = "root@" + ip + " -sshlog \"C:\\WORK\\SSH\\sshlog.txt\" -load " + "DefaultHost" + " \"" + cmd + "\"";
  64.  
  65. startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  66. startInfo.FileName = filename;
  67.  
  68. Report.Info("Cmd: " + " " + filename + " " + arguments);
  69.  
  70. startInfo.Arguments = arguments;
  71. process.StartInfo = startInfo;
  72. process.Start();
  73.  
  74.  
  75. Report.Info("Logged");
  76.  
  77. //processess[ip] = process;
  78.  
  79. Thread.Sleep(250);
  80.  
  81. Report.Info("Responding: " + process.Responding.ToString());
  82. Report.Info("HasExited: " + process.HasExited.ToString());
  83. //Report.Info("StandardOutput.EndOfStream: " + process.StandardOutput.EndOfStream);
  84. //Report.Info("StandardError.EndOfStream: " + process.StandardError.EndOfStream);
  85.  
  86.  
  87.  
  88. string output = process.StandardOutput.ReadToEnd();
  89. string error = process.StandardError.ReadToEnd();
  90.  
  91.  
  92.  
  93. process.WaitForExit();
  94. process.Dispose();
  95.  
  96.  
  97. //string output = process.StandardOutput.ReadToEnd();
  98.  
  99. Report.Info("Output of cmd: '" + output + "'");
  100. Report.Info("Error output of cmd: '" + error + "'");
  101.  
  102. return output;
  103. }
  104.  
  105. public static string cmd1(string ip, string cmd){
  106. int iTry = 0;
  107.  
  108. string outputText = "";
  109. while(true){
  110. Report.Info("Creating PuTTy proccess for: " + ip);
  111. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo{
  112. RedirectStandardInput = true,
  113. RedirectStandardOutput = true,
  114. RedirectStandardError = true,
  115. UseShellExecute = false,
  116. CreateNoWindow = true
  117. };
  118.  
  119. Report.Info("Constructor");
  120. System.Diagnostics.Process process = new System.Diagnostics.Process();
  121.  
  122.  
  123.  
  124. Report.Info("Login into shell of " + ip);
  125.  
  126. //Delay.SpeedFactor = 1;
  127. //Delay.Seconds(5);
  128.  
  129. string logFileName = "sshlog_" + System.DateTime.Now.ToString("yyyyMMddHHmmssffff") + "_.txt";
  130. string filename = @"C:\Program Files\PuTTY\plink.exe";
  131. string arguments = "root@" + ip + " -sshlog \"C:\\RanorexDownload\\SSHLog\\" + logFileName + "\" -batch -load " + "DefaultHost" + " \"" + cmd + "\"";
  132.  
  133. startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  134. startInfo.FileName = filename;
  135.  
  136. Report.Info("Cmd: " + " " + filename + " " + arguments);
  137.  
  138. startInfo.Arguments = arguments;
  139. process.StartInfo = startInfo;
  140.  
  141. process.EnableRaisingEvents = true;
  142.  
  143. process.Exited += (sender, args) =>
  144. {
  145. Report.Info("process exited with code {0}", process.ExitCode.ToString());
  146. };
  147.  
  148. process.Start();
  149.  
  150. var output = new MemoryStream();
  151. var readOutputThread = new Thread(() =>
  152. {
  153. ReadStream(process.StandardOutput.BaseStream, output);
  154. })
  155. { IsBackground = true };
  156. readOutputThread.Start();
  157.  
  158. var error = new MemoryStream();
  159. var readErrorThread = new Thread(() =>
  160. {
  161. ReadStream(process.StandardError.BaseStream, error);
  162. })
  163. { IsBackground = true };
  164. readErrorThread.Start();
  165.  
  166. process.WaitForExit();
  167.  
  168.  
  169. output.Position = 0;
  170. error.Position = 0;
  171. outputText = new StreamReader(output).ReadToEnd();
  172. var errorText = new StreamReader(error).ReadToEnd();
  173.  
  174. Report.Info("output: " + outputText);
  175.  
  176. if(errorText != ""){
  177. iTry++;
  178.  
  179.  
  180. if(iTry >= 5){
  181. Report.Error("SSH error: " + errorText);
  182. Validate.Fail();
  183. break;
  184. }else{
  185. Report.Warn("SSH error(" + iTry.ToString() + "): " + errorText);
  186. continue;
  187. }
  188. }
  189.  
  190.  
  191. Report.Info(readOutputThread.ThreadState.ToString());
  192. Report.Info(readErrorThread.ThreadState.ToString());
  193. break;
  194. }
  195.  
  196. return outputText;
  197. }
  198.  
  199. public static void ReadStream(Stream src, Stream dst)
  200. {
  201. var buffer = new byte[1024 * 4];
  202. while (true)
  203. {
  204. var task = src.ReadAsync(buffer, 0, buffer.Length);
  205. var count = task.Result;
  206. if (count == 0)
  207. {
  208. Report.Info("reach the end of the stream");
  209. return;
  210. }
  211. if (task.Exception != null)
  212. {
  213. Report.Info(task.Exception.ToString());
  214. return;
  215. }
  216. dst.Write(buffer, 0, count);
  217. }
  218. }
  219.  
  220. }
  221. }
RAW Paste Data