Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Created by Ranorex
- * User:
- * Date: 07-May-19
- * Time: 10:49 AM
- *
- * To change this template use Tools > Options > Coding > Edit standard headers.
- */
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Drawing;
- using System.Threading;
- using WinForms = System.Windows.Forms;
- using System.IO;
- using System.Diagnostics;
- using Ranorex;
- using Ranorex.Core;
- using Ranorex.Core.Testing;
- namespace W4KNew.Libraries
- {
- /// <summary>
- /// Creates a Ranorex user code collection. A collection is used to publish user code methods to the user code library.
- /// </summary>
- [UserCodeCollection]
- public class ControllerShell
- {
- // You can use the "Insert New User Code Method" functionality from the context menu,
- // to add a new method with the attribute [UserCodeMethod].
- /// <summary>
- /// This is a placeholder text. Please describe the purpose of the
- /// user code method here. The method is published to the user code library
- /// within a user code collection.
- /// </summary>
- [UserCodeMethod]
- public static string cmd(string ip, string cmd)
- {
- return cmd1(ip, cmd);
- Report.Info("Creating PuTTy proccess for: " + ip);
- System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo{
- RedirectStandardInput = true,
- RedirectStandardOutput = true,
- RedirectStandardError = true,
- UseShellExecute = false
- };
- Report.Info("Constructor");
- System.Diagnostics.Process process = new System.Diagnostics.Process();
- Report.Info("Login into shell of " + ip);
- //Delay.SpeedFactor = 1;
- //Delay.Seconds(5);
- string filename = @"C:\Program Files\PuTTY\plink.exe";
- string arguments = "root@" + ip + " -sshlog \"C:\\WORK\\SSH\\sshlog.txt\" -load " + "DefaultHost" + " \"" + cmd + "\"";
- startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
- startInfo.FileName = filename;
- Report.Info("Cmd: " + " " + filename + " " + arguments);
- startInfo.Arguments = arguments;
- process.StartInfo = startInfo;
- process.Start();
- Report.Info("Logged");
- //processess[ip] = process;
- Thread.Sleep(250);
- Report.Info("Responding: " + process.Responding.ToString());
- Report.Info("HasExited: " + process.HasExited.ToString());
- //Report.Info("StandardOutput.EndOfStream: " + process.StandardOutput.EndOfStream);
- //Report.Info("StandardError.EndOfStream: " + process.StandardError.EndOfStream);
- string output = process.StandardOutput.ReadToEnd();
- string error = process.StandardError.ReadToEnd();
- process.WaitForExit();
- process.Dispose();
- //string output = process.StandardOutput.ReadToEnd();
- Report.Info("Output of cmd: '" + output + "'");
- Report.Info("Error output of cmd: '" + error + "'");
- return output;
- }
- public static string cmd1(string ip, string cmd){
- int iTry = 0;
- string outputText = "";
- string errorText = "";
- string privKeyPath = TestSuite.Current.Parameters["PRIVATE_KEY_PATH"];
- while(true){
- Report.Info("Creating PuTTy proccess for: " + ip);
- System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo{
- RedirectStandardInput = true,
- RedirectStandardOutput = true,
- RedirectStandardError = true,
- UseShellExecute = false,
- CreateNoWindow = true
- };
- Report.Info("Constructor");
- System.Diagnostics.Process process = new System.Diagnostics.Process();
- Report.Info("Login into shell of " + ip);
- //Delay.SpeedFactor = 1;
- //Delay.Seconds(5);
- string logFileName = "sshlog_" + System.DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".txt";
- string filename = @"C:\Program Files\PuTTY\plink.exe";
- string arguments = "root@" + ip + " -sshlog \"C:\\RanorexDownload\\SSHLog\\" + logFileName + "\" -i \"" + privKeyPath + "\" \"" + cmd + "\"";
- startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
- startInfo.FileName = filename;
- Report.Info("Cmd: " + " " + filename + " " + arguments);
- startInfo.Arguments = arguments;
- process.StartInfo = startInfo;
- process.EnableRaisingEvents = true;
- process.Exited += (sender, args) =>
- {
- Report.Info("process exited with code {0}", process.ExitCode.ToString());
- };
- process.Start();
- var output = new MemoryStream();
- var readOutputThread = new Thread(() =>
- {
- ReadStream(process.StandardOutput.BaseStream, output);
- })
- { IsBackground = true };
- readOutputThread.Start();
- var error = new MemoryStream();
- var readErrorThread = new Thread(() =>
- {
- ReadStream(process.StandardError.BaseStream, error);
- })
- { IsBackground = true };
- readErrorThread.Start();
- /*foreach(System.Diagnostics.ProcessThread thread in process.Threads)
- if (thread.ThreadState == System.Diagnostics.ThreadState.Wait
- && thread.WaitReason == ThreadWaitReason.UserRequest){
- process.Kill();
- }*/
- int max = 0;
- while(max < 10){
- if(process.HasExited){
- break;
- }
- //output.Position = 0;
- //outputText = new StreamReader(output).ReadToEnd();
- error.Position = 0;
- errorText = new StreamReader(error).ReadToEnd();
- if(errorText.Contains("WARNING - POTENTIAL SECURITY BREACH")){
- process.StandardInput.WriteLine("y");
- Report.Info("Output (sending 'y'): " + errorText);
- }else{
- if(errorText.Length > 0){
- Report.Warn("Unknown output: " + errorText);
- }
- }
- Thread.Sleep(250);
- max++;
- }
- process.WaitForExit();
- output.Position = 0;
- error.Position = 0;
- outputText = new StreamReader(output).ReadToEnd();
- errorText = new StreamReader(error).ReadToEnd();
- if(errorText.Length > 0){
- Report.Warn("error: " + errorText);
- }
- Report.Info("output: " + outputText);
- if(errorText != ""){
- iTry++;
- if(iTry >= 5){
- Report.Error("SSH error: " + errorText);
- Validate.Fail();
- break;
- }else{
- Report.Warn("SSH error(" + iTry.ToString() + "): " + errorText);
- continue;
- }
- }
- Report.Info(readOutputThread.ThreadState.ToString());
- Report.Info(readErrorThread.ThreadState.ToString());
- break;
- }
- return outputText;
- }
- public static void ReadStream(Stream src, Stream dst)
- {
- var buffer = new byte[1024 * 4];
- while (true)
- {
- var task = src.ReadAsync(buffer, 0, buffer.Length);
- var count = task.Result;
- if (count == 0)
- {
- //Report.Info("reach the end of the stream");
- return;
- }
- if (task.Exception != null)
- {
- Report.Info(task.Exception.ToString());
- return;
- }
- dst.Write(buffer, 0, count);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement