Skip to content

Instantly share code, notes, and snippets.

@fproperzi
Created March 7, 2023 07:52
Show Gist options
  • Save fproperzi/5269baf2e493046b05ff341029e8e5f5 to your computer and use it in GitHub Desktop.
Save fproperzi/5269baf2e493046b05ff341029e8e5f5 to your computer and use it in GitHub Desktop.
Check printers with snmp call to specific OID
# other powershell snmp information
# https://solvedbypowershell.blogspot.com/2014/12/powershell-using-snmp-for-html-network.html
# https://www.powershellgallery.com/packages/Proxx.SNMP/1.0.1.2/Content/Invoke-SnmpGet.ps1
# https://www.powershellgallery.com/packages/Proxx.SNMP/1.0.0.1/Content/Invoke-SnmpWalk.ps1
# https://exchange.nagios.org/directory/Plugins/Hardware/Printers/SNMP-Printer-Check/details
# https://www.powershellgallery.com/packages/Proxx.SNMP/1.1.1.4
# https://www.reddit.com/r/PowerShell/comments/77ls36/printer_page_counter_from_print_server/
# https://gallery.technet.microsoft.com/Get-PrintStatistics-a6bb8323
# https://gallery.technet.microsoft.com/scriptcenter/Script-to-generate-print-84bdcf69
# https://github.com/vvalchev/printer-scanner/blob/master/snmp_func.go
function Get-SNMPPrinterInfo {
param (
[string[]]$printers
)
begin {
$snmp = New-Object -ComObject olePrn.OleSNMP
$ping = New-Object System.Net.NetworkInformation.Ping
}
process {
foreach ($printer in $printers) {
try {
$result = $ping.Send($printer)
} catch {
$result = $null
}
if ($result.Status -eq 'Success') {
# get the ip address
$printerip = $result.Address.ToString()
# OPEN SNMP CONNECTION TO PRINTER
$snmp.open($printerip, 'public', 2, 3000)
$test = ""
# MODEL
try { $model = $snmp.Get('.1.3.6.1.2.1.25.3.2.1.3.1') } catch { $model = $null }
# IF IT HAS A MODEL NAME...
if ($model) {
# DESCRIPTION
try { $sysdescr0 = $snmp.Get('.1.3.6.1.2.1.1.1.0') } catch { $sysdescr0 = $null }
# COLOR
# might want to check on this one
try { if ($snmp.Get('.1.3.6.1.2.1.43.11.1.1.6.1.2') -match 'Toner|Cartridge') { $color = 'Yes' } else { $color = 'No' } } catch { $color = 'No' }
# TRAYS
try { $trays = $($snmp.GetTree('.1.3.6.1.2.1.43.8.2.1.13') | ? {$_ -notlike 'print*'}) -join ';' } catch { $trays = $null }
# COMMENT
try { $comment = $snmp.Get('.1.3.6.1.2.1.1.6.0') } catch { $comment = $null }
# PAGE COUNT
try { $pagecount = $snmp.Get('.1.3.6.1.2.1.43.10.2.1.4.1.1') } catch { $pagecount = $null }
# TONER black (PRC = %) https://iphostmonitor.com/monitoring-toner-level-in-snmp-capable-hp-printer.html
try {$toner_full = $snmp.Get('.1.3.6.1.2.1.43.11.1.1.8.1.1') } catch { $toner_full = $null }
try {$toner_level = $snmp.Get('.1.3.6.1.2.1.43.11.1.1.9.1.1') } catch { $toner_level = $null }
try {$toner = [int](100 * $toner_level / $toner_full) } catch { $toner = $null }
#SERIAL
try { $serial = $snmp.Get('.1.3.6.1.2.1.43.5.1.1.17.1').trim().toupper() } catch { $serial = $null }
##### FEATURES, NAME
$features = $null
$name = $null
switch -Regex ($model) {
'^sharp' {
try { $features = $($snmp.GetTree('.1.3.6.1.4.1.2385.1.1.3.2.1.3') | ? {$_ -notlike '.*'}) -join ';' } catch { $features = $null }
try { $name = $snmp.Get('.1.3.6.1.4.1.1536.1.3.5.4.2.0').toupper() } catch { $name = $null }
}
'^canon' {
try { $name = $snmp.Gettree('.1.3.6.1.4.1.1602.1.3.3.1.1.2.1.1.10.134') | ? {$_ -notlike '.*'} | select -f 1 | % {$_.toupper()} } catch { $name = $null }
}
'^zebra' {
# centimetri stampati
try { $pagecount = $snmp.Get('.1.3.6.1.4.1.10642.200.17.4.0').split(',')[-1].split(' ')[1].trim() } catch { $pagecount = $null }
if($pagecount -eq $null) {
try { $pagecount = $snmp.Get('.1.3.6.1.4.1.10642.3.1.1.0') } catch { $pagecount = $null }
}
$toner = $null
# NOME
try { $name = $snmp.Get('.1.3.6.1.4.1.10642.1.4.0').toupper() } catch { $name = $null }
# SERIALE
#try { $serial = $snmp.Get('.1.3.6.1.4.1.10642.200.19.5.0').toupper() } catch { $serial = $null } #ZM400
try { $serial = $snmp.Get('.1.3.6.1.4.1.10642.200.19.28.0').toupper() } catch { $serial = $null } #ZM400
if($serial -eq $null) {
try { $serial = $snmp.Get('.1.3.6.1.4.1.10642.1.9.0').toupper() } catch { $serial = $null } #ZD420
}
# MODELLO
$tmp = $model #non "Zebra Printer" ma "ZTC ZD420-300DPI ZPL"
try { $model = $snmp.Get('.1.3.6.1.4.1.10642.1.1.0').toupper() } catch { $model = $tmp }
$tmp = $sysdescr0 # non "Zebra Wired" ma MANUFACTURER:Zebra Technologies ;COMMAND SET:ZPL;MODEL:ZTC ZD420-300dpi ZPL;CLASS:PRINTER;OPTIONS:XML;
try { $sysdescr0 = $snmp.Get('.1.3.6.1.4.1.10642.1.3.0') } catch { $sysdescr0 = $tmp }
#try { $test = $snmp.Get('.1.3.6.1.4.1.10642.200.19.5.0').toupper() } catch { $test = $null }
#try { $test = $snmp.Get('.1.3.6.1.4.1.10642.1.9.0').toupper() } catch { $test = $null }
$test = "zebra"
}
'^lexmark' {
try { $name = $snmp.Get('.1.3.6.1.4.1.641.1.5.7.6.0').toupper() } catch { $name = $null }
}
'^ricoh' {
try { $name = $snmp.Get('.1.3.6.1.4.1.367.3.2.1.7.3.5.1.1.2.1.1').toupper() } catch { $name = $null }
}
'^hp' {
try { $name = $snmp.Get('.1.3.6.1.4.1.11.2.4.3.5.46.0').toupper() } catch { $name = $null }
$test = "hp"
}
'^TTP' {
# centimetri stampati
try { $pagecount = $snmp.Get('.1.3.6.1.4.1.51966.2.6.3.3311.1.0') } catch { $pagecount = $null }
$toner = $null
$test = "toshiba"
}
'^brother' {
try { $name = $snmp.Get('.1.3.6.1.2.1.43.5.1.1.17.1').toupper() } catch { $name = $null }
if($toner -eq 150){$toner=100}elseif($toner -eq 100){$toner=50}
$test = "brother"
}
'^samsung' {
try { $name = $snmp.Get('.1.3.6.1.2.1.1.5.0').toupper() } catch { $name = $null }
$test = "samsung"
}
default {
#'features', 'name' | % {Clear-Variable $_}
$features = $null
$name = $null
}
}
#if ($model -like 'SHARP*') {}
# ADDRESS
try { $addr = ($snmp.Gettree('.1.3.6.1.2.1.4.20.1.1') | ? {$_ -match '(?:[^\.]{1,3}\.){3}[^\.]{1,3}$' -and $_ -notmatch '127\.0\.0\.1'} | % {$ip = $_.split('.'); "$($ip[-4]).$($ip[-3]).$($ip[-2]).$($ip[-1])"}) -join ';' } catch { $addr = $null }
[pscustomobject]@{
dtime = [datetime]::Now.ToString('u')
Machine = $printer
IP = $printerip
serial = $serial
Name = $name
Model = $model
Comment = $comment
Color = $color
Trays = $trays
Features = $features
SystemDescription = $sysdescr0
Addresses = $addr
PageCount = $pagecount
TonerPCT = $toner
vendor=$test
}
}
$snmp.Close()
} else {
Write-Warning "Machine '$printer' may be offline."
}
}
}
}
# ZM400: .1.3.6.1.4.1.10642.200.17.6.0 = 254 lunghezza etichetta
# ZD:421: .1.3.6.1.4.1.10642.3.1.10.0 = 194 lunghezza etichetta
# ZD:421: .1.3.6.1.4.1.10642.3.1.6.0 = numero labels
#Get-SNMPPrinterInfo "10.104.64.91"
#Get-SNMPPrinterInfo "10.101.64.95"
# Get-SNMPPrinterInfo "10.101.64.91"
# Get-SNMPPrinterInfo "10.109.64.16" #toshiba
Get-SNMPPrinterInfo "10.102.64.21" #zebra ZM400 IT-office
#Get-SNMPPrinterInfo "10.102.64.14" #zebra ZD420
#Get-SNMPPrinterInfo "10.105.64.17" #zebra castagnole
#Get-SNMPPrinterInfo "10.101.64.11"
#Get-SNMPPrinterInfo "10.102.64.62" #samsung IT
#exit
$rtrange = 101..112
$iprange = 2..254
$dt = [datetime]::Now.ToString('yyyyMMdd-HHmm')
$csv = "debby_printers_snmp.$dt.csv"
$output = @()
# $csvObjects = import-csv debby_printers_snmp.csv
#
# foreach ($item in $csvObjects) {
#
# $output += $item
# }
Foreach ($rt in $rtrange)
{
Foreach ($ip in $iprange)
{
$printer = "10.$rt.64.$ip"
$output += Get-SNMPPrinterInfo $printer
}
}
$output | Export-Csv $csv -NoTypeInformation
@fproperzi
Copy link
Author

Best way to check OID:

  1. Do you want check 10.105.64.17 printer and dont know about OID and MIB?
  2. download SnmpWalk
  3. .\SnmpWalk.exe -r:10.105.64.17 -t:600 > snmp10.105.64.17.txt
  4. example of snmp10.105.64.17.txt file:

_SnmpWalk v1.01 - Copyright (C) 2009 SnmpSoft Company
[ More useful network tools on http://www.snmpsoft.com ]

OID=.1.3.6.1.2.1.1.1.0, Type=OctetString, Value=Zebra Technologies ZD421-203dpi / internal wired
OID=.1.3.6.1.2.1.1.2.0, Type=OID, Value=1.3.6.1.4.1.10642.1.1
OID=.1.3.6.1.2.1.1.3.0, Type=TimeTicks, Value=46 days, 19:53:10.51
OID=.1.3.6.1.2.1.1.4.0, Type=OctetString, Value=
OID=.1.3.6.1.2.1.1.5.0, Type=OctetString, Value=ZD421
OID=.1.3.6.1.2.1.1.6.0, Type=OctetString, Value=
OID=.1.3.6.1.2.1.1.7.0, Type=Integer, Value=72
OID=.1.3.6.1.2.1.2.1.0, Type=Integer, Value=2_
...
OID=.1.3.6.1.4.1.11.2.3.9.1.1.7.0, Type=OctetString, Value=MANUFACTURER:Zebra Technologies ;COMMAND SET:ZPL;MODEL:ZTC ZD421-203dpi ZPL;CLASS:PRINTER;OPTIONS:XML;
OID=.1.3.6.1.4.1.11.2.3.9.1.1.10.0, Type=Integer, Value=10
OID=.1.3.6.1.4.1.11.2.3.9.1.1.11.0, Type=Integer, Value=1
OID=.1.3.6.1.4.1.11.2.3.9.1.1.12.0, Type=Integer, Value=0
OID=.1.3.6.1.4.1.11.2.3.9.1.1.13.0, Type=OctetString, Value=
OID=.1.3.6.1.4.1.11.2.3.9.1.1.14.0, Type=Integer, Value=1
OID=.1.3.6.1.4.1.11.2.3.9.1.1.15.0, Type=Integer, Value=1
OID=.1.3.6.1.4.1.11.2.3.9.1.1.16.0, Type=OctetString, Value=Zebra Technologies ZD421-203dpi / internal wired

...
OID=.1.3.6.1.4.1.10642.1.1.0, Type=OctetString, Value=ZTC ZD421-203dpi ZPL
OID=.1.3.6.1.4.1.10642.1.2.0, Type=OctetString, Value=V93.21.07Z
OID=.1.3.6.1.4.1.10642.1.3.0, Type=OctetString, Value=MANUFACTURER:Zebra Technologies ;COMMAND SET:ZPL;MODEL:ZTC ZD421-203dpi ZPL;CLASS:PRINTER;OPTIONS:XML;
OID=.1.3.6.1.4.1.10642.1.4.0, Type=OctetString, Value=Z203037
OID=.1.3.6.1.4.1.10642.1.5.0, Type=OctetString, Value=
OID=.1.3.6.1.4.1.10642.1.6.0, Type=Integer, Value=4
OID=.1.3.6.1.4.1.10642.1.7.0, Type=OctetString, Value=N93_21_07P55716-v15
OID=.1.3.6.1.4.1.10642.1.8.0, Type=Integer, Value=2
OID=.1.3.6.1.4.1.10642.1.9.0, Type=OctetString, Value=D6J211701128
OID=.1.3.6.1.4.1.10642.1.10.0, Type=OctetString, Value=MANUFACTURER:Zebra Technologies ;COMMAND SET:ZPL;MODEL:ZTC ZD421-203dpi ZPL;CLASS:PRINTER;OPTIONS:XML;
OID=.1.3.6.1.4.1.10642.1.11.0, Type=OctetString, Value=Zebra Technologies
OID=.1.3.6.1.4.1.10642.1.12.0, Type=OctetString, Value=
OID=.1.3.6.1.4.1.10642.1.13.0, Type=OctetString, Value=
OID=.1.3.6.1.4.1.10642.1.14.0, Type=OctetString, Value=https://www.zebra.com/support.html
OID=.1.3.6.1.4.1.10642.1.15.0, Type=OctetString, Value=https://www.zebra.com
OID=.1.3.6.1.4.1.10642.1.16.0, Type=OctetString, Value=2/24/2021
OID=.1.3.6.1.4.1.10642.1.17.0, Type=OctetString, Value=ZD421-200dpi,V93.21.07Z,8,8176KB
OID=.1.3.6.1.4.1.10642.1.18.0, Type=OctetString, Value=6.3

@fproperzi
Copy link
Author

Info extracted by snmp from 10.105.64.17:

dtime : 2023-03-07 09:07:23Z
Machine : 10.105.64.17
IP : 10.105.64.17
serial : D6J211701128
Name : Z203037
Model : ZTC ZD421-203DPI ZPL
Comment :
Color : No
Trays :
Features :
SystemDescription : MANUFACTURER:Zebra Technologies ;COMMAND SET:ZPL;MODEL:ZTC ZD421-203dpi ZPL;CLASS:PRINTER;OPTIONS:XML;
Addresses : 10.105.64.17
PageCount : 367663 (for zebra = mm, not pages...)
TonerPCT :
vendor : zebra

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment