Archive

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()])
file.close()

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 = "http://notalwaysright.com/?random";
        break;
    case 13:
        $url = "http://icanhascheezburger.com/?random";
        break;
    case 66:
        $url = "http://dynamic.xkcd.com/comic/random/";
        break;
    case 73:
        $url = "http://bash.org/?random";
        break;
    case 100:
        $url = "http://linux.die.net/man/4/random";
        break;
    default:
        $url = "http://www.google.com/";
}
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:
                activemails.append(line.split(None,1)[0])
file.close()

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
file.close()
Categories: Coding, Servers Tags: , , ,

dork-e.com is Stephen Fry proof thanks to caching by WP Super Cache