Command Line Wizardry 2: Example.

So, after all that stuff I wrote a few minutes ago, I thought a real life example might be helpful.  So here is something I wrote with a brief explanation.

#!/usr/local/bin/bash
grep 12.129.157.67 namedb/* | awk '{print $1}' | awk -F / '{print $2}' | awk -F ":" '{print $1}' | uniq > sites/w1.sites
grep 12.129.157.68 namedb/* | awk '{print $1}' | awk -F / '{print $2}' | awk -F ":" '{print $1}' | uniq > sites/w2.sites
grep 12.129.157.69 namedb/* | awk '{print $1}' | awk -F / '{print $2}' | awk -F ":" '{print $1}' | uniq > sites/w3.sites
grep 12.129.157.70 namedb/* | awk '{print $1}' | awk -F / '{print $2}' | awk -F ":" '{print $1}' | uniq > sites/w4.sites
grep 12.129.157.71 namedb/* | awk '{print $1}' | awk -F / '{print $2}' | awk -F ":" '{print $1}' | uniq > sites/w5.sites
grep 12.129.157.72 namedb/* | awk '{print $1}' | awk -F / '{print $2}' | awk -F ":" '{print $1}' | uniq > sites/w6.sites

grep 12.129.157.75 namedb/* | awk '{print $1}' | awk -F / '{print $2}' | awk -F ":" '{print $1}' | uniq > sites/loadbalanced.sites

What is all of this?  Well, say you own a lot of domain names and websites and they are spread across numerous servers?  What this does is checks the DNS entries and outputs the domain.com entry for each one based on IP address.  So lets break one of these down.

grep 12.129.157.67 namedb/* | awk '{print $1}' | awk -F / '{print $2}' | awk -F ":" '{print $1}' | uniq > sites/w1.sites

First is grep.  It searches all of the files in namedb/ for any lines matching 12.129.157.69 and outputs those lines to the console.  Except, that there is a pipe so all of the output is fed into awk which simply prints out the first column of data. Which would look like this:

namedb/domain.com:www

I don’t care about that namedb part, so that output is piped into another awk comand.  The -F says to basically separate columns of data on the / character that I specified.  So then $1 is namedb and $2 is domain.com:www.  So that awk command prints $2 and gives this:

domain.com:www

I don’t care about the www so the output is piped into a final awk command which uses -F : to say to split things up on the :.  So if I print $1 that will give me:

domain.com

Finally!  The output I’ve been looking for.  Now I know that domain.com resides on whichever IP address.  Since there are lots of domains and subdomains possible like www.domain.com or mail.domain.com, and I only want to know the base domain I pipe all of that input to uniq.  Which only displays unique entries.  So if before the uniq I had 30 domain.com’s listed, uniq will only give me one.

Then, I have > sites/w1.sites.  That says to direct the output to the sites/w1.sites file.  So I don’t get output, it is written to that file.  So all I have to do is run that script, and I suddenly know where all of my domains reside.

February 12th, 2010, posted by rl

Command Line Wizardry 2.

Today, I thought I might just write about some general commands that help navigate files safely.

Now, the commands in the arsenal of what we’ll be using are:cat, more, grep, awk, sed, and vim

Read the rest of this entry »

February 12th, 2010, posted by rl

Command-Line Wizardry

So with the System Administration work I do now I write a lot of convoluted commands. I think there is a kind of poetry involved with it. So here are some commands I’ve used recently and a synopsis of their purpose

grep -liR --exclude='cow' foo /bar/* >> foobar.txt  &

Grep searches the content of files to match a pattern. liR tells it to only output the filenames that match, ignore the case of the pattern to match, and to search recursively through files and directories. The exclude pattern does that, anything that is ‘cow’ is ignored. Finally the output is appended to the foobar.txt file. That little & at the end tells it to run in the background. Helpful if you have more work to do.

Next is this:
rsync -avvze 'ssh' --stats --exclude-from='foobar.txt' foo.bar:/foo/ /foo/

Now for those who don’t know rsync. It is an amazing program. It’s purpose is to perform network file copies intelligently. Specifically it will only move the differences between files. It can do nearly anything under the sun related to that task.

This command in particular is going to copy everything from the remote server foo.bar to the local directory on the computer we run the command from. Now, along the way it will compare files between the two and only move ones that are different. The –excludes-from will read a file of things to ignore in this process. The -avvze ’ssh’ does a couple of things. The two v’s increase the output to level 2, the e tells rsync to tunnel everthing through ssh, the z compresses all the data before it sends it, and the a is a shortcut for a lot of other options that make it really great for making backups.

Now, the reason I put those two commands together is simple.  You can use the grep command to find files that you don’t want to move, lets say they have sensitive login information.  Those files you don’t want rsynced are listed in a text file that you can use on the rsync command so that they will be ignored.  So rsync will copy everything but those files, so the sensitive information is preserved.

Clear as mud, right?

February 4th, 2010, posted by rl

Obsession.

Alright, I’ll say it. I have a man crush. Though I also need to clarify that the gender isn’t really relevant other than to make people raise an eyebrow.

Now then.

What I’m talking about, or rather, who I am talking about is Jacques Pepin. I’m sure I’ve written about him before, but he is the closest famous person (Depending on how you define famous) that I have ever had an obsession with.

For those of you who don’t know, he is probably the greatest chef that is in the public eye. The reason you probably don’t know him is that he has only ever been on PBS and not on the Food Network.

Anyway, he did a show a few years ago with Julia which is where most people have seen him, but he’s been around much longer. He was the main chef for 3 heads of state in France, helped out in a few restaurants and is a freaking wizard.

Seriously, watch him make an omelette and tell me you could ever do better.

I bought his complete book of techniques which is fantastic. It shows you how to hold, use, and maintain your knives, how to chop, julienne, and dice, how to make puff pastry, sugar baskets, and chocolate sculptures.

So far I am working on the basic bread recipes and techniques and roasting and carving chickens. I have gotten pretty good at the whole process, and that makes me feel good.

I had some friends and my family over to have my chicken. It was a new experience because I cooked 2. I figured that since there were 6 of us, one 3 lb chicken wouldn’t cut it. I was kind of wrong. Some how, one of the chickens was barely touched.

Nevertheless, I trussed the two chickens, roasted and basted them, and then carved them the way Jacques recommends. My mom told me she woke up in the middle of the night wanting more. I don’t know if I’ve had such a good compliment.

Anyway, if you have any interest in cooking, check him out. He is worth obsessing over.

February 1st, 2010, posted by rl

Owari.

Well, this will be the last post from my blog that appears on Facebook (Unless I screw up anyway). I’m happier with the feeling that I can write what I want in two distinct places instead of my bad language permeating to everyone I have ever met on Facebook.

This may or may not have anything to do with the alarming amount of older relatives that are showing up.

January 27th, 2010, posted by rl

  • Weather


    Click for Forecast
  • Archives

  • Categories

  • flickr photos

    Teijin Rachel's Wedding Hiroshima(Nikon) Koh Chang Teijin Koh Chang Koh Chang Bangkok Rachel's Wedding Hiroshima(Nikon)
  • Meta