Unix

sed

sed is a stream editor. This tool allows you to change the contents of a file, without opening the file, but its power goes far beyond that.

Basic syntax:
sed -i 'backup_extension' 'command' filename

For example, if you had a file test.txt:

Hello World!
This is line 2!

In the first example, we will change the word ‘World’ to ‘User’:

sed -i '.bak' 's/World/User/' test.txt

This will give us the test.txt file containing Hello User!, and a backup file test.txt.bak.

Unix

Comments (0)

Permalink

cron / crontab

Cron is a tool used to automate tasks at a specified time. Crontab is the individual file (per user) that cron accesses.

Sample crontab:

@reboot /bin/sleep 60 && /usr/local/bin/fetchmail >/dev/null 2>&1
*/10 * * * * /usr/local/bin/fetchmail >/dev/null 2>&1

The first line:
“At reboot, sleep for 60 seconds, then run ‘fetchmail’, and output stdout to /dev/null.” By default, cron emails the root user output after crontab completion.

The second line:
“Every 10 minute interval for every hour, all days, all weeks, all months, run fetchmail, and output stdout in /dev/null.”

*/10 * * * * /usr/local/bin/fetchmail >/dev/null 2>&1
 ^   ^ ^ ^ ^ {_________^____________} {______^_______}
 |   | | | |           |                     |
 |   | | | |           |               stdout to /dev/null
 |   | | | |           |
 |   | | | |    command to run
 |   | | | |
 |   | | | all months
 |   | | |
 |   | | all weeks
 |   | |
 |   | all days
 |   |
 |   all hours
 |
 all minutes divided by 10 (or every :X0) ie, 6:10, 6:20, etc

Unix

Comments (0)

Permalink

rsync

Rsync is a tool to synchronize data. Before you read this tutorial, you should understand the conditions from which I am writing this.

My situation is as follows:

  • I use two separate machines to update this website.
  • I need a “fast” tool to read/write “changed” files.
  • I need to do this over an SSH (secure shell) connection.

Both of my development boxes are either running FreeBSD or Ubuntu, so under FreeBSD, I compiled rsync from ports.

cd /usr/ports/net/rsync
make install clean

*Be sure to compile with the “Use SSH for RSH connections” option.

Once rsync is compiled, you are ready to create a script. Again, keep in mind I am using this tool to connect to a remote hosting company server to update my website. You will have to change what I have posted here. I have included #comments.

My sample `webdevget’ script updates files FROM my server TO my development machines.

#!/bin/sh

echo "Starting connection"
/usr/local/bin/rsync \
 -razv \
 --delete \
 --progress \
 --exclude=logs \
 --exclude=*~ \
 username@hosting.com:/home/username/* \
 /home/remote/files
 echo "Connection closed."
 echo "Done!"

What this script does, line by line:

#!/bin/sh

# Acknowledge the script has been initialized
echo "Starting connection"
# start `rsync' with the 'recursive', 'archive',
# 'compress', and 'verbose' flags, excluding any
# logs and backup (~) files
/usr/local/bin/rsync \
 -razv \
 --delete \
 --progress \
 --exclude=logs \
 --exclude=*~\

# log in to 'hosting.com' with 'username' (you will be
# prompted for a password), and copy all files from
# '/home/username/' to the '/home/remote/files' directory.
username@hosting.com:/home/username/* \
/home/remote/files

# Acknowledge the script has been ended
echo "Connection closed."
echo "Done!"

Unix

Comments (0)

Permalink

Mutt + Gmail IMAP

Please note that this page is not necessarily a “how-to”, but more of an example of my configuration decisions. At the time of this writing, I am using Mutt 1.5.18 (2008-05-17), the mail/mutt-devel port in the FreeBSD ports tree.

Problem:
You want to use the mail client mutt to access your Gmail account.

Solution:
Mutt needs to be compiled with IMAP, SMTP and OpenSSL support for this to work. If you already have mutt installed, you can check the build flags with the following command:

mutt -v

If you do not see the options:

+USE_IMAP  +USE_SMTP +USE_SSL_OPENSSL

you will need to recompile mutt.

Compiling Mutt with IMAP, SMTP, OpenSSL Support in FreeBSD:
In FreeBSD, to compile mutt with the required options:

cd /usr/ports/mail/mutt-devel
make -DWITH_MUTT_IMAP \
 -DWITH_MUTT_IMAP_HEADER_CACHE \
 -DWITH_MUTT_SMTP \
 -DWITH_MUTT_CYRUS_SASL2
make install

Explanations:

  • The WITH_MUTT_IMAP option enables IMAP server connectivity — disabled by default.
  • The WITH_MUTT_IMAP_HEADER_CACHE option saves mail headers locally, enabling a quicker folder download time — especially useful if you use Gmail for mailing lists.
  • The WITH_MUTT_SMTP option is not required if you only plan to read email locally, or if you run a local mail server.
  • The WITH_MUTT_CYRUS_SASL2 option allows mutt to connect to the Gmail SMTP servers with TLS authentication — a requirement if you plan to send mail with mutt through Gmail.

Configuring Your ~/.muttrc:
As with all other mutt tricks, you need to edit your ~/.muttrc file to get IMAP/SMTP connectivity. My sample configuration is provided here:

set spoolfile = imaps://imap.gmail.com:993/INBOX        # Default to the inbox
set folder = "imaps://imap.gmail.com:993/"              # Connect to IMAP over SSL
set imap_user = 'my_username@gmail.com'                 # User name
set imap_pass = 'my_secret_password'                    # Password
set smtp_url="smtp://my_username@smtp.gmail.com:587/"   # User name
set smtp_pass = 'my_secret_password'                    # Password
set postponed="imaps://imap.gmail.com/[Gmail]/Drafts"   # Save 'postponed' mail to Drafts
set mail_check=300                                      # Check for new mail every 5 minutes
set imap_check_subscribed="yes"                         # Check 'subscribed' folders only
set imap_list_subscribed="yes"                          # List only 'subscribed' folders
set header_cache="~/.mutt/msgcache"                     # Location to save cached mail headers
set message_cachedir="~/.mutt/cache/bodies"             # Location to save cached mail bodies
set certificate_file="~.mutt/certs"                     # SASL2 certificate location
set timeout=60                                          # Reconnect every minute
macro generic,index,pager y ?                           # Press 'y' to change folders
bind pager,browser y exit                               # Press 'y' to change folders

Some options in my sample ~/.muttrc file may seem a bit odd at first, but after reading the sections below, should become more clear. If not, please use the contact form, and let me know what was unclear, or how I could improve this tutorial.

Individual Preferences:
This is the part that gets a bit tricky. This is the part where you, the reader, are required to be creative. Here’s what I mean:

  • Gmail sees “Labels” as folders
  • Gmail uses its own “default” folder — [Gmail]
  • Labels exist outside of the [Gmail] folder

So, logically, the hierarchy would look as such:

[Gmail]
        /Inbox
        /Sent Mail
        /Drafts
Mutt-Users
Mutt-Devel
KDE-General
KDE-Devel
FreeBSD-Questions
FreeBSD-Stable
FreeBSD-Current
FreeBSD-PF
FreeBSD-Jails
School-Professors
School-Students
School-Clubs-CIS

Though this layout works perfectly fine, depending on how many labels you have, this can get a bit cumbersome, even with using header and body caching. The solution I found is to name labels in a Unix-like folder fashion. For instance, School-Clubs-CIS would the become School/Clubs/CIS, thus making the logical layout as any other Unix directory:

[Gmail]
        /Inbox
        /Sent Mail
        /Drafts
Lists
        /Mutt
                /Users
                /Devel
        /KDE
                /General
                /Devel
        /FreeBSD
                /Questions
                /Stable
                /Current
                /PF
                /Jails
School
        /Professors
        /Students
        /Clubs
                /CIS

As I said, this is where you have to be creative. My solution here will not work for everyone, and I’m certain there are many naming conventions others will prefer.

By the way, this is where the ‘y’ in the ~/.muttrc file comes in — from the Inbox, pressing ‘y’ will take you to the folder list, where you can navigate through your folders.

FreeBSD
Linux
Unix

Comments (0)

Permalink

Passwordless SSH

Secure Shell (ssh) is a widely used UNIX tool to manage remote servers over a secured connection. Typically, the syntax is:

ssh username@remoteserver

However, ssh is a highly versatile tool. For example, if you ran the ssh daemon (or background process) on a port other than the default (22) — let’s say 8800 — you would execute the following command:

ssh -p 8800 username@remoteserver

Another useful feature that ssh is capable of is “passwordless authentication.” To do this, you have to:

  1. Generate a private SSH key pair
  2. Upload the public key to the server
  3. Test it

First, generate the key:

ssh-keygen -t rsa

*To generate a ‘passwordless’ key, do not enter anything when prompted for a password. (This typically is not recommended, however I use passwordless SSH for my rsync tutorial). Just press [Enter]. For a complete list of the ssh-keygen parameters, ie., number of bytes the key should contain, ‘man ssh-keygen’. Next, upload the key to the server.

Method 1:

  • fish:// protocol (included in KDE’s Konqueror)
  • Screen sessions (using copy/paste — although my reason for using ’screen’ is explained next)

I use ’screen’ to test the SSH key for a very good reason — the chance of getting locked out of the server. Let’s face it. Weird things happen. That includes corrupt file transfers. If you upload the key using the ssh protocol alone (which is possible), and the file is corrupt in transit, there is a (slight) chance of locking yourself out of your server. That would be bad.

So now, copy the information to the remote server:

cd ~/.ssh
less id_rsa.pub

The information starting with “ssh-rsa” to “username@host” all has to be copied to the server’s ~/authorized_keys file. If you would rather append a key (if you wanted more than one passwordless client), copy the file as a different filename, for example, my_key, and on the server, run:

cd ~/.ssh
cat my_key >> authorized_keys

Method 2:
Use the ssh-copy-id tool included with OpenSSH to upload the key to the ~/.ssh/authorized_keys file for remoteuser@remotehost.

ssh-copy-id ~/.ssh/id-rsa.pub remoteuser@remotehost
ssh remoteuser@remotehost

Now, you’re ready to test your passwordless ssh connection.

Unix

Comments (0)

Permalink