Archive for the 'Programming' Category
Cisco Syslog Parser – slides

Here are the slides from my PDX.pm talk this week. A link to the accompanying podcast will be along soon.

Other fun things we discussed at the meeting:
Cisco::Reconfig
trapgen
logger
logwatch

Thanks for the lively discussion!

[edit] podcast!

[edit] Clarification of two items from the podcast:
- multiline messages do indeed come in multiple packets. There is a message counter that increments for each message, so you could use the host name + message counter to match up multi-line messages. For what I’m doing, the important part is in that first line, so the payoff isn’t worth the investment.
- re hypens in the mnemonic field of the system message: I went back through and wasn’t able to find any examples of this, so I retract my statement. (I do have examples of system messages with hyphens in the facility field.)

Git is my hero

Last night, an incredible number of people showed up to Code-N-Splode to hear about Git.

I was not at all prepared for that number of people, but grateful that a couple experts were on hand to help – Sarah Sharp, Audrey Eschright and Michael Schwern (and others) all contributed to the discussion. Thanks for all the great questions! I’m looking forward to Sarah’s advanced tutorial next month.

Vim tidbits of the day – 9/22/08

:bd – closes the current buffer window

CTRL-F – page down; CTRL-B – page up; CTRL-D – put current line in the center of your screen

CTRL-R – Redo (plus COUNT for redoing a number of changes); ‘u‘ is for undo; ‘U‘ is for undoing all changes on a particular line

:set undolevels=NUM – number of changes saved in memory (can set to negative number if running out of memory

:split – split your current window into two; switch between windows with CTRL-W CTRL-W

!{cmd} – run a shell command, shows you the output and prompts you before returning to your current buffer; ‘:!‘ by itself runs the last external command; ‘:!!‘ repeats the last command; :silent !{cmd} eliminates the need to hit enter after the command is done; ‘:r !{cmd}‘ puts the output of $cmd into the current buffer.

Command-line trick to help your memory

I’m learning the PostgreSQL system catalog tables right now, and was wishing that I had some catalog flashcards.  I made my wish ‘aloud’ on IRC today, and @davidfetter managed to blow my mind with this simple command-line switch for psql: -E. Now, every time I use a command, the tool tells me what the underlying SQL query is. Great for jogging the memory, and I’ll be putting off making my catalog flashcards for a few more days. Now I wonder what other revealing command-line switches I’ve overlooked!

See screenshot:

Cisco::Reconfig

Poking around on CPAN a week or so ago, I stumbled across Cisco::Reconfig.

It looked pretty interesting, and turned out to be the work of David Muir Sharnoff. He’s responsible for Net::Netmask, which is one of my five favorites, so I had to check it out.

Sample router config (not all lines shown):

interface Loopback0
 ip address 10.254.254.1 255.255.255.255
!
interface Ethernet0/0
 description Admin LAN
 ip address 10.10.1.1 255.255.255.0
 duplex auto
!
interface Serial0/0
 description to Internet ID W065432
 ip address 1.1.1.1 255.255.255.252
!
interface Ethernet0/1
 no ip address
 shutdown
!
interface Ethernet1/0
 description User LAN
 ip address 10.10.2.1 255.255.254.0
 duplex auto
!
interface Ethernet1/1
 ip address 10.10.4.1 255.255.254.0
 duplex auto
!

Let’s write a quick & dirty script^W program to look for blank interface descriptions, because that’s something I find really annoying:

#!/usr/local/bin/perl

use strict;
use Cisco::Reconfig;

my $host        = "gabrielle";
my $host_config = "$host.confg";
print ("Checking: $host\n");
my $config  = readconfig("$host_config");

for my $intf ( $config->get('interface') ) {
    next if ( $intf->get('shutdown') );	#ignore, don't care
    next if ( $intf =~ /Loopback/ );	#idc

    my $descr   = $intf->get('description');
    chomp ($intf, $descr);  	# hm, kinda feel like I shouldn't have to do this?
    print ("$intf: $descr\n");	# just to be chatty
    if (! $descr) {
        print ("$host: $intf: Description is blank!\n");
    }
}

exit 0;

:::–>./sample.pl
Checking: gabrielle
interface Ethernet0/0: description Admin LAN
interface Serial0/0: description to Internet ID W065432
interface Ethernet1/0: description User LAN
interface Ethernet1/1:
gabrielle: interface Ethernet1/1: Description is blank!

I’m primarily interested in it for checking compliance of our configurations with our business standards. No need to chunk through a config line-by-line with regexps; in a lot of cases, a simple ‘get’ will tell me if something’s configured or not (eg “snmp server-location”.)

Specific features that look really intriguing:
- you can generate the commands you need to “fix” your config
- the “context” method allows you to draw out the surrounding lines

I can’t wait to mess around with this some more.

Survey of Perl Modules I Can’t Live Without

Specifically for the management of IP networks & equipment – routers, switches, etc

1. Connection automation:

Because it’s really handy to update, say, the passwords on all 1000 of your network devices in a couple of hours by kicking off a single script. It’s faster and more reliable. Of course, if you fat-finger the password in the script, you’ve just fat-fingered it on your entire network, so please test it first.

Net::Telnet::Cisco – this is really cool, but doesn’t work on older equipment, due to Cisco’s lack of a standardized interface. So, we roll our own with Expect.pm.

—–
2. IP Addressing:

Socket.pm of course, though not being a C programmer I have a hard time remembering the syntax off the top of my head so I keep a couple of examples around.

my $ip = $ARGV[0];
my @addr = split(/\./, $ip);
my $addr = pack(‘C4′, @addr);
my $name = gethostbyaddr($addr, AF_INET);
print (“Name: $name\n”);

my $host = $ARGV[0];
my $ip = scalar gethostbyname($host);
my @ip = unpack(“C4″,$ip);
my $ip = join(“.”,@ip);
print (“IP: $ip\n”);
—–
3. Subnetting:

Net::Netmask, baby! For figuring out network & bcast addresses, accept no substitutes.

my $ip = “10.1.1.1″;
my $netmask = “255.255.255.0″;
my $block = new Net::Netmask($ip, $netmask);
my $bcast_addr = $block->broadcast();
print (“Broadcast address: $bcast_addr\n”);

—–
4. SNMP. Y’all knew I would have to talk about this.

Several options, two I have actual experience with.

A lot of people use Net::SNMP I use SNMP.pm (which comes with Net-SNMP – confused?* :) ) It’s probably the most complex, but also (to me) the most useful. I can get pretty much any data I want with this module.

SNMP-Simple – much more user-friendly than SNMP.pm. Lighter, faster, but you can only get *values* back from this, you can not get the OIDs, so it requires some pre-knowledge of what you want to monitor, which isn’t always possible.

—–
5. RRD[ps].pm – perl module for Tobi Oetiker’s RRDTool. Many network performance tools are based on RRDTool: mrtg, cacti, orca, NMIS. It’s indispensable if you’re going to write your own monitoring app, or tinker with one of the aforementioned tools.

—–
6. Date::Format and Date::Manip. I use Date::Format because it’s so easy to create timestamps for log files & reports – it follows the Unix strftime format. Date::Manip is huge and slow, but it’s the only thing I found that could handle some complex user time reporting I was doing a while back, and I love it.

—–
7. Net::MAC. I haven’t had time to experiment with this to the extent I’d like, but I have a feeling it’s going to be as useful as Net::Netmask. I’ll let you know in a couple of weeks.

*One of my favorite jokes, just for me.