Tag: command line
What domains are awaiting mail from my server?
by someguy on Jun.10, 2008, under Uncategorized
Here’s a less than elegant solution to get a summary of the number of emails per domain that have mail sitting in your postfix queue.
#!/bin/sh
postqueue -p|egrep '^([[:space:]])+([[:alnum:]]|.|-|_)+@([[:alnum:]]|-)+.([[:alnum:]]+|.|-)*([[:alpha:]]){2,4}$’|awk ‘{ print $1 }’
|awk -F@ ‘{ print $2 }’|sort|uniq -i -c
| awk ‘BEGIN {sum=0} {sum += $1} {print $0} END {printf(”—————————n “sum” Total E-mails that are properly formattedn”)}’
Thank you, awk
Flickr-ify your Gnome Screen Saver
by someguy on Oct.28, 2007, under Uncategorized
I’ve been reading about the estarling, a digital photo frame that will sit on a wireless network and grab photos from an RSS feed, on say, Flickr, or Picasaweb. What a fantastic idea this is — and really it’s the product I’ve been waiting to come to market for a long time if its features work as promised. Imagine: stick it in the home of your grandparents, parents, etc. - and _you_ can send the photos to their frame - keeping them up to date with all the latest photos from the lives of their family and friends.
To go along with this, I wanted a simple screen saver for my new install of Ubuntu Feisty-Fawn that did the same thing — grabbed latest images from an RSS feed on the web and then serve them when the screen was locked, or the system idle for more than a few minutes. It seemed like every Mac in my office has been taunting me with a feature like this, but maybe all those photo slideshows I’ve been seeing are from local images.
So, this is nothing short of a hack, but if you want this functionality on your desktop, maybe you’ll think it’s worth it like I did. The end result is Gnome Pictures Screen Saver with a modified version of bashpodder that grabs the latest photos from any feed(s) I specify to become the content for my screensaver.
The script takes a config file which consists of the URL to the feed you want to subscribe to and the xsl file which pulls out the URLs to the photos themselves from the feed. Currently RSS feeds/xsl transformations from Gallery, Picasaweb, and Flickr have been tested. When it runs, it checks the feed against files previously downloaded and only downloads the new stuff. It could be run from cron or perhaps when you log in. I suggest running it by hand the first time to make sure it is doing what you expect.
Package available in this thread:
Prerequisite pkg: libwww-perl (I used GET instead of wget - long story)
To install:
$tar -xzvf rss_photos.tar.gz
Edit rss_photos.sh and change the values of CONF_FILE and LOG_FILE to a permanent location. Create the CONF_FILE you just specified.
# Lines in CONF_FILE should be of the pipe-delimited format:
#
# e.g.
# http://api.flickr.com/services/feeds/photos_public .gne?id=YOURIDHERE&lang=en-us&format=atom|parse_flickr_enclosure.xsl
Obviously, you will need to enable the “Pictures Folder” screen saver in Gnome for this to work - the script simply grabs photos for the screen saver.
Linux: How many processors do I have?
by someguy on Sep.13, 2006, under Uncategorized
On Linux, you can get a ton of information about the physical hardware you are running right from the command line. Simply cat’ing /proc/cpuinfo will give you lots of gory details about the processors you have installed. Unfortunately, it gets a little confusing thanks to hyperthreading, dual cores, etc.. So here’s a simple command to find out how many physical processors you have installed.
$ dmesg | grep "Physical Processor" | awk '{ print $5 }' |sort -nu|wc -l
Variable Variables: Double Interpolation in Bash
by someguy on Sep.12, 2006, under Uncategorized
Ok, so most of you are wondering why I’d be even bothering to do double interpolation in Bash rather than a real language. Well, I already had some scripts in progress and I didn’t want to start from scratch. What is double interpolation and why would I bother?
In my case, I knew that I was going to have a dynamic list (an array), and that for every array member, there would be a number of other things I would need to know about it (yes, like a multi-dimensional array).
Perhaps we want to do some simple monitoring of an unknown amount of servers in each data center and for each server we need to know how to reach it -- the IP address, and the way we want to reach it to test (i.e.http/ping/etc.). So ideally, we need to be able to keep a separate config file which just holds X number of shell variables with server configurations in it -- but that the contents of that config file might be wildly different (number of servers, server specs, etc.) between environments and keep the script simple.
So, in one environment we want to monitor the servers, moe, larry, and curly (woop, woop, woop) :
We write a config file that has one array for the server names, then for each of those we define variables to hold the IP and check-type.
The config file, servers.cfg, looks like:
SERVERS="moe larry curly"
moe_IP=10.10.10.10
moe_CHECKTYPE=http
larry_IP=10.10.10.11
larry_CHECKTYPE=ping
curly_IP=10.10.10.12
curly_CHECKTYPE=ping
Then we write a script which just loops through the given servers and does the check based on whatever check-type and IP we’ve configured.
#!/bin/bash
# Source our config file
. servers.cfg
# Loop through the servers and test them!
for server in $SERVERS;
do
IP=$(eval echo $`echo ${server}`_IP);
CHECKTYPE=$(eval echo $`echo ${server}`_CHECKTYPE);
if [ "$CHECKTYPE" = "http" ];
then
# Do dome kind of http check - this is oversimplified for this example
wget $IP
elseif [ "$CHECKTYPE" = "http" ]
then
# Ping it! Normally we would test the success/failure of the ping here
ping $IP
fi
done
The beauty of what we have here is that we don’t need to reconfigure the script to add or remove servers, just maintain a simple config file.
The “if” statement: Shell-scripter’s Hammer
by someguy on Feb.25, 2006, under Uncategorized
One of the most common control structures in shell programming (and in many popular programming languages). Why? Because it is versatile and easy to understand. You can use it to test for many conditions that might exist. The syntax for an “if” statement can take several forms, but perhaps the most common is this one:
if [ <some test switch> <some file, number, or string of characters> ];
then
do something
fi
To see an exhaustive list of the comparison operators and switches here, issue “man test” at the command line. If it’s not obvious, the shell evaluates what happens between the brackets and only runs the code between “then” and “fi” if it evaluates to true.
Here’s a real example:
MYFILE=/etc/hosts
if [ -e $MYFILE ];
then
echo "/etc/hosts exists."
fi
and here’s what it looks like at a command line:
[me@myhost directory]$ MYFILE=/etc/hosts; if [ -e $MYFILE ]; then echo "$MYFILE exists." ; fi
/etc/hosts exists.
The “-e” flag tests if the file given exists.
What are some of the other things you can test for? Here’s a partial list:
-b FILE
FILE exists and is block special
-c FILE
FILE exists and is character special
-d FILE
FILE exists and is a directory
-e FILE
FILE exists
-f FILE
FILE exists and is a regular file
-g FILE
FILE exists and is set-group-ID
-G FILE
FILE exists and is owned by the effective group ID
-k FILE
FILE exists and has its sticky bit set
-L FILE
FILE exists and is a symbolic link
-O FILE
FILE exists and is owned by the effective user ID
-p FILE
FILE exists and is a named pipe
-r FILE
FILE exists and is readable
-s FILE
FILE exists and has a size greater than zero
-S FILE
FILE exists and is a socket
-t [FD]
file descriptor FD (stdout by default) is opened on a terminal
-u FILE
FILE exists and its set-user-ID bit is set
For a complete list, run “man test” at your shell prompt. The “if” statement is commonly used in most other languages as well. Easy to use and a powerful friend.
Want to know how to do something at the shell? Just ask.