private void SendLoginPassword_HighPriv(Telnet.Terminal telnetSwitch, DisableGetObjects.Setting_SwitchType whatTypeOfSwitch) { //enable密码 LogInToEvent.WriteDebug(nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "等待要求enable密码"); lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n" + nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "等待要求enable密码\n"; } try { telnetSwitch.WaitForString(whatTypeOfSwitch.PromptForEnablePassword); } catch (Exception e) { LogInToEvent.WriteDebug(nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + e.ToString()); lock (nowUsingItem) { nowUsingItem.FlushResult = "没有提示输入enable密码/enable用户名错误?"; } throw new Exception(); } lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n" + nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "发送enable密码\n"; } if (!telnetSwitch.SendResponse(nowUsingItem.EnablePassword, true)) { lock (nowUsingItem) { nowUsingItem.FlushResult = "错误:发送enable密码失败"; } throw new Exception(); } if (!telnetSwitch.WaitForChangedScreen()) { lock (nowUsingItem) { nowUsingItem.FlushResult = "错误:WaitForChangedScreen返回false"; } throw new Exception(); } }
private void GetIntoTerminalConfigMode(Telnet.Terminal telnetSwitch, DisableGetObjects.Setting_SwitchType whatTypeOfSwitch) { nowUsingItem.LastFlushLog += "\n GetIntoTerminalConfigMode "; nowUsingItem.LastFlushLog += "\n 发送:" + whatTypeOfSwitch.SwitchCommandForConfigMode + "\r\n"; if (!telnetSwitch.SendResponse(whatTypeOfSwitch.SwitchCommandForConfigMode, true)) { lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n" + "进入终端配置模式失败\n"; } throw new Exception(); } for (int i = 0; i < 20; ++i) { telnetSwitch.SendResponse(" ", true); } }
private void SendErrdisableRecoveryCommand(Telnet.Terminal telnetSwitch, DisableGetObjects.Setting_SwitchType whatTypeOfSwitch) { nowUsingItem.LastFlushLog += "\n SendErrdisableRecoveryCommand "; nowUsingItem.LastFlushLog += "\n 发送:" + whatTypeOfSwitch.SwitchCommandForRecoving + "\r\n"; if (!telnetSwitch.SendResponse(whatTypeOfSwitch.SwitchCommandForRecoving, true)) { lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n" + "发送恢复命令失败\n"; } throw new Exception(); } for (int i = 0; i < 20; ++i) { telnetSwitch.SendResponse(" ", true); } }
private void SendEndToSuperModeCommand(Telnet.Terminal telnetSwitch, DisableGetObjects.Setting_SwitchType whatTypeOfSwitch) { lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n SendEndToSuperModeCommand \r\n"; nowUsingItem.LastFlushLog += "\n 发送:" + whatTypeOfSwitch.SwitchCommandForEndConfig + "\r\n"; } if (!telnetSwitch.SendResponse(whatTypeOfSwitch.SwitchCommandForEndConfig, true)) { lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n" + "回到普通命令提示模式失败\n"; } throw new Exception(); } for (int i = 0; i < 20; ++i) { telnetSwitch.SendResponse(" ", true); } }
private void SendLoginUserNameIfNeeded(Telnet.Terminal telnetSwitch, DisableGetObjects.Setting_SwitchType whatTypeOfSwitch) { if (whatTypeOfSwitch.IsThisSwitchNeedsOfUserName) { LogInToEvent.WriteDebug(nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "要求登录用户名"); lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n" + nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "要求登录用户名\n"; } try { telnetSwitch.WaitForString(whatTypeOfSwitch.PromptForUserName); } catch (Exception e) { lock (nowUsingItem) { nowUsingItem.FlushResult = "要求用户名时遇到异常-" + e.ToString(); } LogInToEvent.WriteDebug(nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + e.ToString()); throw new Exception(); } if (!telnetSwitch.SendResponse(nowUsingItem.UserName, true)) { lock (nowUsingItem) { nowUsingItem.FlushResult = "未发送用户名"; } throw new Exception(); } if (!telnetSwitch.WaitForChangedScreen()) { lock (nowUsingItem) { nowUsingItem.FlushResult = "等待用户名结果失败"; } throw new Exception(); } } }
private void SendEnableCommand(Telnet.Terminal telnetSwitch) { if (!telnetSwitch.SendResponse("enable", true)) { lock (nowUsingItem) { nowUsingItem.FlushResult = "发送enable指令失败"; } throw new Exception(); } if (!telnetSwitch.WaitForChangedScreen()) { lock (nowUsingItem) { nowUsingItem.FlushResult = "发送enable指令后没有响应"; } throw new Exception(); } }
private void SendLoginPassword_LowPriv(Telnet.Terminal telnetSwitch, DisableGetObjects.Setting_SwitchType whatTypeOfSwitch) { LogInToEvent.WriteDebug(nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "要求登录密码"); lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n" + nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "要求登录密码\n"; } try { telnetSwitch.WaitForString(whatTypeOfSwitch.PromptForPassword); } catch (Exception e) { LogInToEvent.WriteDebug(nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + e.ToString()); lock (nowUsingItem) { nowUsingItem.FlushResult = "没有要求密码或密码请求字符串不正确"; } throw new Exception(); } LogInToEvent.WriteDebug(nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "发送登录密码"); lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n" + nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "发送登录密码\n"; } if (!telnetSwitch.SendResponse(nowUsingItem.Password, true)) { lock (nowUsingItem) { nowUsingItem.FlushResult = "发送密码失败"; } throw new Exception(); } if (!telnetSwitch.WaitForChangedScreen()) { lock (nowUsingItem) { nowUsingItem.FlushResult = "发送密码后没有响应"; } throw new Exception(); } }
private void SendEnableUserNameIfNeeded(Telnet.Terminal telnetSwitch, DisableGetObjects.Setting_SwitchType whatTypeOfSwitch) { //enable登录过程 if (whatTypeOfSwitch.IsThisSwitchNeedsOfEnableUserName) { LogInToEvent.WriteDebug(nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "等待要求enable用户名登录"); nowUsingItem.LastFlushLog += "\n" + nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "等待要求enable用户名登录\n"; try { telnetSwitch.WaitForString(whatTypeOfSwitch.PromptForEnableUserName); } catch (Exception e) { LogInToEvent.WriteDebug(nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + e.ToString()); lock (nowUsingItem) { nowUsingItem.FlushResult = "没有提示输入enable用户名"; } throw new Exception(); } lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n" + nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "发送enable用户名\n"; } if (!telnetSwitch.SendResponse(nowUsingItem.EnableUsername, true)) { lock (nowUsingItem) { nowUsingItem.FlushResult = "发送enable用户名失败"; } throw new Exception(); } if (!telnetSwitch.WaitForChangedScreen()) { lock (nowUsingItem) { nowUsingItem.FlushResult = "发送enable用户名后没有响应"; } throw new Exception(); } } }
void ICommand.DoCommand() { const string nowProgressing = "正在处理..."; lock (nowUsingItem) { nowUsingItem.LastFlushLog = "开始处理"; nowUsingItem.FlushResult = nowProgressing; } //进行刷新操作 const int DieTime = 10;//second Telnet.Terminal telnetSwitch = new Telnet.Terminal(nowUsingItem.IpAddress, 23, DieTime, 800, 600); ///////////////// LogInToEvent.WriteDebug("试图连接"); lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n试图连接\n"; } if (telnetSwitch.Connect()) { LogInToEvent.WriteDebug("连接成功"); lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n连接成功\n"; } var whatTypeOfSwitch = DisableGetObjects.ApplicationSettings.GetSwitchTypeByName(settings.SwitchTypeItems, nowUsingItem.SwitchTypeNameBelongTo); if (whatTypeOfSwitch != null) { LogInToEvent.WriteDebug("类型:" + whatTypeOfSwitch.ToString()); } else { lock (nowUsingItem) { nowUsingItem.FlushResult = "错误的类型" + nowUsingItem.SwitchTypeNameBelongTo; } goto errorProcress; } telnetSwitch.WaitForChangedScreen(); try { // 发送登录用户名 SendLoginUserNameIfNeeded(telnetSwitch, whatTypeOfSwitch); // 发送登录密码 SendLoginPassword_LowPriv(telnetSwitch, whatTypeOfSwitch); lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n" + nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "取得结果\n"; } // 确认低权限登录密码正确 MakeSureLowPrivIsGood(telnetSwitch, whatTypeOfSwitch); LogInToEvent.WriteDebug(nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "发送enable"); lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n" + nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "发送enable\n"; } // 发送Enable命令 SendEnableCommand(telnetSwitch); // 若需要,发送Enable用户名 SendEnableUserNameIfNeeded(telnetSwitch, whatTypeOfSwitch); // 发送Enable密码 SendLoginPassword_HighPriv(telnetSwitch, whatTypeOfSwitch); //enable 成功? LogInToEvent.WriteDebug(nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "确认enable成功"); lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n" + nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "确认enable成功\n"; } MakeSureEnableSuccess(telnetSwitch, whatTypeOfSwitch); //发送查询状态信息 LogInToEvent.WriteDebug(nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "发送状态查询指令" + whatTypeOfSwitch.CommandForFindStatus); lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n" + nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "发送状态查询指令\n"; } //清除屏幕,因为之前已经有enable态的提示符了。 ClearScreen(telnetSwitch); if (!telnetSwitch.SendResponse(whatTypeOfSwitch.CommandForFindStatus, true)) { lock (nowUsingItem) { nowUsingItem.FlushResult = "发送查询端口指令失败"; } throw new Exception(); } if (!telnetSwitch.WaitForChangedScreen()) { lock (nowUsingItem) { nowUsingItem.FlushResult = "发送查询端口指令后,没有响应"; } throw new Exception(); } lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n" + nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "发送状态查询指令完成\n"; } //System.Threading.Thread.Sleep(1000); lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n" + nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "发送状态查询指令完成+1s\n"; } WaitForAllSwitchStatusTransmited(telnetSwitch, whatTypeOfSwitch); string dataContains = GetAllDataTransmited(telnetSwitch); bool isthisswitchhasdisable; string flushResult; CheckForDisableStringInData(whatTypeOfSwitch, dataContains, out isthisswitchhasdisable, out flushResult); lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n" + nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "检查结束"; } if (isthisswitchhasdisable) { AddsInToDisableList(flushResult); } else { RemoveFromDisabledList(); } } catch { goto errorProcress; } } else { lock (nowUsingItem) { nowUsingItem.FlushResult = "无法连接到该交换机"; } goto errorProcress; } try { DealWithSuccessStuff(nowProgressing, telnetSwitch); } catch { goto errorProcress; } return; errorProcress: //失败 DealWithErrorStuff(telnetSwitch); }
private void WaitForAllSwitchStatusTransmited(Telnet.Terminal telnetSwitch, DisableGetObjects.Setting_SwitchType whatTypeOfSwitch) { //大量交换机显示==more==信息,因此需要等待命令行 //added @ 2012 05 20 by sc for (int counter = 0; counter < 20; ++counter) { if (!telnetSwitch.SendResponse(" ", false)) { lock (nowUsingItem) { nowUsingItem.FlushResult = "发送等待命令行参数的请求失败"; } throw new Exception(); } } ///最大尝试次数 const int MAXIMUM_TRY_COUNT = 20; for (int currectTry = 0; currectTry <= MAXIMUM_TRY_COUNT; ++currectTry) { telnetSwitch.WaitForChangedScreen(); //若是屏幕上没有出现命令提示符 try { if (telnetSwitch.VirtualScreen.FindOnScreen(whatTypeOfSwitch.PromptForCommandAfterEnable, false) != null) { break; } } catch (NullReferenceException) { lock (nowUsingItem) { nowUsingItem.FlushResult = "发送请求命令行参数的请求中,连接断开"; } throw new Exception(); } catch (Exception) { lock (nowUsingItem) { nowUsingItem.FlushResult = "发送请求命令行参数的请求失败"; } throw new Exception(); } if (currectTry == MAXIMUM_TRY_COUNT) { //到达最大重试门限 lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n到达取得数据最大门限\n"; } } else { //发送空格以及两个回车 if (!telnetSwitch.SendResponse(" ", true)) { lock (nowUsingItem) { nowUsingItem.FlushResult = "发送等待命令行参数的请求失败-1"; } throw new Exception(); } } } }