Abfrage von SNMP-Daten mit snmpget und snmpwalk
Das Simple Network Management Protocol (SNMP) ist ein Netzwerkprotokoll zur Überwachung und Steuerung von Netzwerkgeräten wie Router, Switches, Firewalls, Drucker oder NAS Appliances. In diesem Wikiartikel wird erläutert, wie Sie mit den bekannten Tools snmpget und snmpwalk auf SNMP v1, SNMP v2c und SNMP v3 fähige Geräte zugreifen, des weiteren wie Sie mit Hinzufügen einer MIB die Lesbarkeit der Rückgabewerte erhöhen.
Net-SNMP Package
Die beiden Tools sind Teil des Net-SNMP Packages, es kann unter Ubuntu bequem per apt installiert werden.
sudo apt install snmp
snmpget
Das snmpget Kommando liefert den Wert bzw. Datensatz zu einer spezifischen OID aus.
Beispielabfragen
Nachfolgend werden drei Ausgaben von snmpget durchgeführt (SNMP v1, v2c und v3), hier z.B. mit der Angabe der OID, das einen Wert rückliefert, ob ein Update der Betriebssoftware vorhanden ist.
Abfrage per SNMP v1
$ snmpget -v1 -c <Community-String> <IP-Adresse> 1.3.6.1.4.1.6574.1.5.4.0
Abfrage per SNMP v2c
$ snmpget -v2c -c <Community-String> <IP-Adresse> 1.3.6.1.4.1.6574.1.5.4.0
Abfrage per SNMP v3 und -l authNoPriv
- Synology Geräte unterstützen maximal die Authentifizierung per Usernamen und Passwort, keine Übertragungsverschlüsselung.
$ snmpget -v3 -l authNoPriv -u <SNMP-User> -a MD5 -A <SNMP-Passwort> <IP-Adresse> 1.3.6.1.4.1.6574.1.5.4.0
Ausgabe aller drei Abfragen
iso.3.6.1.4.1.6574.1.5.4.0 = INTEGER: 2
Weitere Beispielabfragen
Nachfolgende Beispiele ergänzen die Möglichkeiten, die SNMP v3 bietet.
Abfrage per SNMP v3 und -l noAuthNoPriv
- Falls ein SNMP-Gerät nur diese Methode unterstützt, in diesem Fall wird nur ein gültiger Benutzername abgefragt.
$ snmpget -v3 -l noAuthNoPriv -u <SNMP-User> <IP-Adresse> <OID>
Abfrage per SNMP v3 und -l authPriv
- Hier wird eine Benutzerauthentifizierung per Passwort und eine Verschlüsselung der Übertragung vorgenommen.
$ snmpget -v3 -l authPriv -u <SNMP-User> -a <MD5|SHA> -A <SNMP-Passwort> -x <AES|DES> -X <privKey> <IP-Adresse> <OID>
Hilfeausgabe
Die Hilfefunktion von snmpget zeigt ausführlich die verfügbaren Parameter und Informationen dazu auf.[1]
$ snmpget -h USAGE: snmpget [OPTIONS] AGENT OID [OID]... Version: 5.7.2 Web: http://www.net-snmp.org/ Email: net-snmp-coders@lists.sourceforge.net OPTIONS: -h, --help display this help message -H display configuration file directives understood -v 1|2c|3 specifies SNMP version to use -V, --version display package version number SNMP Version 1 or 2c specific -c COMMUNITY set the community string SNMP Version 3 specific -a PROTOCOL set authentication protocol (MD5|SHA) -A PASSPHRASE set authentication protocol pass phrase -e ENGINE-ID set security engine ID (e.g. 800000020109840301) -E ENGINE-ID set context engine ID (e.g. 800000020109840301) -l LEVEL set security level (noAuthNoPriv|authNoPriv|authPriv) -n CONTEXT set context name (e.g. bridge1) -u USER-NAME set security name (e.g. bert) -x PROTOCOL set privacy protocol (DES|AES) -X PASSPHRASE set privacy protocol pass phrase -Z BOOTS,TIME set destination engine boots/time General communication options -r RETRIES set the number of retries -t TIMEOUT set the request timeout (in seconds) Debugging -d dump input/output packets in hexadecimal -D[TOKEN[,...]] turn on debugging output for the specified TOKENs (ALL gives extremely verbose debugging output) General options -m MIB[:...] load given list of MIBs (ALL loads everything) -M DIR[:...] look in given list of directories for MIBs (default: $HOME/.snmp/mibs:/usr/share/snmp/mibs:/usr/share/snmp/mibs/iana:/usr/share/snmp/mibs/ietf:/usr/share/mibs/site:/usr/share/snmp/mibs:/usr/share/mibs/iana:/usr/share/mibs/ietf:/usr/share/mibs/netsnmp) -P MIBOPTS Toggle various defaults controlling MIB parsing: u: allow the use of underlines in MIB symbols c: disallow the use of "--" to terminate comments d: save the DESCRIPTIONs of the MIB objects e: disable errors when MIB symbols conflict w: enable warnings when MIB symbols conflict W: enable detailed warnings when MIB symbols conflict R: replace MIB symbols from latest module -O OUTOPTS Toggle various defaults controlling output display: 0: print leading 0 for single-digit hex characters a: print all strings in ascii format b: do not break OID indexes down e: print enums numerically E: escape quotes in string indices f: print full OIDs on output n: print OIDs numerically q: quick print for easier parsing Q: quick print with equal-signs s: print only last symbolic element of OID S: print MIB module-id plus last element t: print timeticks unparsed as numeric integers T: print human-readable text along with hex strings u: print OIDs using UCD-style prefix suppression U: don't print units v: print values only (not OID = value) x: print all strings in hex format X: extended index format -I INOPTS Toggle various defaults controlling input parsing: b: do best/regex matching to find a MIB node h: don't apply DISPLAY-HINTs r: do not check values for range/type legality R: do random access to OID labels u: top-level OIDs must have '.' prefix (UCD-style) s SUFFIX: Append all textual OIDs with SUFFIX before parsing S PREFIX: Prepend all textual OIDs with PREFIX before parsing -L LOGOPTS Toggle various defaults controlling logging: e: log to standard error o: log to standard output n: don't log at all f file: log to the specified file s facility: log to syslog (via the specified facility) (variants) [EON] pri: log to standard error, output or /dev/null for level 'pri' and above [EON] p1-p2: log to standard error, output or /dev/null for levels 'p1' to 'p2' [FS] pri token: log to file/syslog for level 'pri' and above [FS] p1-p2 token: log to file/syslog for levels 'p1' to 'p2' -C APPOPTS Set various application specific behaviours: f: do not fix errors and retry the request
snmpwalk
Eine Abfrage (und Ausgabe) von kompletten MIBs kann mit snmpwalk durchgeführt werden. Mit snmpwalk können somit einfach Tabellen und Unterknoten eines OID-Knotens durchgesucht werden.
Beispielabfragen
Dieses Beispiel verwendet zur Abfrage der Werte ebenso ein Synology NAS Gerät. Die hier definierte OID ist aus der MIB SYNOLOGY-SYSTEM-MIB.txt entnommen, sie listet sämtliche Systeminformationen auf.
Abfrage per SNMP v1
$ snmpwalk -v1 -c <Community-String> <IP-Adresse> 1.3.6.1.4.1.6574.1.5.4
Abfrage per SNMP v2c
$ snmpwalk -v2c -c <Community-String> <IP-Adresse> 1.3.6.1.4.1.6574.1.5.4
Abfrage per SNMP v3 und -l authPriv
snmpwalk -v3 -l authNoPriv -u <SNMP-User> -a MD5 -A <SNMP-Passwort> <IP-Adresse> 1.3.6.1.4.1.6574.2.1.1.5
Ausgabe aller drei Abfragen
iso.3.6.1.4.1.6574.1.1.0 = INTEGER: 1 iso.3.6.1.4.1.6574.1.2.0 = INTEGER: 45 iso.3.6.1.4.1.6574.1.3.0 = INTEGER: 1 iso.3.6.1.4.1.6574.1.4.1.0 = INTEGER: 1 iso.3.6.1.4.1.6574.1.4.2.0 = INTEGER: 1 iso.3.6.1.4.1.6574.1.5.1.0 = STRING: "DS1515+" iso.3.6.1.4.1.6574.1.5.2.0 = STRING: "<SERIENNUMMER>" iso.3.6.1.4.1.6574.1.5.3.0 = STRING: "DSM 6.1-15047" iso.3.6.1.4.1.6574.1.5.4.0 = INTEGER: 2
Zum Vergleich ein Aufruf per snmpget
Mit dieser OID resultiert der Aufruf einen Fehler, da kein Objekt existiert. Snmpwalk läuft in diesem Fall alle Unterknoten durch.
$ snmpget -v2c -c <Community-String> <IP-Adresse> 1.3.6.1.4.1.6574.1 iso.3.6.1.4.1.6574.1 = No Such Object available on this agent at this OID
Weitere Beispielabfragen
Nachfolgende Beispiele ergänzen die Möglichkeiten, die SNMP v3 bietet.
Abfrage per SNMP v3 und -l noAuthNoPriv
- Falls ein SNMP-Gerät nur diese Methode unterstützt, in diesem Fall wird nur ein gültiger Benutzername abgefragt.
snmpwalk -v3 -l noAuthNoPriv -u <SNMP-User> <IP-Adresse> <OID>
Abfrage per SNMP v3 und -l authPriv
- Hier wird eine Benutzerauthentifizierung per Passwort und eine Verschlüsselung der Übertragung vorgenommen.
snmpwalk -v3 -l authPriv -u <SNMP-User> -a <MD5|SHA> -A <SNMP-Passwort> -x <AES|DES> -X <privKey> <IP-Adresse> <OID>
Hilfeausgabe
Die Hilfefunktion von snmpwalk zeigt ausführlich die verfügbaren Parameter und Informationen dazu auf.[2]
$ snmpwalk -h USAGE: snmpwalk [OPTIONS] AGENT [OID] Version: 5.7.2 Web: http://www.net-snmp.org/ Email: net-snmp-coders@lists.sourceforge.net OPTIONS: -h, --help display this help message -H display configuration file directives understood -v 1|2c|3 specifies SNMP version to use -V, --version display package version number SNMP Version 1 or 2c specific -c COMMUNITY set the community string SNMP Version 3 specific -a PROTOCOL set authentication protocol (MD5|SHA) -A PASSPHRASE set authentication protocol pass phrase -e ENGINE-ID set security engine ID (e.g. 800000020109840301) -E ENGINE-ID set context engine ID (e.g. 800000020109840301) -l LEVEL set security level (noAuthNoPriv|authNoPriv|authPriv) -n CONTEXT set context name (e.g. bridge1) -u USER-NAME set security name (e.g. bert) -x PROTOCOL set privacy protocol (DES|AES) -X PASSPHRASE set privacy protocol pass phrase -Z BOOTS,TIME set destination engine boots/time General communication options -r RETRIES set the number of retries -t TIMEOUT set the request timeout (in seconds) Debugging -d dump input/output packets in hexadecimal -D[TOKEN[,...]] turn on debugging output for the specified TOKENs (ALL gives extremely verbose debugging output) General options -m MIB[:...] load given list of MIBs (ALL loads everything) -M DIR[:...] look in given list of directories for MIBs (default: $HOME/.snmp/mibs:/usr/share/snmp/mibs:/usr/share/snmp/mibs/iana:/usr/share/snmp/mibs/ietf:/usr/share/mibs/site:/usr/share/snmp/mibs:/usr/share/mibs/iana:/usr/share/mibs/ietf:/usr/share/mibs/netsnmp) -P MIBOPTS Toggle various defaults controlling MIB parsing: u: allow the use of underlines in MIB symbols c: disallow the use of "--" to terminate comments d: save the DESCRIPTIONs of the MIB objects e: disable errors when MIB symbols conflict w: enable warnings when MIB symbols conflict W: enable detailed warnings when MIB symbols conflict R: replace MIB symbols from latest module -O OUTOPTS Toggle various defaults controlling output display: 0: print leading 0 for single-digit hex characters a: print all strings in ascii format b: do not break OID indexes down e: print enums numerically E: escape quotes in string indices f: print full OIDs on output n: print OIDs numerically q: quick print for easier parsing Q: quick print with equal-signs s: print only last symbolic element of OID S: print MIB module-id plus last element t: print timeticks unparsed as numeric integers T: print human-readable text along with hex strings u: print OIDs using UCD-style prefix suppression U: don't print units v: print values only (not OID = value) x: print all strings in hex format X: extended index format -I INOPTS Toggle various defaults controlling input parsing: b: do best/regex matching to find a MIB node h: don't apply DISPLAY-HINTs r: do not check values for range/type legality R: do random access to OID labels u: top-level OIDs must have '.' prefix (UCD-style) s SUFFIX: Append all textual OIDs with SUFFIX before parsing S PREFIX: Prepend all textual OIDs with PREFIX before parsing -L LOGOPTS Toggle various defaults controlling logging: e: log to standard error o: log to standard output n: don't log at all f file: log to the specified file s facility: log to syslog (via the specified facility) (variants) [EON] pri: log to standard error, output or /dev/null for level 'pri' and above [EON] p1-p2: log to standard error, output or /dev/null for levels 'p1' to 'p2' [FS] pri token: log to file/syslog for level 'pri' and above [FS] p1-p2 token: log to file/syslog for levels 'p1' to 'p2' -C APPOPTS Set various application specific behaviours: p: print the number of variables found i: include given OID in the search range I: don't include the given OID, even if no results are returned c: do not check returned OIDs are increasing t: Display wall-clock time to complete the walk T: Display wall-clock time to complete each request E {OID}: End the walk at the specified OID
Laden einer MIB
Für eine bessere Interpretation eines Rückgabewertes eines SNMP Agenten, kann auf dem Client-Rechner, der als SNMP Manager fungiert, die erforderliche MIB geladen werden.
Installieren des MIBs Downloader
Mit Installation des Paketes snmp-mibs-downloader können MIBs heruntergeladen und gemanagt werden.[3]
sudo apt install snmp-mibs-downloader
Kopieren der MIB in das Zielverzeichnis
Dazu kopieren Sie zum Beispiel die Datei SYNOLOGY-DISK-MIB.txt in das Verzeichnis /usr/share/snmp/mibs auf dem Client-Rechner (bei Ubuntu/Debian Linux).
Setzen einer Environment-Variable
Die folgende Environment-Variable gibt hierbei den lokalen Tools (wie snmpget und snmpwalk) diese MIB bekannt:
export MIBS=+SYNOLOGY-SYSTEM-MIB
Ausgabe des snmpget Kommandos
Nachfolgend wird das snmpget Kommando einmal ohne geladener und einmal mit geladener MIB aufgerufen, der Unterschied der Ausgabe ist sehr deutlich:
$ snmpget -v2c -c <Community-String> <IP-Adresse> 1.3.6.1.4.1.6574.1.5.4.0 iso.3.6.1.4.1.6574.1.5.4.0 = INTEGER: 2 $ export MIBS=+SYNOLOGY-SYSTEM-MIB $ snmpget -v2c -c <Community-String> <IP-Adresse> 1.3.6.1.4.1.6574.1.5.4.0 SYNOLOGY-SYSTEM-MIB::upgradeAvailable.0 = INTEGER: 2
Permanentes Setzen der Environment-Variable
Das folgende Kommando setzt diese Einstellung für den aktuellen Benutzer permanent:
$ echo "mibs +MY-MIB" >> $HOME/.snmp/snmp.conf
Einzelnachweise
- ↑ snmpget(1) - Linux man page (linux.die.net)
- ↑ snmpwalk(1) - Linux man page (linux.die.net)
- ↑ Package: snmp-mibs-downloader (1.1) [multiverse] (packages.ubuntu.com
Autor: Thomas Niedermeier Thomas Niedermeier arbeitet im Product Management Team von Thomas-Krenn. Er absolvierte an der Hochschule Deggendorf sein Studium zum Bachelor Wirtschaftsinformatik. Seit 2013 ist Thomas bei Thomas-Krenn beschäftigt und kümmert sich unter anderem um OPNsense Firewalls, das Thomas-Krenn-Wiki und Firmware Sicherheitsupdates. |