Пример #1
0
        /// <summary>
        /// Reset the class. Initialize all member values to class defaults.
        /// </summary>
        public void Reset()
        {
            _address = new IpAddress(System.Net.IPAddress.Loopback);
            _port    = 161;
            _version = SnmpVersion.Ver3;
            _timeout = 2000;
            _retry   = 1;

            _engineId    = new OctetString();
            _engineBoots = new Integer32();
            _engineTime  = new Integer32();

            _engineTimeStamp = DateTime.MinValue;

            _privacyProtocol        = PrivacyProtocols.None;
            _authenticationProtocol = AuthenticationDigests.None;

            _privacySecret        = new MutableByte();
            _authenticationSecret = new MutableByte();

            _contextEngineId = new OctetString();
            _contextName     = new OctetString();
            _securityName    = new OctetString();

            // max message size is initialized to 64KB by default. It will be
            // to the smaller of the two values after discovery process
            _maxMessageSize = new Integer32(64 * 1024);

            _reportable = true;
        }
 /// <summary>
 /// Based on the supplied privacyProtocol, return instance of the privacy protocol implementation class.
 /// </summary>
 /// <param name="privProtocol">Privacy protocol code. Available protocols are <see cref="PrivacyProtocols.DES"/>,
 /// <see cref="PrivacyProtocols.AES128"/>, <see cref="PrivacyProtocols.AES192"/>, <see cref="PrivacyProtocols.AES256"/> and
 /// <see cref="PrivacyProtocols.TripleDES"/>.</param>
 /// <returns>Privacy protocol implementation class on success. If privacy protocol is <see cref="PrivacyProtocols.None"/>
 /// then null is returned.</returns>
 public static IPrivacyProtocol GetInstance(PrivacyProtocols privProtocol)
 {
     if (privProtocol == PrivacyProtocols.None)
     {
         return(null);
     }
     else if (privProtocol == PrivacyProtocols.DES)
     {
         return(new PrivacyDES());
     }
     else if (privProtocol == PrivacyProtocols.AES128)
     {
         return(new PrivacyAES128());
     }
     else if (privProtocol == PrivacyProtocols.AES192)
     {
         return(new PrivacyAES192());
     }
     else if (privProtocol == PrivacyProtocols.AES256)
     {
         return(new PrivacyAES256());
     }
     else if (privProtocol == PrivacyProtocols.TripleDES)
     {
         return(new Privacy3DES());
     }
     return(null);
 }
        /// <summary>
        /// Copy constructor. Initialize the class with the values of the parameter class values.
        /// </summary>
        /// <param name="second">Parameter class.</param>
        public SecureAgentParameters(SecureAgentParameters second)
            : this()
        {
            _contextEngineId.Set(second.ContextEngineId);
            _contextName.Set(second.ContextName);
            _engineBoots.Value = second.EngineBoots.Value;
            _engineId.Set(second.EngineId);
            _engineTime.Value     = second.EngineTime.Value;
            _engineTimeStamp      = second.EngineTimeStamp();
            _maxMessageSize.Value = second.MaxMessageSize.Value;
            _privacyProtocol      = second.Privacy;
            _privacySecret.Set(second.PrivacySecret);
            _authenticationProtocol = second.Authentication;
            _authenticationSecret.Set(second.AuthenticationSecret);
            _reportable = second.Reportable;
            _securityName.Set(second.SecurityName);
            if (second.AuthenticationKey != null)
            {
                _authenticationKey = (byte[])second.AuthenticationKey.Clone();
            }

            if (second.PrivacyKey != null)
            {
                _privacyKey = (byte[])second.PrivacyKey.Clone();
            }
        }
        /// <summary>
        /// Reset the class. Initialize all member values to class defaults.
        /// </summary>
        public void Reset()
        {
            _engineId    = new OctetString();
            _engineBoots = new Integer32();
            _engineTime  = new Integer32();

            _engineTimeStamp = DateTime.MinValue;

            _privacyProtocol        = PrivacyProtocols.None;
            _authenticationProtocol = AuthenticationDigests.None;

            _privacySecret        = new MutableByte();
            _authenticationSecret = new MutableByte();

            _contextEngineId = new OctetString();
            _contextName     = new OctetString();
            _securityName    = new OctetString();

            // max message size is initialized to 64KB by default. It will be
            // to the smaller of the two values after discovery process
            _maxMessageSize = new Integer32(64 * 1024);

            _reportable = true;

            _privacyKey        = null;
            _authenticationKey = null;
        }
Пример #5
0
        /// <summary>
        /// Based on the supplied privacyProtocol, return instance of the privacy protocol implementation class.
        /// </summary>
        /// <param name="privProtocol">Privacy protocol code. Available protocols are <see cref="PrivacyProtocols.DES"/>,
        /// <see cref="PrivacyProtocols.AES128"/>, <see cref="PrivacyProtocols.AES192"/>, <see cref="PrivacyProtocols.AES256"/> and
        /// <see cref="PrivacyProtocols.TripleDES"/>.</param>
        /// <returns>Privacy protocol implementation class on success. If privacy protocol is <see cref="PrivacyProtocols.None"/>
        /// then null is returned.</returns>
        public static IPrivacyProtocol GetInstance(PrivacyProtocols privProtocol)
        {
            if (privProtocol == PrivacyProtocols.None)
            {
                return(null);
            }
#if !NETCOREAPP11 && !NETSTANDARD15
            else if (privProtocol == PrivacyProtocols.DES)
            {
                return(new PrivacyDES());
            }
            else if (privProtocol == PrivacyProtocols.AES128)
            {
                return(new PrivacyAES128());
            }
            else if (privProtocol == PrivacyProtocols.AES192)
            {
                return(new PrivacyAES192());
            }
            else if (privProtocol == PrivacyProtocols.AES256)
            {
                return(new PrivacyAES256());
            }
#endif
            else if (privProtocol == PrivacyProtocols.TripleDES)
            {
                return(new Privacy3DES());
            }
            return(null);
        }
 /// <summary>
 /// Prepare class for NoAuthNoPriv operations. Set authentication and privacy protocols to none.
 /// </summary>
 /// <param name="securityName">User security name</param>
 public void NoAuthNoPriv(string securityName)
 {
     _securityName.Set(securityName);
     _authenticationProtocol = AuthenticationDigests.None;
     _authenticationSecret.Clear();
     _privacyProtocol = PrivacyProtocols.None;
     _privacySecret.Clear();
 }
 /// <summary>
 /// Prepare class for authNoPriv operations. Set privacy protocol to none
 /// </summary>
 /// <param name="securityName">User security name</param>
 /// <param name="authDigest">Authentication protocol</param>
 /// <param name="authSecret">Authentication secret (password)</param>
 public void AuthNoPriv(string securityName, AuthenticationDigests authDigest, string authSecret)
 {
     _securityName.Set(securityName);
     _authenticationProtocol = authDigest;
     _authenticationSecret.Set(authSecret);
     _privacyProtocol = PrivacyProtocols.None;
     _privacySecret.Clear();
 }
 /// <summary>
 /// Prepare class for AuthPriv operations.
 /// </summary>
 /// <param name="securityName">User security name</param>
 /// <param name="authDigest">Authentication protocol</param>
 /// <param name="authSecret">Authentication secret (password)</param>
 /// <param name="privProtocol">Privacy protocol</param>
 /// <param name="privSecret">Privacy secret (encryption password)</param>
 public void AuthPriv(string securityName, AuthenticationDigests authDigest, string authSecret, PrivacyProtocols privProtocol, string privSecret)
 {
     _securityName.Set(securityName);
     _authenticationProtocol = authDigest;
     _authenticationSecret.Set(authSecret);
     _privacyProtocol = privProtocol;
     _privacySecret.Set(privSecret);
 }
Пример #9
0
 /// <summary>
 /// Copy constructor.
 /// </summary>
 /// <param name="value">Class to copy values from</param>
 public UserSecurityModel(UserSecurityModel value)
     : this()
 {
     _engineId.Set(value.EngineId);
     _engineBoots.Value = value.EngineBoots;
     _engineTime.Value  = value.EngineTime;
     _securityName.Set(value.SecurityName);
     _authenticationParameters = new OctetString();
     _privacySecret            = new MutableByte();
     _privacy           = PrivacyProtocols.None;
     _privacyParameters = new OctetString();
 }
Пример #10
0
        /// <summary>
        /// Reset USM object to default values. All OctetString and MutableByte members are reset to 0 length and
        /// privacy and authentication protocols are set to none.
        /// </summary>
        public void Reset()
        {
            _asnType        = 3;
            _engineId       = new OctetString();
            _engineBoots    = new Integer32();
            _engineTime     = new Integer32();
            _authentication = AuthenticationDigests.None;

            _securityName             = new OctetString();
            _authenticationSecret     = new MutableByte();
            _authenticationParameters = new OctetString();
            _privacySecret            = new MutableByte();
            _privacy           = PrivacyProtocols.None;
            _privacyParameters = new OctetString();
        }
Пример #11
0
 /// <summary>
 /// Based on the supplied privacyProtocol, return instance of the privacy protocol implementation class.
 /// </summary>
 /// <param name="privProtocol">Privacy protocol code. Available protocols are <see cref="PrivacyProtocols.DES"/>, 
 /// <see cref="PrivacyProtocols.AES128"/>, <see cref="PrivacyProtocols.AES192"/>, <see cref="PrivacyProtocols.AES256"/> and
 /// <see cref="PrivacyProtocols.TripleDES"/>.</param>
 /// <returns>Privacy protocol implementation class on success. If privacy protocol is <see cref="PrivacyProtocols.None"/>
 /// then null is returned.</returns>
 public static IPrivacyProtocol GetInstance(PrivacyProtocols privProtocol)
 {
     if (privProtocol == PrivacyProtocols.None)
         return null;
     else if (privProtocol == PrivacyProtocols.DES)
         return new PrivacyDES();
     else if (privProtocol == PrivacyProtocols.AES128)
         return new PrivacyAES128();
     else if (privProtocol == PrivacyProtocols.AES192)
         return new PrivacyAES192();
     else if (privProtocol == PrivacyProtocols.AES256)
         return new PrivacyAES256();
     else if (privProtocol == PrivacyProtocols.TripleDES)
         return new Privacy3DES();
     return null;
 }
Пример #12
0
        /// <summary>
        /// Construct and send SNMP v3 authPriv Trap
        /// </summary>
        /// <param name="receiver">Trap receiver IP address</param>
        /// <param name="receiverPort">Trap receiver UDP port number</param>
        /// <param name="engineId">Sender SNMP engineId</param>
        /// <param name="senderEngineBoots">Sender SNMP engine boots</param>
        /// <param name="senderEngineTime">Sender SNMP engine time</param>
        /// <param name="senderUserName">Security (user) name</param>
        /// <param name="senderUpTime">Sender upTime</param>
        /// <param name="trapObjectID">Trap object ID</param>
        /// <param name="varList">Variable binding list</param>
        /// <param name="authDigest">Authentication digest. See <see cref="AuthenticationDigests"/> enumeration for
        /// available digests</param>
        /// <param name="authSecret">Authentication secret</param>
        /// <param name="privProtocol">Privacy protocol. See <see cref="PrivacyProtocols"/> enumeration for
        /// available privacy protocols.</param>
        /// <param name="privSecret">Privacy secret</param>
        public void SendV3Trap(IpAddress receiver, int receiverPort, byte[] engineId, Int32 senderEngineBoots,
                               Int32 senderEngineTime, string senderUserName, UInt32 senderUpTime, Oid trapObjectID, VbCollection varList,
                               AuthenticationDigests authDigest, byte[] authSecret, PrivacyProtocols privProtocol, byte[] privSecret)
        {
            SnmpV3Packet packet = new SnmpV3Packet();

            packet.Pdu.Type = PduType.V2Trap;
            packet.authPriv(ASCIIEncoding.UTF8.GetBytes(senderUserName), authSecret, authDigest, privSecret, privProtocol);
            packet.SetEngineId(engineId);
            packet.SetEngineTime(senderEngineBoots, senderEngineTime);
            packet.ScopedPdu.TrapObjectID.Set(trapObjectID);
            packet.ScopedPdu.TrapSysUpTime.Value = senderUpTime;
            packet.ScopedPdu.VbList.Add(varList);
            packet.MsgFlags.Reportable = false;
            SendV3Trap(packet, receiver, receiverPort);
        }
Пример #13
0
 /// <summary>
 /// Copy constructor. Initialize the class with the values of the parameter class values.
 /// </summary>
 /// <param name="second">Parameter class.</param>
 public SecureAgentParameters(SecureAgentParameters second)
     : this()
 {
     this._contextEngineId.Set(second.ContextEngineId);
     this._contextName.Set(second.ContextName);
     this._engineBoots.Value = second.EngineBoots.Value;
     this._engineId.Set(second.EngineId);
     this._engineTime.Value     = second.EngineTime.Value;
     this._engineTimeStamp      = second.EngineTimeStamp();
     this._maxMessageSize.Value = second.MaxMessageSize.Value;
     this._privacyProtocol      = second.Privacy;
     this._privacySecret.Set(second.PrivacySecret);
     this._authenticationProtocol = second.Authentication;
     this._authenticationSecret.Set(second.AuthenticationSecret);
     this._reportable = second.Reportable;
     this._securityName.Set(second.SecurityName);
 }
        /// <summary>
        /// Copy all relevant values from the SnmpV3Packet class. Do not use this class for
        /// updating the SNMP version 3 discovery process results because secret name, authentication
        /// and privacy values are updated as well which discovery process doesn't use.
        /// </summary>
        /// <param name="packet"><see cref="SnmpV3Packet"/> cast as <see cref="SnmpPacket"/></param>
        /// <exception cref="SnmpInvalidVersionException">Thrown when SNMP packet class other then version 3
        /// is passed as parameter</exception>
        public void UpdateValues(SnmpPacket packet)
        {
            if (packet is SnmpV3Packet pkt)
            {
                _authenticationProtocol = pkt.USM.Authentication;
                _privacyProtocol        = pkt.USM.Privacy;
                _authenticationSecret.Set(pkt.USM.AuthenticationSecret);
                _privacySecret.Set(pkt.USM.PrivacySecret);
                _securityName.Set(pkt.USM.SecurityName);
                if (pkt.MaxMessageSize < _maxMessageSize.Value)
                {
                    _maxMessageSize.Value = pkt.MaxMessageSize;
                }

                UpdateDiscoveryValues(pkt);
            }
            else
            {
                throw new SnmpInvalidVersionException("Invalid SNMP version.");
            }
        }
Пример #15
0
        /// <summary>
        /// Reset the class. Initialize all member values to class defaults.
        /// </summary>
        public void Reset()
        {
            _engineId = new OctetString();
            _engineBoots = new Integer32();
            _engineTime = new Integer32();

            _engineTimeStamp = DateTime.MinValue;

            _privacyProtocol = PrivacyProtocols.None;
            _authenticationProtocol = AuthenticationDigests.None;

            _privacySecret = new MutableByte();
            _authenticationSecret = new MutableByte();

            _contextEngineId = new OctetString();
            _contextName = new OctetString();
            _securityName = new OctetString();

            // max message size is initialized to 64KB by default. It will be
            // to the smaller of the two values after discovery process
            _maxMessageSize = new Integer32(64 * 1024);

            _reportable = true;

            _privacyKey = null;
            _authenticationKey = null;
        }
Пример #16
0
        /// <summary>
        /// Construct and send SNMP v3 authPriv Trap
        /// </summary>
        /// <param name="receiver">Trap receiver IP address</param>
        /// <param name="receiverPort">Trap receiver UDP port number</param>
        /// <param name="engineId">Sender SNMP engineId</param>
        /// <param name="senderEngineBoots">Sender SNMP engine boots</param>
        /// <param name="senderEngineTime">Sender SNMP engine time</param>
        /// <param name="senderUserName">Security (user) name</param>
        /// <param name="senderUpTime">Sender upTime</param>
        /// <param name="trapObjectID">Trap object ID</param>
        /// <param name="varList">Variable binding list</param>
        /// <param name="authDigest">Authentication digest. See <see cref="AuthenticationDigests"/> enumeration for
        /// available digests</param>
        /// <param name="authSecret">Authentication secret</param>
        /// <param name="privProtocol">Privacy protocol. See <see cref="PrivacyProtocols"/> enumeration for
        /// available privacy protocols.</param>
        /// <param name="privSecret">Privacy secret</param>
        public void SendV3Trap(IpAddress receiver, int receiverPort, byte[] engineId, Int32 senderEngineBoots,
			Int32 senderEngineTime, string senderUserName, UInt32 senderUpTime, Oid trapObjectID, VbCollection varList,
			AuthenticationDigests authDigest, byte[] authSecret, PrivacyProtocols privProtocol, byte[] privSecret)
        {
            SnmpV3Packet packet = new SnmpV3Packet();
            packet.Pdu.Type = PduType.V2Trap;
            packet.authPriv(ASCIIEncoding.UTF8.GetBytes(senderUserName), authSecret, authDigest,privSecret, privProtocol);
            packet.SetEngineId(engineId);
            packet.SetEngineTime(senderEngineBoots, senderEngineTime);
            packet.ScopedPdu.TrapObjectID.Set(trapObjectID);
            packet.ScopedPdu.TrapSysUpTime.Value = senderUpTime;
            packet.ScopedPdu.VbList.Add(varList);
            packet.MsgFlags.Reportable = false;
            SendV3Trap(packet, receiver, receiverPort);
        }
Пример #17
0
 /// <summary>
 /// Set packet security to authentication enabled and privacy protection enabled (SNMP v3 mode authPriv)
 /// </summary>
 /// <param name="userName">User name</param>
 /// <param name="authenticationPassword">Authentication password</param>
 /// <param name="authenticationProtocol">Authentication protocol. See definitions in <see cref="AuthenticationDigests"/> enumeration.</param>
 /// <param name="privacyPassword">Privacy protection password.</param>
 /// <param name="privacyProtocol">Privacy protocol. See definitions in <see cref="PrivacyProtocols"/> enumeration.</param>
 public void authPriv(byte[] userName, byte[] authenticationPassword, AuthenticationDigests authenticationProtocol, byte[] privacyPassword, PrivacyProtocols privacyProtocol)
 {
     NoAuthNoPriv(userName); // reset authentication and privacy values and set user name
     _msgFlags.Authentication = true;
     _userSecurityModel.AuthenticationSecret.Set(authenticationPassword);
     _userSecurityModel.Authentication = authenticationProtocol;
     _msgFlags.Privacy = true;
     _userSecurityModel.PrivacySecret.Set(privacyPassword);
     _userSecurityModel.Privacy = privacyProtocol;
 }
Пример #18
0
 /// <summary>
 /// Set packet security to authentication enabled and privacy protection enabled (SNMP v3 mode authPriv)
 /// </summary>
 /// <param name="userName">User name</param>
 /// <param name="authenticationPassword">Authentication password</param>
 /// <param name="authenticationProtocol">Authentication protocol. See definitions in <see cref="AuthenticationDigests"/> enumeration.</param>
 /// <param name="privacyPassword">Privacy protection password.</param>
 /// <param name="privacyProtocol">Privacy protocol. See definitions in <see cref="PrivacyProtocols"/> enumeration.</param>
 public void authPriv(byte[] userName, byte[] authenticationPassword, AuthenticationDigests authenticationProtocol, byte[] privacyPassword, PrivacyProtocols privacyProtocol)
 {
     NoAuthNoPriv(userName);             // reset authentication and privacy values and set user name
     _msgFlags.Authentication = true;
     _userSecurityModel.AuthenticationSecret.Set(authenticationPassword);
     _userSecurityModel.Authentication = authenticationProtocol;
     _msgFlags.Privacy = true;
     _userSecurityModel.PrivacySecret.Set(privacyPassword);
     _userSecurityModel.Privacy = privacyProtocol;
 }
Пример #19
0
        /// <summary>
        /// Reset the class. Initialize all member values to class defaults.
        /// </summary>
        public void Reset()
        {
            _address = new IpAddress(System.Net.IPAddress.Loopback);
            _port = 161;
            _version = SnmpVersion.Ver3;
            _timeout = 2000;
            _retry = 1;

            _engineId = new OctetString();
            _engineBoots = new Integer32();
            _engineTime = new Integer32();

            _engineTimeStamp = DateTime.MinValue;

            _privacyProtocol = PrivacyProtocols.None;
            _authenticationProtocol = AuthenticationDigests.None;

            _privacySecret = new MutableByte();
            _authenticationSecret = new MutableByte();

            _contextEngineId = new OctetString();
            _contextName = new OctetString();
            _securityName = new OctetString();

            // max message size is initialized to 64KB by default. It will be
            // to the smaller of the two values after discovery process
            _maxMessageSize = new Integer32(64 * 1024);

            _reportable = true;
        }
Пример #20
0
 /// <summary>
 /// Copy constructor. Initialize the class with the values of the parameter class values.
 /// </summary>
 /// <param name="second">Parameter class.</param>
 public SecureAgentParameters(SecureAgentParameters second)
     : this()
 {
     this._contextEngineId.Set(second.ContextEngineId);
     this._contextName.Set(second.ContextName);
     this._engineBoots.Value = second.EngineBoots.Value;
     this._engineId.Set(second.EngineId);
     this._engineTime.Value = second.EngineTime.Value;
     this._engineTimeStamp = second.EngineTimeStamp();
     this._maxMessageSize.Value = second.MaxMessageSize.Value;
     this._privacyProtocol = second.Privacy;
     this._privacySecret.Set(second.PrivacySecret);
     this._authenticationProtocol = second.Authentication;
     this._authenticationSecret.Set(second.AuthenticationSecret);
     this._reportable = second.Reportable;
     this._securityName.Set(second.SecurityName);
     if( second.AuthenticationKey != null )
         this._authenticationKey = (byte[])second.AuthenticationKey.Clone();
     if( second.PrivacyKey != null )
         this._privacyKey = (byte[])second.PrivacyKey.Clone();
 }
Пример #21
0
 /// <summary>
 /// Prepare class for authPriv operations.
 /// </summary>
 /// <param name="securityName">User security name</param>
 /// <param name="authDigest">Authentication protocol</param>
 /// <param name="authSecret">Authentication secret (password)</param>
 /// <param name="privProtocol">Privacy protocol</param>
 /// <param name="privSecret">Privacy secret (encryption password)</param>
 public void authPriv(String securityName, AuthenticationDigests authDigest, String authSecret, PrivacyProtocols privProtocol, String privSecret)
 {
     _securityName.Set(securityName);
     _authenticationProtocol = authDigest;
     _authenticationSecret.Set(authSecret);
     _privacyProtocol = privProtocol;
     _privacySecret.Set(privSecret);
 }
Пример #22
0
 /// <summary>
 /// Prepare class for authNoPriv operations. Set privacy protocol to none
 /// </summary>
 /// <param name="securityName">User security name</param>
 /// <param name="authDigest">Authentication protocol</param>
 /// <param name="authSecret">Authentication secret (password)</param>
 public void authNoPriv(String securityName, AuthenticationDigests authDigest, String authSecret)
 {
     _securityName.Set(securityName);
     _authenticationProtocol = authDigest;
     _authenticationSecret.Set(authSecret);
     _privacyProtocol = PrivacyProtocols.None;
     _privacySecret.Clear();
 }
Пример #23
0
        /// <summary>
        /// Standard constructor.
        /// </summary>
        public UserSecurityModel()
        {
            _asnType = 3;
            _engineId = new OctetString();
            _engineBoots = new Integer32();
            _engineTime = new Integer32();
            _authentication = AuthenticationDigests.None;

            _securityName = new OctetString();
            _authenticationSecret = new MutableByte();
            _authenticationParameters = new OctetString();
            _privacySecret = new MutableByte();
            _privacy = PrivacyProtocols.None;
            _privacyParameters = new OctetString();
        }
Пример #24
0
 /// <summary>
 /// Prepare class for noAuthNoPriv operations. Set authentication and privacy protocols to none.
 /// </summary>
 /// <param name="securityName">User security name</param>
 public void noAuthNoPriv(String securityName)
 {
     _securityName.Set(securityName);
     _authenticationProtocol = AuthenticationDigests.None;
     _authenticationSecret.Clear();
     _privacyProtocol = PrivacyProtocols.None;
     _privacySecret.Clear();
 }
Пример #25
0
        static void Main(string[] args)
        {
            InitLogger(true);
            AuthenticationDigests authDigest      = AuthenticationDigests.None;
            PrivacyProtocols      privacyprotocol = PrivacyProtocols.None;
            string       ip           = "172.19.27.8";
            int          port         = 161;
            VbCollection vbCollection = null;
            string       community    = "public";
            UdpTarget    target       = null;
            int          version      = 2;
            SnmpV2Packet result2      = null;
            int          ErrorStatus  = -1;
            int          ErrorIndex   = -1;

            string sidbase2 = "1.3.6.1.4.1.1429.2.2.6.5.12.3.1.2.";
            string NameBase = "1.3.6.1.4.1.1429.2.2.6.5.12.3.1.4.";
            string sidbase  = "1.3.6.1.4.1.8813.2.366";

            try
            {
                String     snmpAgent     = "172.19.27.8";
                String     snmpCommunity = "public";
                SimpleSnmp snmp          = new SimpleSnmp(snmpAgent, snmpCommunity);
                // Create a request Pdu
                Pdu pdu = new Pdu();
                pdu.Type = PduType.GetNext;
                List <string> lst1 = new List <string>();
                List <string> lst2 = new List <string>();

                string sid         = "";
                string channelName = "";

                Dictionary <string, string> channelList = new Dictionary <string, string>();
                Dictionary <Oid, AsnType>   result      = null;

                for (int i = 0; i < 1; i++)
                {
                    sid         = "";
                    channelName = "";
                    result      = snmp.GetNext(SnmpVersion.Ver2, new string[] { (sidbase + i) });
                    if (result == null)
                    {
                        logger.Debug("Request failed.");
                    }
                    else
                    {
                        foreach (KeyValuePair <Oid, AsnType> entry in result)
                        {
                            if (entry.Key.ToString().Contains(sidbase))
                            {
                                sid = entry.Value.ToString();
                            }
                            else
                            {
                                i = 100;
                            }
                        }
                    }
                    result = snmp.GetNext(SnmpVersion.Ver2, new string[] { (NameBase + i) });
                    if (result == null)
                    {
                        logger.Debug("Request failed.");
                    }
                    else
                    {
                        foreach (KeyValuePair <Oid, AsnType> entry in result)
                        {
                            if (entry.Key.ToString().Contains(NameBase))
                            {
                                channelName = entry.Value.ToString();
                            }
                        }
                    }

                    if (!sid.Equals("") && !channelName.Equals(""))
                    {
                        channelList.Add(sid, channelName);
                    }
                }

                foreach (var item in channelList)
                {
                    logger.Debug(item.Key + ":" + item.Value);
                }
            }
            catch (Exception e)
            {
                logger.Debug(e.Message);
                logger.Debug(e.StackTrace);
            }

            /*
             * try
             * {
             *  VbCollection coll = new VbCollection();
             *  coll.Add("1.3.6.1.4.1.1429.2.2.6.5.2.1.1.1");
             *  coll.Add("1.3.6.1.4.1.1429.2.2.6.5.2.1.1.2");
             *  Pdu pdu = new Pdu(coll, PduType.GetBulk, 0);
             *  pdu.MaxRepetitions = 10;
             *  String snmpAgent = "172.19.54.30";
             *  String snmpCommunity = "public";
             *  SimpleSnmp snmp = new SimpleSnmp(snmpAgent, snmpCommunity);
             *  for (int i = 0; i < 1; i++)
             *  {
             *      Dictionary<Oid, AsnType> result = snmp.GetBulk(pdu);
             *
             *      if (result == null)
             *      {
             *          logger.Debug("Request failed.");
             *      }
             *      else
             *      {
             *          logger.Debug("result size: " + result.Count);
             *          string filename = "D:\\Amir\\SNMP " + i + ".txt";
             *          using (System.IO.StreamWriter file =
             *          new System.IO.StreamWriter(filename))
             *          {
             *              foreach (KeyValuePair<Oid, AsnType> entry in result)
             *              {
             *                  var x = entry.Value;
             *                  file.WriteLine("{0} = {1}: {2}", entry.Key.ToString(), SnmpConstants.GetTypeName(entry.Value.Type),
             *                  entry.Value.ToString());
             *              }
             *          }
             *
             *      }
             *  }
             *
             *
             * }
             * catch (Exception e)
             * {
             *
             *  logger.Debug(e.Message);
             *  logger.Debug(e.StackTrace);
             * }
             */
            /*try
             * {
             *  IpAddress ipa = new IpAddress(ip);
             *  target = new UdpTarget((IPAddress)ipa);
             *  target.Port = port;
             *  // Construct a Protocol Data Unit (PDU)
             *  Pdu pdu = new Pdu(PduType.Get);
             *  // Set the request type (default is Get)
             *  // pdu.Type = PduType.Get;
             *  // Add variables you wish to query
             *  pdu.VbList.Add(oid); //oid);
             *  OctetString communityOctet = new OctetString(community);
             *  AgentParameters param = new AgentParameters(communityOctet);
             *
             *
             *
             *  param.Version = SnmpVersion.Ver2;
             *  logger.Debug("before get");
             *  result2 = (SnmpV2Packet)target.Request(pdu, param);
             *  logger.Debug("after get");
             *
             *  vbCollection = result2.Pdu.VbList;
             *  ErrorStatus = result2.Pdu.ErrorStatus;
             *  ErrorIndex = result2.Pdu.ErrorIndex;
             *
             *  if (ErrorStatus != 0)
             *  {
             *      // agent reported an error with the request
             *      logger.Debug("Error in SNMP reply. Error {0} index {1}",
             *          ErrorStatus, ErrorIndex);
             *  }
             *  else
             *  {
             *      if (vbCollection != null)
             *      {
             *          foreach (Vb v in vbCollection)
             *          {
             *              logger.Debug("OID={0}\r\nType={1}\r\nValue:{2}",
             *                  v.Oid.ToString(),
             *                  SnmpConstants.GetTypeName(v.Value.Type), v.Value.ToString());
             *          }
             *      }
             *      else logger.Debug("VB COLLECTION IS NULL");
             *  }
             *  target.Close();
             * }
             * catch (Exception ex)
             * {
             *  if (target != null)
             *      target.Close();
             *  logger.Debug("Exception: " + ex.Message + " type: " + ex.GetType().ToString());
             * }
             */
            /*
             * string oid = "";
             * string ip = "";
             * int port = 161;
             * string securityuser = "";
             * string authenticationdigest = "";
             * AuthenticationDigests authDigest = AuthenticationDigests.None;
             * string authenticationpassword = "";
             * PrivacyProtocols privacyprotocol = PrivacyProtocols.None;
             * string privacyprotocolstring = "";
             * string privacyprotocolpassword = "";
             * string community = "";
             * int version = -1;
             * SnmpV3Packet result3;
             * SnmpV1Packet result1;
             * SnmpV2Packet result2;
             *
             * int ErrorStatus = -1;
             * int ErrorIndex = -1;
             * if (args.Length == 0)
             * {
             *  Console.WriteLine("Specify arguments:\r\n" +
             *  "-v:version (Example: -v:1,-v:2c, -v:3)\r\n" +
             *  "-r:IP (Example: -r:192.168.1.0)\r\n" +
             *  "-p:port (Optional, default 161, example: -p:555\r\n" +
             *  "-o:OID (Example: -o:1.3.6.1.2.1.1.2.0" +
             *  "-c:community_string (Example: -c:public)\r\n" +
             *  "-ap:AuthenticationDigest SHA/MD5(Example: -ap:SHA)\r\n" +
             *  "-aw:AuthenticationPassword\r\n" +
             *  "-pp:PrivacyProtocol DES/AES128/AES192/AES256/TripleDES (Example: -pp:AES128)\r\n" +
             *  "-pp:PrivacyPassword\r\n" +
             *  "-sn:SecurityName");
             *  return;
             * }
             * else
             * {
             *  foreach (string arg in args)
             *  {
             *      if (arg.IndexOf("-v:") == 0)
             *      {
             *          if (arg.Substring(3) == "1")
             *              version = 1;
             *          if (arg.Substring(3) == "2c")
             *              version = 2;
             *          if (arg.Substring(3) == "3")
             *              version = 3;
             *      }
             *      if (arg.IndexOf("-o:") == 0)
             *      {
             *          oid = arg.Substring(3);
             *      }
             *      if (arg.IndexOf("-r:") == 0)
             *      {
             *          ip = arg.Substring(3);
             *      }
             *      if (arg.IndexOf("-sn:") == 0)
             *      {
             *          securityuser = arg.Substring(4);
             *      }
             *      if (arg.IndexOf("-c:") == 0)
             *      {
             *          community = arg.Substring(3);
             *      }
             *      if (arg.IndexOf("-p:") == 0)
             *      {
             *          string ports = arg.Substring(3);
             *          try
             *          {
             *              port = Int32.Parse(ports);
             *          }
             *          catch (Exception ex)
             *          {
             *              port = -1;
             *              Console.WriteLine("Not correct port value: " + ports);
             *              break;
             *          }
             *      }
             *      if (arg.IndexOf("-ap:") == 0)
             *      {
             *          authenticationdigest = arg.Substring(4);
             *          if (String.Compare(authenticationdigest, "SHA", true) == 0)
             *              authDigest = AuthenticationDigests.SHA1;
             *          if (String.Compare(authenticationdigest, "MD5", true) == 0)
             *              authDigest = AuthenticationDigests.MD5;
             *      }
             *      if (arg.IndexOf("-aw:") == 0)
             *      {
             *          authenticationpassword = arg.Substring(4);
             *      }
             *      if (arg.IndexOf("-pp:") == 0)
             *      {
             *          privacyprotocolstring = arg.Substring(4);
             *          if (String.Compare(privacyprotocolstring, "DES", true) == 0)
             *              privacyprotocol = PrivacyProtocols.DES;
             *          if (String.Compare(privacyprotocolstring, "AES128", true) == 0)
             *              privacyprotocol = PrivacyProtocols.AES128;
             *          if (String.Compare(privacyprotocolstring, "AES192", true) == 0)
             *              privacyprotocol = PrivacyProtocols.AES192;
             *          if (String.Compare(privacyprotocolstring, "AES256", true) == 0)
             *              privacyprotocol = PrivacyProtocols.AES256;
             *          if (String.Compare(privacyprotocolstring, "TripleDES", true) == 0)
             *              privacyprotocol = PrivacyProtocols.TripleDES;
             *      }
             *      if (arg.IndexOf("-pw:") == 0)
             *      {
             *          privacyprotocolpassword = arg.Substring(4);
             *      }
             *  }
             * }
             * if (port == -1)
             * {
             *  Console.WriteLine("Not valid port: " + port.ToString());
             *  return;
             * }
             * if ((version < 1) || (version > 3))
             * {
             *  Console.WriteLine("Not valid version: ");
             *  return;
             * }
             */
        }
Пример #26
0
 /// <summary>
 /// Copy all relevant values from the SnmpV3Packet class. Do not use this class for
 /// updating the SNMP version 3 discovery process results because secret name, authentication
 /// and privacy values are updated as well which discovery process doesn't use.
 /// </summary>
 /// <param name="packet"><see cref="SnmpV3Packet"/> cast as <see cref="SnmpPacket"/></param>
 /// <exception cref="SnmpInvalidVersionException">Thrown when SNMP packet class other then version 3 
 /// is passed as parameter</exception>
 public void UpdateValues(SnmpPacket packet)
 {
     if (packet is SnmpV3Packet)
     {
         SnmpV3Packet pkt = (SnmpV3Packet)packet;
         _authenticationProtocol = pkt.USM.Authentication;
         _privacyProtocol = pkt.USM.Privacy;
         _authenticationSecret.Set(pkt.USM.AuthenticationSecret);
         _privacySecret.Set(pkt.USM.PrivacySecret);
         _securityName.Set(pkt.USM.SecurityName);
         if (pkt.MaxMessageSize < _maxMessageSize.Value)
             _maxMessageSize.Value = pkt.MaxMessageSize;
         UpdateDiscoveryValues(pkt);
     }
     else
         throw new SnmpInvalidVersionException("Invalid SNMP version.");
 }
Пример #27
0
 /// <summary>
 /// Copy constructor.
 /// </summary>
 /// <param name="value">Class to copy values from</param>
 public UserSecurityModel(UserSecurityModel value)
     : this()
 {
     _engineId.Set(value.EngineId);
     _engineBoots.Value = value.EngineBoots;
     _engineTime.Value = value.EngineTime;
     _securityName.Set(value.SecurityName);
     _authenticationParameters = new OctetString();
     _privacySecret = new MutableByte();
     _privacy = PrivacyProtocols.None;
     _privacyParameters = new OctetString();
 }