Friday, June 19, 2009

I just need the file name!

I am always downloading files from the command prompt using curl or wget. Sometimes I like to automate the download process. One thing I found to be helpful is using sed to extract the file name from the URL.

I created the following bash script in utilizing my sendmail.pl Perl script found in Why wait for a download?. I called the following script DownloadAndEmailMe.sh. Here it is.

#!/bin/bash
curl -O $1
filename=`echo $1 | sed 's/\(.*\)\///'
perl /path/to/sendmail.pl localhost youremail@gmail.com youremail@gmail.com
"Download Done: $filename" "The following file is done downloading: $filename"
Of course there are a few things to change in the script above to make it yours:
1. Change /path/to/sendmail.pl to your own path to sendmail.pl
2. Change localhost to your own SMTP server name.
3. Change youremail@gmail.com to your own email.

This is how I run it
DownloadAndEmailMe.sh http://mirrors.easynews.com/linux/ubuntu-releases/jaunty/ubuntu-9.04-desktop-i386.iso
When the download is done it will send you a nice email that reads:
The following file is done downloading: ubuntu-9.04-desktop-i386.iso

I would also recommend looking at sed and see how it works. I use it a lot. It's very powerful.

Tuesday, June 16, 2009

Watching Hard Drive Activity

On Linux systems I use top all the time, but there is another tool called iotop which lets you watch disk IO on your system.

Here is how to install it on Ubuntu

sudo apt-get install iotop


It's really cool. Thanks Josh for finding it.

Sync directories with rsync

Sometime I need some directories synchronized between 2 different machines.

I use rsync to get the task done, my command would look like this:

rsync -zae "ssh" /path/to/local/dir/ remoteHost:/path/to/remote/dir/
To learn more bout rsync command visit the man pages

Every time you run this command you will be prompted for a password, which is OK if you run the sync manually. If you like to use cron to schedule the sync, you will need to refer to my post SSH without a password to do it.

Monday, June 15, 2009

SSH without a password

I like to log on to my remote hosts without a password, it saves you from prying eyes, and it helps you automate certain things, like backups. This is how to do it:

1. Generate your public/private keys using ssh-keygen.

ssh-keygen -t rsa
Note: Leave passphrase empty

2. Copy id_rsa.pub to the .ssh directory of the remote host you want to log on to as authorized_keys2.
scp ~/.ssh/id_rsa.pub foo@remoteHost:~/.ssh/authorized_keys2 
Note: If you have other keys in authorized_keys2 make sure not to overwrite it but append the new one to it.

3. SSH to the remote host and make authorized_keys2 writeable only to the user
chmod 644 ~/.ssh/authorized_keys2


Next time you log on to your remote host it will not prompt you for a password.

Friday, June 12, 2009

Why wait for a download?

Say you have to download the latest Ubuntu cd, but you don't want to just sit there until it's done. I like to start it and get an email on my Blackberry when it's done.

Step 1: Open a terminal window, Create a Perl script called sendmail.pl. You can use any editing tool, I prefer vi myself. Insert the following code in the file and save it.

#!/usr/bin/perl -w
use Net::SMTP;
if ($#ARGV != 4) {
print "usage:mailsever to from subject body\n";
exit;
}
my($mailServer) =$ARGV[0];
my($to)=$ARGV[1];
my(@mailTo)=split(/,/, $to);
my($mailFrom)=$ARGV[2];
my($mailSubject)=$ARGV[3];
my($mailBody)=$ARGV[4];
$smtp = Net::SMTP->new($mailServer);
$smtp->mail($mailFrom);
$smtp->recipient(@mailTo);
$smtp->data();
$smtp->datasend("To: " . $ARGV[1] . "\n");
$smtp->datasend("Subject: " . $mailSubject . "\n");
$smtp->datasend("\n");
$smtp->datasend($mailBody . "\n");
$smtp->dataend();
$smtp->quit;

Step 2: I use a tool like wget or curl to download a url
wget url
curl -O url

Step 3: All together, Step 1 and 2, command syntax
wget url; perl sendmail.pl mailserver to from subject body;

My command will look something like this
wget http://mirrors.easynews.com/linux/ubuntu-releases/jaunty/ubuntu-9.04-desktop-i386.iso; perl sendmail.pl localhost youremail@gmail.com youremail@gmail.com
"I am Done" "Download is done, file is ready";
Make sure to replace youremail@gmail.com with a valid email address.
In the above example, I am using localhost as the SMTP server.

After your file is done downloading you will get an email.
Note: This could also be used for any long running process.

Google Checkout

It seems to be a Google kind of day. Have you ever used Google Checkout? it seems to be a new way of checking out at a lot of online vendors. It's similar to PayPal. It makes it much easier to checkout, instead of putting in all your credit card information every time. Yes you must sign up for a Google account!

Google Reader

I seem to be addicted to Google Reader. Every chance I get when I am online I click the short-cut. I think it's great, it just has a cool way of displaying the RSS feeds. Check it out for yourself, you should get a Google account of course.