Archive for the ‘Coding’ Category

Quick Summary of User Home Sizes

April 28th, 2009 No comments

I had a niche need to make an easy summary of user directory sizes for an FTP server where the users’ homes could be in one of a few locations. I whipped up this script which should make it easy even for an ultra-novice to get this summary:

#!/usr/bin/env python

import subprocess

def run_this(commandline):
    process = subprocess.Popen(commandline, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    out,err = process.communicate()
    return out

print "Now calculating home directory size for all non-system users.\nThis may take a few minutes, please wait."

file = open('/etc/passwd','r')
userlist = list()
for line in file:
    user = line.split(':')
    if int(user[2]) >= 1000 and "nobody" not in user[0]:
        usage = run_this("du -hs " + user[5] + " | cut -f 1")
        userlist.append([user[4].split(',',1)[0], usage.strip()])

print "\nStorage Consumed by Users"
print "------------------------------------------------\n"

for userinfo in userlist:
    print userinfo[0] + "\t\t\t" + userinfo[1]

print "\n"

Categories: Coding, Servers Tags: , , ,

Randomly Redirect

April 14th, 2009 No comments

So, I recently had a need to randomly redirect a page on occasion…don’t ask.

< ?php
$case = mt_rand(1,100);
switch($case) {
    case 42:
        $url = "";
    case 13:
        $url = "";
    case 66:
        $url = "";
    case 73:
        $url = "";
    case 100:
        $url = "";
        $url = "";
header("Location: " . $url);

Categories: Coding Tags: , ,

Quickly find disabled emails in Virtualmin

April 8th, 2009 2 comments

Anybody who’s adminned a Virtualmin server has probably had update hell.  One that I personally have had to wrangle with is where Virtualmin will randomly disable email accounts, resulting in frustrated users calling you when noone can email them. I’ve written a quick Python script to dump out a list of all disabled email accounts to reduce the inevitable frustration caused by this issue, allowing one to quickly find all of the disabled emails and re-enable them, hopefully before anyone complains. 🙂

#/usr/bin/env python

file = open('/etc/postfix/virtual','r')
activemails = list()
for line in file:
        if '@' in line:

file = open ('/etc/passwd','r')
for line in file:
        if '@' in line and '/bin/false' not in line:
                email = line.split(':',1)[0]
                if email not in activemails:
                        print email

Categories: Coding, Servers Tags: , , ,