コード例 #1
0
        private void Change_Click(object sender, EventArgs e)
        {
            //правильно ли указан community, то бишь может ли она получить доступ по SNMP
            UdpTarget target = new UdpTarget((IPAddress) new IpAddress(ComboBoxIpAddressCisco.Text), 161, 500, 0);
            Pdu       pdu    = new Pdu(PduType.Get);

            pdu.VbList.Add(".1.3.6.1.2.1.1.6.0");
            AgentParameters aparam = new AgentParameters(SnmpVersion.Ver2, new OctetString(TextBoxCommunity.Text));
            SnmpV2Packet    response;

            try
            {
                response = target.Request(pdu, aparam) as SnmpV2Packet;
            }

            catch (Exception ex)
            {
                MessageBox.Show("Скорее всего введен неверный Community");
                MessageBox.Show(ex.Message);
                target.Close();
                this.ComboBoxIpAddressCisco.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;
                return;
            }



            var oldValue = StaticValues.CiscoList.FirstOrDefault(item => item.IP.Contains(ComboBoxIpAddressCisco.Text));

            if (oldValue == null)
            {
                return;
            }

            var newValue = new IPCom(ComboBoxIpAddressCisco.Text, TextBoxCommunity.Text);


            var serviceContext = new WaterGateServiceContext();
            var list           = StaticValues.CiscoList.ToList();

            list.Remove(oldValue);
            list.Add(newValue);

            Save.Enabled   = false;
            Delete.Enabled = false;
            Change.Enabled = false;
            Cursor         = Cursors.AppStarting;



            foreach (var item in StaticValues.JDSUCiscoArray)
            {
                if (item.CiscoIPCom.IP == ComboBoxIpAddressCisco.Text)
                {
                    item.CiscoIPCom.Com = TextBoxCommunity.Text;
                }
            }

            //   var serviceContext = new WaterGateServiceContext();
            serviceContext.UpdatePorts(StaticValues.JDSUCiscoArray, (error) =>
            {
                if (error != null)
                {
                    Invoke(new Action(() =>
                                      MessageBox.Show(
                                          "Произошла ошибка при соединении с сервером, проверьте наличие соединения.",
                                          "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error)));
                }
            });



            serviceContext.UpdateCiscoRouters(list, (error) =>
            {
                if (error != null)
                {
                    MessageBox.Show("Произошла ошибка при соединении с сервером, проверьте наличие соединения.",
                                    "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                Invoke(new Action(() =>
                {
                    MessageBox.Show("Запись успешно изменена.",
                                    "Готово", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    //удаляем старый ip и community
                    StaticValues.CiscoList.Remove(oldValue);

                    //и добавляем в наш массив новые данные и все должно работать =)
                    StaticValues.CiscoList.Add(newValue);


                    ComboBoxIpAddressCisco.SelectedIndex      = -1;
                    this.ComboBoxIpAddressCisco.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;
                    TextBoxCommunity.Clear();
                }));

                Invoke(new Action(() =>
                {
                    Save.Enabled   = true;
                    Delete.Enabled = true;
                    Change.Enabled = true;
                    Cursor         = Cursors.Arrow;
                }));
            });
        }
コード例 #2
0
        private void Save_Click(object sender, EventArgs e)
        {
            IPAddress address;

            if (!IPAddress.TryParse(ComboBoxIpAddressCisco.Text, out address))
            {
                MessageBox.Show("Введите корректно IP адрес");
                return;
            }

            //Проверяем существует ли такой ip адрес
            if (ComboBoxIpAddressCisco.FindString(ComboBoxIpAddressCisco.Text) != -1)
            {
                MessageBox.Show("Cisco с таким IPадресом уже добавлена");
                return;
            }

            //идет ли пинг
            try
            {
                Ping      pingSender = new Ping();
                PingReply reply      = pingSender.Send(ComboBoxIpAddressCisco.Text);
            }

            catch
            {
                MessageBox.Show("IP адрес не доступен");
                return;
            }

            //правильно ли указан community, то бишь может ли она получить доступ по SNMP
            UdpTarget target = new UdpTarget((IPAddress) new IpAddress(ComboBoxIpAddressCisco.Text), 161, 500, 0);
            Pdu       pdu    = new Pdu(PduType.Get);

            pdu.VbList.Add(".1.3.6.1.2.1.1.6.0");
            AgentParameters aparam = new AgentParameters(SnmpVersion.Ver2, new OctetString(TextBoxCommunity.Text));
            SnmpV2Packet    response;

            try
            {
                response = target.Request(pdu, aparam) as SnmpV2Packet;
            }

            catch (Exception ex)
            {
                MessageBox.Show("connection failed");
                MessageBox.Show(ex.Message);
                target.Close();
                return;
            }


            var value = new IPCom(ComboBoxIpAddressCisco.Text, TextBoxCommunity.Text);

            var serviceContext = new WaterGateServiceContext();
            var list           = StaticValues.CiscoList.ToList();

            list.Add(value);

            Save.Enabled   = false;
            Delete.Enabled = false;
            Change.Enabled = false;
            Cursor         = Cursors.AppStarting;
            serviceContext.UpdateCiscoRouters(list, (error) =>
            {
                if (error != null)
                {
                    MessageBox.Show("Произошла ошибка при соединении с сервером, проверьте наличие соединения.",
                                    "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    Invoke(new Action(() =>
                    {
                        MessageBox.Show("Запись успешно добавлена.",
                                        "Готово", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        //добавляем в наш массив
                        StaticValues.CiscoList.Add(value);

                        //добавляем в Combobox
                        this.ComboBoxIpAddressCisco.Items.Add(ComboBoxIpAddressCisco.Text);

                        TextBoxCommunity.Clear();
                        ComboBoxIpAddressCisco.ResetText();
                    }));
                }


                Invoke(new Action(() =>
                {
                    Save.Enabled   = true;
                    Delete.Enabled = true;
                    Change.Enabled = true;
                    Cursor         = Cursors.Arrow;
                }));
            });
        }
コード例 #3
0
        private void Delete_Click(object sender, EventArgs e)
        {
            if (StaticValues.JDSUCiscoArray.Any(exampl => exampl.CiscoIPCom.IP == ComboBoxIpAddressCisco.Text))
            {
                // MessageBox.Show(StaticValues.JDSUCiscoArray.First(exampl => exampl.CiscoIPCom.IP == ComboBoxIpAddressCisco.Text).JDSUPort.ToString());
                MessageBox.Show("элемент коммутационного оборудования используется в конфигурации. Привязан к порту:",
                                StaticValues.JDSUCiscoArray.First(exampl => exampl.CiscoIPCom.IP == ComboBoxIpAddressCisco.Text)
                                .JDSUPort.ToString());
                return;
            }
            else
            {
                var value = StaticValues.CiscoList.FirstOrDefault(item => item.IP.Contains(ComboBoxIpAddressCisco.Text));
                if (value == null)
                {
                    return;
                }

                var serviceContext = new WaterGateServiceContext();
                var list           = StaticValues.CiscoList.ToList();

                list.Remove(value);

                Save.Enabled   = false;
                Delete.Enabled = false;
                Change.Enabled = false;
                Cursor         = Cursors.AppStarting;
                serviceContext.UpdateCiscoRouters(list, (error) =>
                {
                    if (error != null)
                    {
                        MessageBox.Show("Произошла ошибка при соединении с сервером, проверьте наличие соединения.",
                                        "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    Invoke(new Action(() =>
                    {
                        MessageBox.Show("Запись успешно удалена.",
                                        "Готово", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        //удаляем старый ip и community из combobox'а
                        StaticValues.CiscoList.Remove(value);

                        //удаляем из Combobox
                        this.ComboBoxIpAddressCisco.Items.Remove(ComboBoxIpAddressCisco.Text);


                        TextBoxCommunity.Clear();
                        ComboBoxIpAddressCisco.SelectedIndex      = -1;
                        this.ComboBoxIpAddressCisco.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;
                    }));


                    Invoke(new Action(() =>
                    {
                        Save.Enabled   = true;
                        Delete.Enabled = true;
                        Change.Enabled = true;
                        Cursor         = Cursors.Arrow;
                    }));
                });
            }
        }