private bool ConnectSSH(string User, string Pwd, string Host) { string strOutput = ""; Regex re = new Regex(@"\n([^\n].*)\s*\n*$", RegexOptions.IgnoreCase); bool bResult = ConMain.open(User, Pwd, Host, 0, "ssh2", "", "", ""); if (bResult) { strOutput = ConMain.WaitForRegEx(unknownPrompt); strOutput = ConMain.ShowScreen(); //objCon.WaitForRegEx(unknownPrompt); Match m = re.Match(strOutput); if (m.Success) { Prompt = m.Groups[1].Value; Prompt = Prompt.Replace(@"\", "\\\\"); Prompt = Prompt.Replace(@"[", "\\["); Prompt = Prompt.Replace(@"]", "\\]"); Prompt = Prompt.Replace(@"(", "\\("); Prompt = Prompt.Replace(@")", "\\)"); Prompt = Prompt.Replace(@"{", "\\{"); Prompt = Prompt.Replace(@"}", "\\}"); Prompt = Prompt.Replace(@"$", "\\$"); // Prompt = "\n+" + Prompt; Prompt = @"[\r\n]*" + Prompt + @"\s*[\r\n]?$"; ConMain.Prompt = Prompt; bResult = true; } else { bResult = false; } ConMain.DeleteScreen(); } return(bResult); }
public ConsoleClass() { ConMain = new TelnetSSHLogin(); //ConMain.PrepareTerminalParameter("Username", "Pwd***", "192.168.192.165", 0, ConnectionMethod.SSH2, "", LogType.Default, ""); ConMain.PrepareTerminalParameter("Username", "Pwd***", "172.25.156.2", 0, ConnectionMethod.Telnet, "", LogType.Binary, ""); if (!ConMain.StartConnection()) { return; } string strOutput = ""; strOutput = ConMain.WaitFor(new string[] { "Username:"******"Login:"******"Password:"******"Press any key to continue" }, false, 0); if ((strOutput == null) || (strOutput.Length == 0)) { return; } if (strOutput.ToLower().Contains("press any key to continue")) { ConMain.print(" "); strOutput = ConMain.WaitFor(new string[] { "username:"******"login:"******"password:"******"username:"******"login:"******"Username"); strOutput = ConMain.WaitForString("Password:"******"Pwd***"); } else if (strOutput.ToLower().Contains("password:"******"Pwd1***"); } else { Console.WriteLine("Error: Found no Loginprompt."); return; } if ((strOutput == null) || (strOutput.Length == 0)) { Console.WriteLine("Error: Found no Prompt after Login."); } Console.WriteLine(ConMain.ShowScreen()); strOutput = ConMain.cmd("terminal length 0"); strOutput = ConMain.cmd("terminal length 1000"); strOutput = ConMain.cmd("terminal width 0"); strOutput = ConMain.cmd("show version"); if (strOutput.Length > 0) { Console.Write(strOutput); } strOutput = ConMain.cmd("show run"); if (strOutput.Length > 0) { Console.Write(strOutput); } ConMain.print("exit"); ConMain.close(); /* * strOutput = ConMain.WaitForString(">"); * Console.WriteLine(ConMain.ShowScreen()); * ConMain.print("exit"); * strOutput = ConMain.WaitForString(":"); * ConMain.print("y"); * strOutput = ConMain.WaitForString(":"); * ConMain.print("n"); * * Console.WriteLine(ConMain.ShowScreen()); */ return; strOutput = ConMain.cmd("ls -alh /"); if (strOutput.Length > 0) { Console.Write(strOutput); } strOutput = ConMain.cmd("ls -a /"); if (strOutput.Length > 0) { Console.Write(strOutput); } strOutput = ConMain.cmd("ls -alh /"); if (strOutput.Length > 0) { Console.Write(strOutput); } ConMain.print("exit"); }