protected string WriteLine(SshShell _shell, string _line) { CombinedStream oStream = (CombinedStream)(_shell.GetStream()); string strReturn = ""; for (int ii = 0; ii < _line.Length; ii++) { //oStream.Write(_line[ii].ToString()); _shell.Write(_line[ii].ToString()); System.Threading.Thread.Sleep(200); // sleep for a split second int intRead = oStream.ReadByte(); strReturn += _shell.GetStream(); } return(strReturn); }
public void TestCombinedStream() { DisposableFlag f1 = new DisposableFlag(); DisposableFlag f2 = new DisposableFlag(); Assert.IsFalse(f1.Disposed || f2.Disposed); Stream ms1 = new DisposingStream(new MemoryStream(Encoding.ASCII.GetBytes("Hello"))).WithDisposeOf(f1); Stream ms2 = new DisposingStream(new MemoryStream(Encoding.ASCII.GetBytes("There"))).WithDisposeOf(f2); Assert.AreEqual(ms1.Length, ms2.Length); int size = (int)ms1.Length; byte[] bytes = new byte[size * 2]; using (Stream cs = new CombinedStream(ms1, ms2)) { Assert.IsTrue(cs.CanRead); Assert.IsFalse(f1.Disposed); Assert.AreEqual(size, cs.Read(bytes, 0, size)); Assert.IsFalse(f1.Disposed); //still not disposed util read of 0 bytes Assert.AreEqual(1, cs.Read(bytes, size, 1)); //read 1 more byte Assert.IsTrue(f1.Disposed); //now finish the second one... Assert.IsFalse(f2.Disposed); Assert.AreEqual(size - 1, cs.Read(bytes, size + 1, size - 1)); Assert.IsFalse(f2.Disposed);//still not done Assert.AreEqual(-1, cs.ReadByte()); Assert.IsTrue(f2.Disposed); } Assert.AreEqual("HelloThere", Encoding.ASCII.GetString(bytes)); //both were disposed Assert.IsTrue(f1.Disposed && f2.Disposed); }
protected void Page_Load(object sender, EventArgs e) { string strSSH = ""; string strILO = "10.249.237.148"; if (Request.QueryString["ilo"] != null) { strILO = "10.249.237.144"; } Variables oVariable = new Variables(intEnvironment); int intLogging = 0; byte[] byt; string str; Models oModel = new Models(0, dsn); ModelsProperties oModelsProperties = new ModelsProperties(0, dsn); Servers oServer = new Servers(0, dsn); Settings oSetting = new Settings(0, dsn); OnDemand oOnDemand = new OnDemand(0, dsn); Log oEventLog = new Log(0, dsn); SshShell oSSHshell = new SshShell(strILO, oVariable.SolarisUsername(), oVariable.SolarisPassword()); oSSHshell.RemoveTerminalEmulationCharacters = true; oSSHshell.Connect(); Response.Write("Connected to " + strILO + "...sending commands..." + "<br/>"); CombinedStream oSSHstream = (CombinedStream)(oSSHshell.GetStream()); int intStep = 1; if (Request.QueryString["none"] == null) { byt = new byte[100]; str = "" + strSSH_Carriage; byt = System.Text.ASCIIEncoding.ASCII.GetBytes(str); if (oSSHshell.Connected == true && oSSHshell.ShellOpened == true) { oSSHstream.Write(byt); } } int bt = 0; int intMinutePrevious = 0; bool boolProcessing = false; while (bt != -1 && oSSHshell.Connected == true && oSSHshell.ShellOpened == true) { bt = oSSHstream.ReadByte(); // Strip the processing cursor -\|/-\|/ from the output if (bt == 8) // 8 = backspace { // Check to see if previous characters were a processing character as well char chrSSH = strSSH[strSSH.Length - 1]; int intSymbol = (int)chrSSH; while (IsGarbageChar(intSymbol) == true) { if (intLogging > 1) { Response.Write("The symbol [" + chrSSH.ToString() + "] is a garbage character and must be removed" + "<br/>"); } strSSH = strSSH.Substring(0, strSSH.Length - 1); chrSSH = strSSH[strSSH.Length - 1]; intSymbol = (int)chrSSH; } // Set processing to true to exclude future characters boolProcessing = true; } if (boolProcessing == true && IsGarbageChar(bt) == false) { boolProcessing = false; } if (boolProcessing == false) { strSSH += (char)bt; } string strReadSSH = ""; string strWriteSSH = ""; switch (intStep) { case 1: strReadSSH = "-sc>"; strWriteSSH = "poweron"; break; case 2: strReadSSH = "-sc>"; strWriteSSH = "showpower"; break; case 3: strReadSSH = "-sc>"; break; } if (strReadSSH != "" && strSSH.EndsWith(strReadSSH) == true) { try { Response.Write("SSH output ends with [" + strReadSSH + "] : " + strSSH + "<br/>"); } catch { } if (intStep == 3) { break; } // Execute next command byt = new byte[100]; str = strWriteSSH + strSSH_Carriage; byt = System.Text.ASCIIEncoding.ASCII.GetBytes(str); if (oSSHshell.Connected == true && oSSHshell.ShellOpened == true) { try { Response.Write("Sending command [" + strWriteSSH + "] : " + strSSH + "<br/>"); } catch { } oSSHstream.Write(byt); } intStep++; } else { } } Response.Write(strSSH); oSSHstream.Close(); oSSHshell.Close(); }