3 ms·
Perl 5, although I try to write new stuff in python now to help the rest of the team. Still write the occasional webpage in perl -- it ususaly starts off as a o
by iso1210 6y ago
Perl 5, although I try to write new stuff in python now to help the rest of the team. Still write the occasional webpage in perl -- it ususaly starts off as a one liner with grep, sed, etc, but I refactor into perl if it gets complex, been burnt too many times with simple bash scripts becomes more complicated as times goes on
Wrote a poller last year for a bunch of baudion devices to tell me the current configuration of them, outputting as a csv (for version control) or webpage (for nice formatting).
It's basically configure an array (qw//) of IPs and snmp strings, run some snmpwalks (not gets as the oids can change depending) - if it fails try the second IP, pull out the useful data, mangle it a bit, then output
code like
my $locM = `snmpwalk -r 1 -t 1 -OQnv -v2c -c $snmp $mainip sysName`;
my $locR = `snmpwalk -r 1 -t 1 -OQnv -v2c -c $snmp $resip sysName`;
and
if ($ver =~ /SR1/) {
$eth0 = `snmpwalk -r 1 -t 1 -OQnv -v2c -c $snmp $mainip .1.3.6.1.4.1.22425.10.5.1.2.0`;
$eth1 = `snmpwalk -r 1 -t 1 -OQnv -v2c -c $snmp $mainip .1.3.6.1.4.1.22425.10.5.1.3.0`;
} else {
$eth0 = `snmpwalk -r 1 -t 1 -OQnv -v2c -c $snmp $mainip .1.3.6.1.4.1.22425.10.5.1.5.1.3.0`;
$eth1 = `snmpwalk -r 1 -t 1 -OQnv -v2c -c $snmp $mainip .1.3.6.1.4.1.22425.10.5.1.5.1.3.1`;
}
and
my $cmd = `snmpwalk -r 1 -t 1 -OQnv -v2c -c $snmp $mainip 1.3.6.1.4.1.22425.10.5.3.5.1.$oid_rxip`;
foreach (split(/\n/, $cmd)) {
next unless (/.../);
next if (/0.0.0.0/);
s/[" ]*//g;
$srcs->{$_} = 1;
}
and
if ($mainip eq $resip) { $alert .= "NO SECOND IP"; }
if ($locM ne $locR) { $alert .= "Sysname Mismatch"; }
It does the job, it doesn't rely on external language-specific libraries like Net::SNMP which complicates portability. There no doubt far nicer ways to do it, but my process involves
* Prod device to get original information using things like nmap, curl, snmpwalk, etc
* Mangle retrieved information to infer extra information
* Present information
Perl allows me to get from my prodding to useful output in minimal time, going from initial results from prodduing, to functioning output, in well under an hour.
Python could probably do something similar, but things like
/([0-9]*)kbit.*([0-9]*)kbit/;
my $inBit = 1000*$1; my $outBit = 1000*$2;
Is lovely and quick to type up for me compared with something like
inBit = 0
outBit = 0
matches = re.match(".*([0-9]*)kbit.*([0-9]*)kbit/, line);
if (matches):
inBit = 1000*(int(matches.groups()[0]))
outBit = 1000*(int(matches.groups()[1]))
Ultimatly I don't care about elegant resuable code, I care about the results (a page telling me the bitrate on the stream, or that a destination IP has been changed or whatever)