Sunday, May 26, 2013

It's Easy, Like Playing with LEGO

You should be able to pipe commands together like kids play with LEGO.

UNIX/Linux is insanely powerful for text-file manipulation.  Know how to leverage grep's -A -B and -v options.  Know how to leverage awk's -F option.  Heck, know how to leverage awk - associative arrays are ultra-powerful.

Here's an example.  A customer listed part of the output from "ifconfig -a" - showing about 50-60 ip addresses he needed taken down en masse.  As fast as possible, as they were causing conflicts since they were defined on another server as well.

I was able to copy/paste that output into a file, pull out just the IP addresses, and put that list into another file.

While reconfirming the ip address list with the customer, I wrote two other programs - one to "ifdown" the interfaces, one to remove the associated file for each interface so it wouldn't get re-enabled if/when the server rebooted.

Let me clarify - I used two different one-liners to generate files full of shell statements that did exactly what I wanted:

[...]
ifdown eth0:134
ifdown eth0:135
ifdown eth0:136
[...]

and:

[...]
rm /etc/sysconfig/network-scripts/ifcfg-eth0:134
rm /etc/sysconfig/network-scripts/ifcfg-eth0:135
rm /etc/sysconfig/network-scripts/ifcfg-eth0:136
[...]

Neither of these final steps used the IP address but rather the interface name.  For this, grep's "-l" option came to the rescue; it gave me the name of the interface-specification-file that defined each particular ip address.

awk -F- '{print $3}' on those filenames above gives you interface names, doesn't it?  So... that's how I got from IP addresses to filenames to interface names.

I was able to create scripts that did the work FOR me, definitively, 100% sure I was pulling down ONLY the interfaces the customer had asked for, nothing more, nothing less.

Like I said... LEGO.

No comments:

Post a Comment