Esempio n. 1
0
        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);
            }
        }
Esempio n. 2
0
        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);
        }
        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);
        }
        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);
            }
        }