The greatest challenge to any thinker is stating the problem in a way that will allow a solution

Bertrand Russell

By

On 17 Jun 2013

PowerShell

Tags: , , ,


This script utilises a free 3rd party .NET library in order to be able to run SNMP queries from within PowerShell.  I created it in order to be able to keep track of the creation and usage of DHCP scopes during a migration project, and meant that I could instantly find out how short or not we were of available IP addresses.  Longer term you’d obviously want to set-up proper monitoring.

For more info on setting up the SNMP .NET library see my vWiki page – http://vwiki.co.uk/SNMP_and_PowerShell.  Its quite simple.  You have to download the binary package, extract and find the SharpSnmpLib.dll file, then load that into your PowerShell session and hey presto – you have SNMP functionality in PowerShell!

The Script

&<###################################################################################################################
SNMP DHCP Server Poller Script
 
By Simon Strutt - May 2013
 
Version 0.1
- Initial creation
 
####################################################################################################################>
 
$DHCPserver = "77.243.171.48"
$ROcommunity = "public"
 
# SNMP Fucntion ------------------------------------------------------------------------------------------------------------------------
 
function Invoke-SnmpWalk {
    # Create OID object
    $oid = New-Object Lextm.SharpSnmpLib.ObjectIdentifier ($sOIDstart)
 
    # Create list for results
    $results = New-Object 'System.Collections.Generic.List[Lextm.SharpSnmpLib.Variable]'
 
    # Create endpoint for SNMP server
    $ip = [System.Net.IPAddress]::Parse($DHCPserver)
    $svr = New-Object System.Net.IpEndPoint ($ip, 161)
 
    # Use SNMP v2 and walk mode WithinSubTree (as opposed to Default)
    $ver = [Lextm.SharpSnmpLib.VersionCode]::V2
    $walkMode = [Lextm.SharpSnmpLib.Messaging.WalkMode]::Default
 
    # Perform SNMP Get
    try {
        [Lextm.SharpSnmpLib.Messaging.Messenger]::BulkWalk($ver, $svr, $ROcommunity, $oid, $results, 10, 10, $walkMode, $null, $null)
    } catch {
        Write-Host "SNMP Walk error: $_"
        Return $null
    }
 
    $res = @()
    foreach ($var in $results) {
        $line = "" | Select OID, Data
        $line.OID = $var.Id.ToString()
        $line.Data = $var.Data.ToString()
        $res += $line
    }
 
$res
}
 
# -----------------------------------------------------------------------------------------------------------------------------------
 
# Load SNMP assembly
$SnmpAss = [reflection.assembly]::LoadFrom( (Resolve-Path ".\lib\LexTmSharpSNMP\SharpSnmpLib.dll") )
 
# Starting OID for SNMP Walk
$sOIDstart = ".1.3.6.1.4.1.311.1.3.2.1.1"
 
$SNMPresult = Invoke-SnmpWalk
 
# Parse results
$Scopes = @()
foreach ($row in $SNMPresult) {
    if ($row.OID -match ".1.3.6.1.4.1.311.1.3.2.1.1.1.") {
        $scope = "" | Select Subnet, InUse, Free, PcentFull
        $scope.Subnet = $row.Data
        $scope.InUse = [int]($SNMPresult |?{$_.OID -eq (".1.3.6.1.4.1.311.1.3.2.1.1.2." + $scope.Subnet)} | Select Data).Data
        $scope.Free = [int]($SNMPresult |?{$_.OID -eq (".1.3.6.1.4.1.311.1.3.2.1.1.3." + $scope.Subnet)} | Select Data).Data
        $scope.PcentFull = [math]::Round($scope.InUse / ($scope.InUse + $scope.Free) * 100)
        $Scopes += $scope
    }
}
 
# Output results
$Scopes

 


Leave a Reply

XHTML: You can use these tags if you know what they are: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

ERROR: si-captcha.php plugin says GD image support not detected in PHP!

Contact your web host and ask them why GD image support is not enabled for PHP.

ERROR: si-captcha.php plugin says imagepng function not detected in PHP!

Contact your web host and ask them why imagepng function is not enabled for PHP.