I’ve been using Unix/Linux in some form or fashion since 1997. I remember my first foray into shell scripting was during my time at a title company that provided these merged credit reports via these little customized Okidata printers. When credit pull requests would come in from customers we’d have to look at the ones that got stuck, make some edits, and re-submit them to the system for processing. What I realized was that the steps were more or less the same every time with a couple exceptions. This begged to be automated.
My first go at it was writing a short shell script that ran the 5-8 commands in order so I didn’t have to type them myself each time. Then I discovered that I could search through the contents of the file with grep and return the number of matched lines. If it was >0, I would do process X, else Y.
After discovering grep I had to know what else was out there. Once I learned how to get manuals (‘man pages’ for short) it was all over. I was reading the man page for every single program in the /usr/bin and /bin folders. Cut for selecting columns, paste for putting them back together. Use wc for getting the words/lines/characters in files. Check out ps for listing processes, ls for listing files, mkdir, rmdir, rm, etc.
Fast forward to now…
I have to get some information from the server where I’m writing some code. The fastest way to get it is by querying the server via SNMP (a monitoring protocol that uses object identifiers as addresses for various things like the number of interfaces, the CPU load, the memory consumption, etc.). So I run the command I’m familiar with:
snmpwalk -v2c -c<communitystring> localhost 18.104.22.168.22.214.171.124.3.1
I get output like this:
iso.126.96.36.199.188.8.131.52.1.2.41 = OID: iso.184.108.40.206.220.127.116.11.4
iso.18.104.22.168.22.214.171.124.1.2.42 = OID: iso.126.96.36.199.188.8.131.52.4
iso.184.108.40.206.220.127.116.11.1.2.43 = OID: iso.18.104.22.168.22.214.171.124.4
iso.126.96.36.199.188.8.131.52.1.2.44 = OID: iso.184.108.40.206.220.127.116.11.4
iso.18.104.22.168.22.214.171.124.1.3.1 = STRING: "Physical memory"
iso.126.96.36.199.188.8.131.52.1.3.3 = STRING: "Virtual memory"
iso.184.108.40.206.220.127.116.11.1.3.6 = STRING: "Memory buffers"
iso.18.104.22.168.22.214.171.124.1.3.7 = STRING: "Cached memory"
iso.126.96.36.199.188.8.131.52.1.3.8 = STRING: "Shared memory"
iso.184.108.40.206.220.127.116.11.1.3.10 = STRING: "Swap space"
iso.18.104.22.168.22.214.171.124.1.3.31 = STRING: "/"
iso.126.96.36.199.188.8.131.52.1.3.35 = STRING: "/sys/fs/cgroup"
iso.184.108.40.206.220.127.116.11.1.3.41 = STRING: "/run"
iso.18.104.22.168.22.214.171.124.1.3.42 = STRING: "/run/lock"
iso.126.96.36.199.188.8.131.52.1.3.43 = STRING: "/run/shm"
iso.184.108.40.206.220.127.116.11.1.3.44 = STRING: "/run/user"
iso.18.104.22.168.22.214.171.124.1.4.1 = INTEGER: 1024
iso.126.96.36.199.188.8.131.52.1.4.3 = INTEGER: 1024
iso.184.108.40.206.220.127.116.11.1.4.6 = INTEGER: 1024
Okay, fine. I really only need the stuff on the far right. So I open the man page for snmpwalk and flip through the options. Nothing mentions any kind of adjustment to the output, only community strings, authentication, and the like. Hrmph. So I slice up the output and get what I need.
I go to sleep that night and of course if I’m writing any code before I sleep I dream about it. And I’m convinced that there HAS to be some way to adjust what’s coming out of that thing. So when I wake up I go back and read the man page more carefully. Evidently (because snmpget, snmpwalk, snmptable, and on and on) use common options, there’s a separate man page called snmpcmd with those details. I pop that open and lo and behold there are the options -Oq and -Ov. Adding -Oq removes the data type (OID/STRING/INTEGER) and -Ov removes the OID (iso.3.6.1…) leaving just the bit I cared about: the value.
TL;DR: If you’re using a command that’s part of a suite make sure you check for a “common options” man page.