Exemple #1
0
        public async Task <(int bulkwalkResult, IList <Variable> results)> GetSubtreeV3UsmAsync(IPAddress ip, string oid, string community, int port, int?maxRepetitions,
                                                                                                int?retries, TimeSpan?timeout, string authPassword, string privPassword)
        {
            if (ip == null)
            {
                throw new ArgumentNullException(nameof(ip));
            }

            if (string.IsNullOrWhiteSpace(oid))
            {
                throw new ArgumentNullException(nameof(oid));
            }

            if (!Regex.IsMatch(oid, @"^(([0-9]+)\.)+[0-9]+$"))
            {
                throw new ArgumentException(oid, nameof(oid));
            }

            if (port <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(port), port.ToString());
            }

            var maxRepetitionsValue = maxRepetitions ?? 10;

            if (maxRepetitionsValue <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(maxRepetitions), maxRepetitions.ToString());
            }

            var retriesValue = retries ?? 2;

            if (retriesValue <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(retries), retries.ToString());
            }

            var timeoutMs = timeout ?? TimeSpan.FromSeconds(5);

            if (timeoutMs <= TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(timeout), timeout.ToString());
            }

            if (string.IsNullOrWhiteSpace(authPassword))
            {
                throw new ArgumentNullException(nameof(authPassword));
            }

            if (string.IsNullOrWhiteSpace(privPassword))
            {
                throw new ArgumentNullException(nameof(privPassword));
            }

            var discovery = Messenger.GetNextDiscovery(SnmpType.GetRequestPdu);
            var report    = await discovery.GetResponseAsync(new IPEndPoint(ip, 161)).ConfigureAwait(false);

            var auth = new SHA1AuthenticationProvider(new OctetString(authPassword)); // AuthenticationPassword
            var priv = new DESPrivacyProvider(new OctetString(privPassword), auth);   //PrivacyPassword

            var results        = new List <Variable>();
            var bulkwalkResult = await MyMessenger.BulkWalkV3UsmAsync(
                new IPEndPoint(ip, port),
                community == null?OctetString.Empty : new OctetString(community),
                new ObjectIdentifier(oid),
                results,
                maxRepetitionsValue,
                retriesValue,
                timeoutMs,
                WalkMode.WithinSubtree,
                priv,
                report
                ).ConfigureAwait(false);

            return(bulkwalkResult, results);
        }