private void DealWithErrorStuff(Telnet.Terminal telnetSwitch) { string infomationToWriteInfo = nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + nowUsingItem.FlushResult; if (telnetSwitch.VirtualScreen != null) { infomationToWriteInfo += telnetSwitch.VirtualScreen.Hardcopy().Trim(); } else { infomationToWriteInfo += "=====NULL====="; } LogInToEvent.WriteInfo(infomationToWriteInfo); LogInToEvent.WriteDebug(nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "失败,日志:\n" + telnetSwitch.GetHistory); RemoveFromDisableListForErrorOcour(); AddsToErrorList(); lock (nowUsingItem) { nowUsingItem.LastFlushLog = telnetSwitch.GetHistory; } nowUsingItem.LastFlushTime = DateTime.Now; if (telnetSwitch.IsOpenConnection()) { if (telnetSwitch.SendLogout() == false) { lock (nowUsingItem) { nowUsingItem.FlushResult = "请求注销失败"; } } telnetSwitch.Close(); } }
private void DealWithErrorStuff(Telnet.Terminal telnetSwitch) { string infomationToWriteInfo = nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + nowUsingItem.FlushResult; if (telnetSwitch.VirtualScreen != null) { infomationToWriteInfo += telnetSwitch.VirtualScreen.Hardcopy().Trim(); } else { infomationToWriteInfo += "=====NULL====="; } LogInToEvent.WriteInfo(infomationToWriteInfo); LogInToEvent.WriteDebug(nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "失败,日志:\n" + telnetSwitch.GetHistory); lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n===========================\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"; nowUsingItem.LastFlushLog += infomationToWriteInfo; nowUsingItem.LastFlushLog += telnetSwitch.GetHistory; nowUsingItem.LastFlushLog += "\r\n错误返回"; nowUsingItem.FlushResult = "在恢复过程中出现错误"; } nowUsingItem.LastFlushTime = DateTime.Now; if (telnetSwitch.IsOpenConnection()) { if (telnetSwitch.SendLogout() == false) { lock (nowUsingItem) { nowUsingItem.FlushResult = "请求注销失败"; } } telnetSwitch.Close(); } lock (nowUsingItem) { nowUsingItem.FlushResult = "在试图恢复时发生错误"; } }
private void DealWithSuccessStuff(string nowProgressing, Telnet.Terminal telnetSwitch) { nowUsingItem.LastFlushTime = DateTime.Now; LogInToEvent.WriteDebug(nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "成功,日志:\n" + telnetSwitch.GetHistory); lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n" + nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "成功\n"; } //成功 RemoveFromErrorList(); if (nowProgressing == nowUsingItem.FlushResult) { lock (nowUsingItem) { nowUsingItem.FlushResult = ""; } } lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n\n SESSIONLOG:\n\n" + telnetSwitch.GetHistory; } if (telnetSwitch.IsOpenConnection()) { if (telnetSwitch.SendLogout() == false) { lock (nowUsingItem) { nowUsingItem.FlushResult = "请求注销失败"; } throw new Exception(); } telnetSwitch.Close(); } }
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; } if (!whatTypeOfSwitch.IsSwitchSupportDisableRecovering) { lock (nowUsingItem) { nowUsingItem.FlushResult = "交换机不支持Disable恢复!"; } 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); System.Threading.Thread.Sleep(100); // 开始处理errdisable recovery事件 lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n" + nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "开始试图恢复\n"; } try { // 进入终端配置模式 GetIntoTerminalConfigMode(telnetSwitch, whatTypeOfSwitch); telnetSwitch.WaitForChangedScreen(); // 发送终端配置字符串 SendErrdisableRecoveryCommand(telnetSwitch, whatTypeOfSwitch); telnetSwitch.WaitForChangedScreen(); // 退出终端模式 SendEndToSuperModeCommand(telnetSwitch, whatTypeOfSwitch); //telnetSwitch.WaitForChangedScreen(); } catch (Exception e) { if (e.Message == "") { lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n 在处理恢复命令时发生意外"; } throw; } else { lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n" + e.ToString(); } throw; } } } catch { goto errorProcress; } string infomationToWriteInfo = ""; if (telnetSwitch.VirtualScreen != null) { infomationToWriteInfo += telnetSwitch.VirtualScreen.Hardcopy().Trim(); } else { infomationToWriteInfo += "=====NULL====="; } LogInToEvent.WriteInfo(infomationToWriteInfo); LogInToEvent.WriteDebug(nowUsingItem.Name + "/" + nowUsingItem.IpAddress + "-" + "完毕,日志:\n" + telnetSwitch.GetHistory); if (telnetSwitch.IsOpenConnection()) { if (telnetSwitch.SendLogout() == false) { lock (nowUsingItem) { nowUsingItem.FlushResult = "请求注销失败"; } } telnetSwitch.Close(); } else { lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n" + "已经关闭连接,可能失败"; } } lock (nowUsingItem) { nowUsingItem.LastFlushLog += "\n" + "日志:\n" + telnetSwitch.GetHistory + "\n\n屏幕:\n" + infomationToWriteInfo; nowUsingItem.FlushResult = "恢复结束,请单击刷新超链接以确认结果"; } return; errorProcress: DealWithErrorStuff(telnetSwitch); } }