Public/Invoke-SNMPv3Get.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
function Invoke-SNMPv3Get
{

<#
 
.SYNOPSIS
 
    Query information from network devices using SNMP GET requests.
 
.DESCRIPTION
 
    Function supports SNMPv3 with security levels noAuthNoPriv, authNoPriv and authPriv.
    Based on #SNMP Library (https://www.sharpsnmp.com/)
 
.PARAMETER UserName
 
    Username to use when polling information.
 
.PARAMETER Target
 
    SNMP Agent you want to get information from. Accepts IP address or host name.
 
.PARAMETER OID
 
    Object Identifier to get the value from.
 
.PARAMETER AuthType
 
    Allowed authentication types are None, MD5 and SHA1. Defaults to None.
 
.PARAMETER AuthSecret
 
    Authentication password used for security level authNoPriv and authPriv.
 
.PARAMETER PrivType
 
    Allowed encryption types are None, DES, TripleDES, AES128, AES192 and AES256. Defaults to None.
 
.PARAMETER PrivSecret
 
    Encryption password used for security level authPriv.
 
.PARAMETER Context
 
    Context to use. For example to request information in the context of a specific vlan.
 
.PARAMETER Port
 
    UDP port to use when connecting to the SNMP Agent. Defaults to 161.
 
.PARAMETER Timeout
 
    Timeout in milliseconds when connecting to SNMP Agent. Defaults to 3000.
 
.INPUTS
 
    None. You cannot pipe objects to SNMPv3Get.
 
.OUTPUTS
 
    SNMPv3Output
 
.EXAMPLE
 
    PS> Invoke-SNMPv3Get -UserName usr-none-none -Target demo.snmplabs.com -OID 1.3.6.1.2.1.1.1.0
 
    Node OID Type Value
    ---- --- ---- -----
    104.236.166.95 1.3.6.1.2.1.1.1.0 OctetString Linux zeus 4.8.6.5-smp #2 SMP Sun Nov 13 14:58:11 CDT 2016 i686
 
#>


    [CmdletBinding()]
    [OutputType('SNMPv3Output')]
    param(
        [Parameter(Mandatory=$true)]
        [String]$UserName,

        [Parameter(Mandatory=$true)]
        [String]$Target,

        [Parameter(Mandatory=$true)]
        [String[]]$OID,

        [Parameter(Mandatory=$false)]
        [SNMPv3AuthType]$AuthType = 'None',

        [Parameter(Mandatory=$false)]
        [String]$AuthSecret,

        [Parameter(Mandatory=$false)]
        [SNMPv3PrivType]$PrivType = 'None',

        [Parameter(Mandatory=$false)]
        [String]$PrivSecret,

        [Parameter(Mandatory=$false)]
        [String]$Context,

        [Parameter(Mandatory=$false)]
        [int]$Port = 161,

        [Parameter(Mandatory=$false)]
        [int]$Timeout = 3000
    )

    $SecurityLevel = Get-SNMPv3SecurityLevel $AuthType $AuthSecret $PrivType $PrivSecret

    if ($SecurityLevel.IsValid)
    {
        $Authentication = Get-SNMPv3AuthenticationProvider $AuthType $AuthSecret
        $Privacy = Get-SNMPv3PrivacyProvider $PrivType $PrivSecret $Authentication
    }
    else
    {
        $InvalidSecurityLevel = [System.FormatException]::new('Invalid security level provided')
        Throw $InvalidSecurityLevel
    }

    $Context = if ([String]::IsNullOrWhiteSpace($Context)) {[String]::Empty} else {$Context}
    
    $IPAddress = [ipaddress]::None
    if ([ipaddress]::TryParse($Target, [ref]$IPAddress) -eq $false) {
        $IPAddress  = [System.Net.Dns]::GetHostEntry($Target).AddressList[0]
    }

    $IPEndPoint = [System.Net.IPEndPoint]::new($IPAddress, $Port)

    $Discovery = [Lextm.SharpSnmpLib.Messaging.Messenger]::GetNextDiscovery([Lextm.SharpSnmpLib.SnmpType]::GetRequestPdu)
    $Report = $Discovery.GetResponse($Timeout, $IPEndPoint)

    $VariableList = [System.Collections.Generic.List[Lextm.SharpSnmpLib.Variable]]::new()

    foreach ($ObjectIdentifier in $OID)
    {
        $VariableList.Add([Lextm.SharpSnmpLib.ObjectIdentifier]::new($ObjectIdentifier))
    }

    $Request = [Lextm.SharpSnmpLib.Messaging.GetRequestMessage]::new(
        [Lextm.SharpSnmpLib.VersionCode]::V3,
        [Lextm.SharpSnmpLib.Messaging.Messenger]::NextMessageId,
        [Lextm.SharpSnmpLib.Messaging.Messenger]::NextRequestId,
        [Lextm.SharpSnmpLib.OctetString]::new($UserName),
        [Lextm.SharpSnmpLib.OctetString]::new($Context),
        $VariableList,
        $Privacy,
        [Lextm.SharpSnmpLib.Messaging.Messenger]::MaxMessageSize,
        $Report
    )

    $Reply = [Lextm.SharpSnmpLib.Messaging.SnmpMessageExtension]::GetResponse($Request, $Timeout, $IPEndPoint)

    if ($Reply -is [Lextm.SharpSnmpLib.Messaging.ReportMessage])
    {
        if ($Reply.Scope.Pdu.Variables.Count -eq 0)
        {
            Write-Warning "wrong report message received"
            return
        }

        $Id = $Reply.Scope.Pdu.Variables[0].Id
        if ($Id -ne [Lextm.SharpSnmpLib.Messaging.Messenger]::NotInTimeWindow)
        {
            $ErrMsg = [Lextm.SharpSnmpLib.Messaging.Messenger]::GetErrorMessage($Id)
            Write-Warning $ErrMsg
            return
        }

        # according to RFC 3414, send a second request to sync time.
        $Request = [Lextm.SharpSnmpLib.Messaging.GetRequestMessage]::new(
            [Lextm.SharpSnmpLib.VersionCode]::V3,
            [Lextm.SharpSnmpLib.Messaging.Messenger]::NextMessageId,
            [Lextm.SharpSnmpLib.Messaging.Messenger]::NextRequestId,
            [Lextm.SharpSnmpLib.OctetString]::new($UserName),
            [Lextm.SharpSnmpLib.OctetString]::new($Context),
            $VariableList,
            $Privacy,
            [Lextm.SharpSnmpLib.Messaging.Messenger]::MaxMessageSize,
            $Reply
        )

        $Reply = [Lextm.SharpSnmpLib.Messaging.SnmpMessageExtension]::GetResponse($Request, $Timeout, $IPEndPoint)
    }
    elseif ($Reply.ErrorStatus -ne [Lextm.SharpSnmpLib.ErrorCode]::NoError)
    {
        throw [Lextm.SharpSnmpLib.Messaging.ErrorException]::Create(
            "error in response",
            $IPEndPoint.Address,
            $Reply
        )
    }

    $Reply.Scope.Pdu.Variables | foreach {
        [PSCustomObject] @{
            PSTypeName = 'SNMPv3Output'
            Node       = $IPAddress
            OID        = $_.Id.ToString()
            Type       = $_.Data.TypeCode
            Value      = $_.Data
        }
    }
}