public LoginStatus Jump2NextDevice(string nextDevice, string ConnectOptions) { LoginStatus l_LoginResult = LoginStatus.InProgress; DeviceHistoryData DeviceData = new DeviceHistoryData(); DeviceData.bConnected = bConnected; DeviceData.DevicePromptString = DevicePromptString; DeviceData.DevicePrompt = DevicePrompt; DeviceData.DevicePromptConfig = DevicePromptConfig; DeviceData.EnableMode = EnableMode; DeviceData.DeviceType = DeviceType; DeviceData.Device = nextDevice; DeviceHistory.Add(DeviceData); l_LoginResult = doLogin(nextDevice, ConnectOptions); if ((l_LoginResult == LoginStatus.Success) || (l_LoginResult == LoginStatus.UserMode)) { //Login success DeviceCounter++; } else { //Restore Status from Last Device bConnected = DeviceData.bConnected; DevicePromptString = DeviceData.DevicePromptString; DevicePrompt = DeviceData.DevicePrompt; DevicePromptConfig = DeviceData.DevicePromptConfig; EnableMode = DeviceData.EnableMode; DeviceType = DeviceData.DeviceType; //Device = DeviceData.Device; DeviceHistory.RemoveAt(DeviceHistory.Count - 1); } return(l_LoginResult); }
public void doLogout() { string l_DevType = DeviceType; string l_DevPrompt = DevicePrompt; string strOutput = ""; string match = ""; if (DeviceCounter > 0) { if (bConfigMode) { //@strLines = $shell->cmd(String => "end", Timeout => $Timeout, Prompt => $DevicePrompt); ExitConfigMode(); } if (l_DevType.Equals("HP")) { l_DevPrompt = l_DevPrompt.Replace('#', '>'); strOutput = shell.cmd("exit", l_DevPrompt); } DeviceCounter--; // Restore Status from Last Device DeviceHistoryData DeviceData = DeviceHistory[DeviceCounter]; bConnected = DeviceData.bConnected; DevicePromptString = DeviceData.DevicePromptString; DevicePrompt = DeviceData.DevicePrompt; DevicePromptConfig = DeviceData.DevicePromptConfig; EnableMode = DeviceData.EnableMode; DeviceType = DeviceData.DeviceType; //Device = DeviceData.Device; DeviceHistory.RemoveAt(DeviceCounter); if (l_DevType.Equals("HP")) { match = ""; string[] LogoutPrompt = { "Do you want to log out [y/n]", "[y/n]" }; //@strLines = $shell->cmd(String => "exit", Timeout => $Timeout, Prompt => 'Do you want to log out [y/n]'); shell.print("exit"); match = shell.WaitFor(LogoutPrompt, false, Timeout); // if ( (match == null) || (match.Length == 0) ) {} if (match.ToLower().Contains("do you want to log out [y/n]")) { match = ""; shell.print("y"); match = shell.WaitForRegEx(DevicePromptString + @"|Do you want to save current configuration \[y\/n\]|\[y\/n\]"); if ((match != null) && (match.ToLower().Contains("/[y/n]"))) { strOutput = shell.cmd("n", DevicePrompt); } strOutput = shell.cmd(" ", DevicePrompt); } } else if (l_DevType.Equals("H3C")) { strOutput = shell.cmd("quit", DevicePrompt); } else { strOutput = shell.cmd("exit", DevicePrompt); } if (_Debug > 0) { Console.WriteLine("Last Device-Promts are restored: " + DevicePrompt); } } else { if (l_DevType.Equals("H3C")) { if (bConfigMode) { ExitConfigMode(); } shell.print("quit"); } else { shell.print("end"); //sleep 1; shell.print("logout"); if (shell.Connected) { shell.print("exit"); } } if (shell.Connected) { shell.close(); } if (shell != null) { shell = null; } if (_Debug > 0) { Console.WriteLine("Telnet-Session is closed."); } } }