Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $Source = @"
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Threading;
- using System.IO;
- namespace Zh
- {
- public class ZhExpect
- {
- private Process ps;
- private bool inProgress;
- private Thread readPsOut;
- private Thread readPsErr;
- private volatile string receivedText = "";
- private string log = "";
- private object locker = new object();
- public ZhExpect() {
- ps = new Process();
- ps.StartInfo.UseShellExecute = false;
- ps.StartInfo.RedirectStandardError = true;
- ps.StartInfo.RedirectStandardInput = true;
- ps.StartInfo.RedirectStandardOutput = true;
- }
- public void spawn(String fileName, String arguments) {
- ps.StartInfo.Arguments = arguments;
- ps.StartInfo.FileName = fileName;
- ps.Start();
- readPsOut = new Thread(ReadProcess);
- readPsErr = new Thread(ReadProcess);
- inProgress = true;
- readPsOut.Start(ps.StandardOutput.BaseStream);
- readPsErr.Start(ps.StandardError.BaseStream);
- }
- private void ReadProcess (object bs) {
- Stream bstream = (Stream) bs;
- byte[] buffer = new byte[1000];
- int count;
- System.IAsyncResult readResult = bstream.BeginRead(buffer, 0, buffer.Length, null, null);
- while (inProgress) {
- if (readResult.IsCompleted) {
- count = bstream.EndRead(readResult);
- string currentReceivedText = System.Text.Encoding.UTF8.GetString(buffer, 0, count);
- lock (locker) {
- receivedText = receivedText + currentReceivedText;
- log = log + currentReceivedText;
- }
- Console.Write(currentReceivedText);
- readResult = bstream.BeginRead(buffer, 0, buffer.Length, null, null);
- }
- Thread.Sleep(10);
- }
- }
- public void Interact(string stopString) {
- StreamWriter psStreamWriter = ps.StandardInput;
- System.ConsoleKeyInfo key;
- String str = "";
- while(!ps.HasExited) {
- key = Console.ReadKey(true);
- if (key.Key.Equals(System.ConsoleKey.UpArrow)) psStreamWriter.Write("\x1b[A");
- else if (key.Key.Equals(System.ConsoleKey.DownArrow)) psStreamWriter.Write("\x1b[B");
- else if (key.Key.Equals(System.ConsoleKey.LeftArrow)) psStreamWriter.Write("\x1b[D");
- else if (key.Key.Equals(System.ConsoleKey.RightArrow)) psStreamWriter.Write("\x1b[C");
- else {
- str = str + key.KeyChar;
- psStreamWriter.Write(key.KeyChar);
- }
- if (str.Contains(stopString)) {
- for (int i = 0; i < stopString.Length-1; i++) {
- psStreamWriter.Write("\b");
- }
- break;
- }
- }
- }
- public bool Expect(string ExpectingString, int timeOut, bool cleanBuffer) {
- System.DateTime startTime = System.DateTime.Now;
- do {
- lock(locker) {
- if (receivedText.Contains(ExpectingString)) {
- if (cleanBuffer) receivedText = "";
- return true;
- }
- }
- Thread.Sleep(10);
- } while ((System.DateTime.Now - startTime).Seconds < timeOut);
- return false;
- }
- public void Send(string sendString) {
- ps.StandardInput.Write(sendString);
- }
- public string getLog(bool clear) {
- string returnLog = log;
- if (clear) {
- log = "";
- }
- return returnLog;
- }
- public void Close() {
- inProgress = false;
- readPsErr.Join();
- readPsOut.Join();
- ps.StandardInput.Write("\x03");
- ps.StandardOutput.Close();
- ps.StandardInput.Close();
- ps.StandardError.Close();
- if (!ps.HasExited) {
- ps.Kill();
- }
- }
- }
- }
- "@
- Add-Type -Language CSharp -TypeDefinition $Source
- [Zh.ZhExpect] $plink
- [int32] $expectTimeout = 5
- function Spawn-Plink {
- param (
- [parameter(Mandatory=$true)]
- [alias("h")]
- [string]$remoteHost,
- [alias("p")]
- [string]$port = "",
- [alias("pr")]
- [string]$proto = "ssh",
- [alias("l")]
- [string]$login = "",
- [alias("pw")]
- [string]$password = ""
- )
- $script:plink = New-Object Zh.ZhExpect
- $argumentList = "-$proto"
- if ($port.length -gt 0) {
- $argumentList = $argumentList + " -P $port"
- }
- if ($proto -eq "ssh") {
- if ($login.length -gt 0) { $argumentList = $argumentList + " -l $login" }
- if ($password.length -gt 0) { $argumentList = $argumentList + " -pw $password" }
- }
- $argumentList = $argumentList + " " + $remoteHost
- $script:plink.Spawn("plink", $argumentList)
- }
- function Expect {
- param(
- [parameter(Mandatory=$true, Position=1)]
- [alias("e")]
- [String] $expectString,
- [parameter(Position=2)]
- [alias("a")]
- [Scriptblock] $action,
- [alias("t")]
- [Int32] $timeOut = $script:expectTimeout
- )
- if ($script:plink.Expect($expectString, $timeOut, $true)) {
- $action.Invoke()
- }
- }
- function ext_Expect {
- [String[]]$expStrings = @()
- [ScriptBlock[]]$actions = @()
- for ($x=0; $x -le $Args.Length-1; $x=$x+2) {
- if ($Args[$x].GetType().Name -eq "String") {
- if ($Args[$x+1].GetType().Name -eq "ScriptBlock") {
- $expStrings += $Args[$x]
- $actions += $Args[$x+1]
- } else { Write-Error -ErrorAction Stop ( "arg `"" + $Args[$x+1] +
- "`" has type a " + $Args[$x+1].GetType().Name +
- "; must be is not a ScriptBlock!") }
- } else { Write-Error -ErrorAction Stop ("arg `"" + $Args[$x] +
- "`" has type a " + $Args[$x].GetType().Name +
- "; must be a String!") }
- }
- for ($x=0; $x -le $actions.Length-1; $x++) {
- $tmout = $script:expectTimeout
- if ($script:plink.Expect($expStrings[$x], $script:expectTimeout, ($x == $actions.Length-1))) {
- $actions[$x].Invoke()
- }
- $tmout = 0
- }
- }
- function Send {
- param([parameter(Mandatory=$true)]
- [string]$sendString
- )
- $script:plink.Send($sendString)
- }
- function Interact {
- param([parameter(Mandatory=$true)]
- [string]$stopString
- )
- $script:plink.Interact($stopString)
- }
- function Close-Plink {
- $script:plink.Close()
- }
- function Get-Log {
- param(
- [alias("c")]
- [switch]$clear = $false
- )
- if ($clear) {
- return $script:plink.getLog($true)
- } else {
- return $script:plink.getLog($false)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement