Esempio n. 1
0
 private System.Collections.Generic.List <SnmpConfiger> createSnmpConfig(System.Collections.Generic.List <string> ips)
 {
     System.Collections.Generic.List <SnmpConfiger> list = new System.Collections.Generic.List <SnmpConfiger>();
     foreach (string current in ips)
     {
         SnmpConfig snmpConfig;
         if (this.snmpCfg.snmpVer == 0)
         {
             snmpConfig = new SnmpV1Config(current);
             ((SnmpV1Config)snmpConfig).Community = this.snmpCfg.userName;
         }
         else
         {
             if (this.snmpCfg.snmpVer == 1)
             {
                 snmpConfig = new SnmpV2Config(current);
                 ((SnmpV2Config)snmpConfig).Community = this.snmpCfg.userName;
             }
             else
             {
                 snmpConfig = new SnmpV3Config(current);
                 SnmpV3Config snmpV3Config = snmpConfig as SnmpV3Config;
                 snmpV3Config.UserName       = this.snmpCfg.userName;
                 snmpV3Config.Authentication = (Authentication)System.Enum.Parse(typeof(Authentication), this.snmpCfg.authType);
                 snmpV3Config.AuthSecret     = this.snmpCfg.authPSW;
                 snmpV3Config.Privacy        = (Privacy)System.Enum.Parse(typeof(Privacy), this.snmpCfg.privType);
                 snmpV3Config.PrivacySecret  = this.snmpCfg.privPSW;
             }
         }
         snmpConfig.Port    = this.snmpCfg.devPort;
         snmpConfig.Retry   = this.snmpCfg.retry;
         snmpConfig.Timeout = this.snmpCfg.timeout;
         SnmpConfiger item = new SnmpConfiger(snmpConfig);
         list.Add(item);
     }
     return(list);
 }
Esempio n. 2
0
        public SnmpConfig getSnmpConfig(DevSnmpConfig snmpCfg)
        {
            SnmpConfig snmpConfig = null;

            switch (snmpCfg.snmpVer)
            {
            case 0:
                snmpConfig = new SnmpV1Config(snmpCfg.devIP);
                ((SnmpV1Config)snmpConfig).Community = snmpCfg.userName;
                ((SnmpV1Config)snmpConfig).Retry     = snmpCfg.retry;
                ((SnmpV1Config)snmpConfig).Port      = snmpCfg.devPort;
                ((SnmpV1Config)snmpConfig).Timeout   = snmpCfg.timeout;
                break;

            case 1:
                snmpConfig = new SnmpV2Config(snmpCfg.devIP);
                ((SnmpV2Config)snmpConfig).Community = snmpCfg.userName;
                ((SnmpV2Config)snmpConfig).Retry     = snmpCfg.retry;
                ((SnmpV2Config)snmpConfig).Port      = snmpCfg.devPort;
                ((SnmpV2Config)snmpConfig).Timeout   = snmpCfg.timeout;
                break;

            case 3:
                snmpConfig         = new SnmpV3Config(snmpCfg.devIP);
                snmpConfig.Retry   = snmpCfg.retry;
                snmpConfig.Timeout = snmpCfg.timeout;
                snmpConfig.Port    = snmpCfg.devPort;
                ((SnmpV3Config)snmpConfig).Authentication = (Authentication)System.Enum.Parse(typeof(Authentication), snmpCfg.authType);
                ((SnmpV3Config)snmpConfig).AuthSecret     = snmpCfg.authPSW;
                ((SnmpV3Config)snmpConfig).Privacy        = (Privacy)System.Enum.Parse(typeof(Privacy), snmpCfg.privType);
                ((SnmpV3Config)snmpConfig).PrivacySecret  = snmpCfg.privPSW;
                ((SnmpV3Config)snmpConfig).UserName       = snmpCfg.userName;
                break;
            }
            return(snmpConfig);
        }
Esempio n. 3
0
 public SnmpV3Session(SnmpV3Config config) : base(config)
 {
 }
Esempio n. 4
0
        private System.Collections.Generic.Dictionary <string, string> SendPacket(SnmpOperationType pduType, System.Collections.Generic.List <VarBinding> variables)
        {
            if (variables == null || variables.Count < 1)
            {
                throw new System.ArgumentNullException("The variables for the " + pduType + " operation is null or empty.");
            }
            UdpTarget        udpTarget       = null;
            IAgentParameters agentParameters = null;
            Pdu pdu = null;

            System.Collections.Generic.Dictionary <string, string> result3;
            try
            {
                udpTarget = new UdpTarget(System.Net.IPAddress.Parse(this.config.AgentIp), this.config.Port, this.config.Timeout, this.config.Retry);
                if (this.config.Version == SnmpVersionType.Ver3)
                {
                    agentParameters = new SecureAgentParameters();
                    SecureAgentParameters secureAgentParameters = agentParameters as SecureAgentParameters;
                    if (!udpTarget.Discovery(secureAgentParameters))
                    {
                        throw new SnmpException("Discovery failed: The device with ip(" + this.config.AgentIp + ") is unreachable.");
                    }
                    pdu = new ScopedPdu();
                    SnmpV3Config snmpV3Config = this.config as SnmpV3Config;
                    secureAgentParameters.SecurityName.Set(snmpV3Config.UserName);
                    secureAgentParameters.Authentication = (AuthenticationDigests)snmpV3Config.Authentication;
                    secureAgentParameters.AuthenticationSecret.Set(snmpV3Config.AuthSecret);
                    secureAgentParameters.Privacy = (PrivacyProtocols)snmpV3Config.Privacy;
                    secureAgentParameters.PrivacySecret.Set(snmpV3Config.PrivacySecret);
                    secureAgentParameters.Reportable = true;
                }
                else
                {
                    if (this.config.Version == SnmpVersionType.Ver1)
                    {
                        OctetString community = new OctetString(((SnmpV1Config)this.config).Community);
                        agentParameters = new AgentParameters(SnmpVersion.Ver1, community);
                    }
                    else
                    {
                        OctetString community = new OctetString(((SnmpV2Config)this.config).Community);
                        agentParameters = new AgentParameters(SnmpVersion.Ver2, community);
                    }
                    pdu = new Pdu();
                }
                DictionaryUtil dictionaryUtil = new DictionaryUtil();
                foreach (VarBinding current in variables)
                {
                    try
                    {
                        if (current is LeafVarBinding)
                        {
                            if (pduType.Equals(SnmpOperationType.GetTable) || pduType.Equals(SnmpOperationType.Walk))
                            {
                                pdu.Type = PduType.Get;
                            }
                            else
                            {
                                pdu.Type = (PduType)pduType;
                                if (pduType.Equals(SnmpOperationType.GetBulk))
                                {
                                    this.configBulkPdu(pdu, current.MaxRepetition);
                                }
                            }
                            System.Collections.Generic.Dictionary <string, string> result = this.ReceiveResponseWithLeafVB((LeafVarBinding)current, pdu, udpTarget, agentParameters);
                            dictionaryUtil.Add(result);
                        }
                        else
                        {
                            if (agentParameters.Version == SnmpVersion.Ver1)
                            {
                                pdu.Type = PduType.GetNext;
                            }
                            else
                            {
                                pdu.Type = PduType.GetBulk;
                                this.configBulkPdu(pdu, current.MaxRepetition);
                            }
                            System.Collections.Generic.Dictionary <string, string> result2 = this.ReceiveResponseWithTableVB((TableVarBinding)current, pdu, udpTarget, agentParameters);
                            dictionaryUtil.Add(result2);
                        }
                    }
                    catch (System.Exception ex)
                    {
                        if (!ex.Message.Contains("Invalid ASN.1 type encountered 0x00. Unable to continue decoding."))
                        {
                            throw new SnmpException(ex.Message);
                        }
                    }
                }
                result3 = dictionaryUtil.Result;
            }
            catch (System.Exception ex2)
            {
                throw new SnmpException(ex2.Message);
            }
            finally
            {
                if (udpTarget != null)
                {
                    udpTarget.Close();
                }
            }
            return(result3);
        }