EasyDNS With Python
Written by Jonathan Hood   
Monday, 02 July 2012 17:50

I recently needed a simple dynamic DNS updater for the EasyDNS dns service. There was a linux client available, but it was written in C and required quite a bit of work to get working. So, I wrote a simple python library to do the job.

[Download]

Features

  • Retrieve your publicly available IP address using the service at icanhazip.com
  • Update a domain in the EasyDNS service with the ip address of the current system.
Requirements
  • Python 2.6.x, Python 2.7.x
  • This will not work in Python 3.x.
To Use:
import ddns
print ddns.getPublicIPAddr()
ddns.username = 'username'
ddns.password = 'password'
ddns.updateEasyDNS( 'my.domain.com' )

 

Last Updated on Monday, 02 July 2012 18:00
 
Virtual Email Domains with Postfix and Courier
Written by Jonathan Hood   
Tuesday, 01 February 2011 22:30

I've recently had to setup postfix and courier on a server that hosts multiple domains. Setting up the two for this task isn't hard, but proper documentation and tutorials can be hard to come by. We won't be using any extra services like MySQL to store the users and mailbox mappings. It works great for a small number of domains. I'm going to assume that Postfix and Courier are already set up and work for email addresses at your server's hostname. This how-to was tested on Debian Lenny. For our example, we will be creating the mail@foobar.org email address.

First, open /etc/postfix/main.cf.

:/pico /etc/postfix/main.cf

There, you are going to add a few configuration keys to the end of the file.

virtual_mailbox_domains = foobar.org
virtual_mailbox_base = /var/mail
virtual_mailbox_maps = hash:/etc/postfix/vmail
virtual_uid_maps = static:6000
virtual_gid_maps = static:6000

Now, lets create the /etc/postfix/vmail file. This is where we will list the mailboxes and their directory to be stored

:/pico /etc/postfix/vmail
#Insert these values
mail@foobar.org          /var/mail/foobar.org/mail/

Now, we need to create a user for postfix to use to put the mail into each mailbox

:/groupadd -g 6000 vmail
:/useradd -g vmail -u 6000

Now, create the mailbox directory and give permission to vmail.

:/mkdir -p /var/mail/foobar.org
:/chown vmail:vmail -R /var/mail

Now, create the hash of our vmail mapping file. This has to be done every time you adjust the /etc/postfix/vmail file. Then, we'll reload postfix and let it run the new config.

:/postmap /etc/postfix/vmail
:/postfix reload

Ok, postfix is set up. Now, lets setup courier. For courier, we will be using the userdb auth package for authentication. For that to work, we need to give it a list of users with their mail directory. Then we will assign a password to each user. The first step is to update the authdaemonrc configuration.

:/pico /etc/courier/authdaemonrc
#Change this key
authmodulelist="authpam"
#to
authmodulelist="authpam authuserdb"

Now, lets create the mail user in userdb. Whenever we create a user, we need to set uid=6000 and gid=6000 since this is the id of the vmail user used by postfix. The other variables point to the mailbox directory created by postfix.

:/userdb mail@foobar.org set uid=6000 gid=6000 home=/var/mail/foobar.org/mail \
mail=/var/mail/foobar.org/mail
:/userdbpw -md5 | userdb mail@foobar.org set systempw

Every time we change userdb, we have to update it. To do so:

makeuserdb

Now, just reload authdaemonrc.

:/ /etc/init.d/authdaemonrc reload

That should do it. Try logging in to the new email address. Note that you must use the full email address as the username.

Credit to dannorth.net for alot of this information. In many cases these steps are verbatim, but some of them had to be corrected (hence the motivation for this page).

Last Updated on Wednesday, 02 February 2011 00:19
 
Map Generator
Written by Jonathan Hood   
Tuesday, 01 February 2011 22:15

After playing Minecraft for awhile I got interested in procedural terrain generation. I wondered how a world could just be 'generated' while playing a game. So, like all good problems, I wrote a small program to solve it. In my research I can across the Diamond-Square algorithm and made an implementation of it. The output looks like this:

 

 

Here, if this image where to be interpreted as a height map, the blacker areas represent higher elevation and the white areas represent lower elevations. Using this data, a map can be generated.

If you want to try it out for yourself, download my implementation. It requires the pypng library to visualize its output. It has been tested on python 2.6.

Last Updated on Tuesday, 01 February 2011 23:52
 
How-To Send Email with Python
Written by Jonathan Hood   
Sunday, 30 January 2011 22:56

Sending email from a script is a very useful tool. On a linux machine, you can setup a mta to get mail from any cron jobs, but there are alot of situations where that functionality may not be available. This happened to me the other day, and I found the fantastic smtplib that exists in the python standard library. So, you need to send a message? Its super easy, first import smtplib and connect to a server

import smtplib
server = smtplib.SMTP( "your.smtp.address" )

At this point there may or may not be a few extra steps to perform. My test server is configured for STARTTLS and authentication. I'll detail the steps for that here.

server.starttls()
server.ehlo()
server.login( "username", "password" )

Now, build your message. The primary downside to smtplib is that you have to build the header yourself. As it turns out, that is pretty trivial.

mailto = "test.email@foo.com"
mailfrom = "otheremail@foo.com"
subject = "Test Email"
head = "From: " + mailfrom + "\r\nTo: " + mailto 
       + "\r\nSubject: " + subject + "\r\n\r\n"

The rest of the message is just plain text.

body = "Hello world!"

Now, put the two together and send the message.

msg = head + body
server.sendmail( mailfrom, mailto, msg )

I wrote a function to encapsulate this behavior, maybe it will be useful for you.

def sendEmail( mailfrom, mailto, subject, body, servername, username, password ):
    import smtplib
    server = smtplib.SMTP( servername )
    server.starttls()
    server.ehlo()
    server.login( username, password )
    msg = "From: " + mailfrom + "\r\nTo: " + mailto 
          + "\r\nSubject: " + subject + "\r\n\r\n" + body
    server.sendmail( mailfrom, mailto, msg )
Last Updated on Tuesday, 01 February 2011 23:52
 
<< Start < Prev 1 2 3 Next > End >>

Joomla templates by a4joomla