Snmp Value as Hex String

Hi,

I’m trying to obtain the MAC Address of printers and using snmpwalk, I can see it returns a hex-string like this:

.1.3.6.1.2.1.2.2.1.6.1 = HEX-STRING: 1C7D220D0768

Base on the above OID, i cobbled up the below code to return the Mac Address:

$PrinterAddress = "10.20.30.40"
$SNMP = New-Object -ComObject olePrn.OleSNMP
$SNMP.Open($PrinterAddress, "public", 2, 1000)
$MacAddress = $SNMP.Get(".1.3.6.1.2.1.2.2.1.6.1")
$MacAddress

But the above returns the following value:

h}"

I’m not even sure where to go from here. Any pointers would be much appreciated.

Thanks.

Does that happen to be a fujifilm printer? Because that hex string looks like a MAC address and when I look up the MAC it shows as a fujifilm

Yes, Fuji Xerox to be exact.

I’m trying to return that mac address hex string thingee “1C7D220D0768” but getting some weird 3 character thingee instead.

I tried several printers with your example code (which is the first google record) and a few modules that used custom DLLs. I wasn’t having much luck because they would return gibberish from different printers. However SNMPWalk works great. I played around with this and ended up making this function.

Function Get-PrinterMacAddress {
    [cmdletbinding()]
    Param(
        [parameter(ValueFromPipeline)]
        $IP,
        $Community = 'Public',
        $Port = 161,
        $Timeout = 5
    )

    begin {
        $oidlist = '1.3.6.1.2.1.2.2.1.6','.1.3.6.1.2.1.55.1.5.1.8'
        $snmpwalk = Join-Path $env:TEMP 'SNMPWalk\snmpwalk.exe'

        if(-not (Test-Path $snmpwalk)){
            $zipfile = Join-Path $env:TEMP SNMPWalk.zip
            $destination = New-Item (Split-Path $snmpwalk -Parent) -Force -ItemType Directory
            Invoke-WebRequest -UseBasicParsing 'https://dl.ezfive.com/snmpsoft-tools/SnmpWalk.zip?_gl=1*19n1cvv*_ga*MjAzNzczMjA0NS4xNjY3OTc4ODUx*_ga_BEFD2E3R5Z*MTY2Nzk3ODg1MC4xLjEuMTY2Nzk3ODg4My4yNy4wLjA.' -OutFile $zipfile
            $shell = New-Object -ComObject Shell.Application
            $shell.Namespace($destination.FullName).copyhere(($shell.NameSpace($zipfile)).items(),1540)
        }
    }

    process {
        foreach($printer in $IP){
            foreach($oid in $oidlist){
                $output = & $snmpwalk -r:$printer -os:"$oid.0" -op:"$oid.2" -p:$Port -t:$Timeout -csv

                Write-Verbose "[$printer] $output"

                $results = $output |
                    ConvertFrom-Csv -Header OID, Type, Value, Value1 |
                        Where-Object Value -match '^(\w\w\s){6}\s+?$'
        
                if(-not $results){
                    continue
                }

                $mac = $results.Value.Trim() -replace '\s',':'

                if($mac){
                    [PSCustomObject]@{
                        IP  = $printer
                        MAC = $mac
                    }
                    break
                }
            }
        }
    }
}

You can test it out with this example

$PrinterAddress = "10.20.30.40", "10.20.30.50", "10.20.30.51"

Get-PrinterMacAddress -IP $PrinterAddress

You can also pipe the IPs to the function

$PrinterAddress = "10.20.30.40", "10.20.30.50", "10.20.30.51"

$PrinterAddress | Get-PrinterMacAddress -Verbose -Timeout 1

The function works a treat. Much appreciated.

I was playing around with snmpwalk.exe and the -os and -op parameters took a bit of trial and error to figure out. Since I only needed the value from “.1.3.6.1.2.1.2.2.1.6.1”, I tried -os and -op with the same oid but nothing was returned.

Turns out you need to actually give a range of oids and it’ll return all except the starting oid (-os). So in my case, I needed to …

$PrinterAddress = "10.20.30.40"
$OidStart = ".1.3.6.1.2.1.2.2.1.6.0"
$OidEnd = ".1.3.6.1.2.1.2.2.1.6.1"
$SnmpWalk = "\\Path\to\SnmpWalk.exe"
$MacAddress = (& $SnmpWalk -r:$PrinterAddress -os:$OidStart -op:$OidEnd | Select-String -Pattern "Value" | Out-String).Trim().Split("=")[3].TrimStart() -replace " ",":"
$MacAddress

… and it’ll return the value of “.1.3.6.1.2.1.2.2.1.6.1”, but not “.1.3.6.1.2.1.2.2.1.6.0”.

If $OidStart is .1 and $OidEnd is .2, it’ll return the value of .2 only … which in my case was null as only interface 1 (".1.3.6.1.2.1.2.2.1.6.1) had a MAC address value.