Postgres and REALBasic

I’m learning REALBasic and starting to shift over to writing cross platform desktop apps, and one thing I wanted to do is to talk to a postgres database. Here is a quick and dirty bit of code:

dim pgdb as PostgreSQLDatabase
dim sql as string
dim rs as RecordSet

pgdb = new PostgreSQLDatabase
pgdb.DatabaseName = “database.name”
pgdb.Host = “database.hostname”
pgdb.UserName = “user”
pgdb.Password = “password”

if pgdb.Connect then

sql = “SELECT foo FROM bar WHERE woo=’yay’;”
rs = pgdb.SQLSelect(sql)
if rs <> nil then
while not rs.EOF
EditField1.Text = rs.Field(”foo”)
rs.MoveNext()
wend

else
EditField1.Text = “NULL RETVAL”
end if
else
MsgBox “Connect Fail”
end if

Endpoint Compliance Systems

I have had some experience with a certain Endpoint Compliance System (ECS) and have been thinking about how I could write a cross-platform compatible ECS myself using freeRADIUS as its authenticating agent.

An ECS has an agent which is installed on a user’s machine, runs checks and reports back to a central server which then decides whether or not to allow elevated access to a wireless network.

On my development server, I already have a working RADIUS which talks to a database, and my wireless network is authenticating against the RADIUS, so all I need to do now is to write the small application which can do the following:

  • Run specific checks on the client machine
  • Report back to the RADIUS server on whether or not to allow the device

I think this system would be really useful for networks which can support multiple networks – have one which is locked down and one which is authenticated by RADIUS for network / Internet traffic.

NukeAll – for OSX

nukeall finds and removes (recursevely) the specified file(s) in the current folder.
Example:
nukeall .DS_Store
nukeall *.tmp
nukeall .*

You can also pass the -trashes option which will search and empty. Trashes folders which is useful on file servers.

Version 1.0 can be downloaded by clicking here

Check Apple RAIDs and email when there is a problem

This script can be used in a cron which runs every 5 or 10 minutes and will email as soon as it detects an issue with an Apple RAID.
I am working on an Apple XServe RAID checker, but this one will handle internal drive bay RAIDs.

#!/usr/bin/env python

import os
import string
import smtplib

from socket import gethostname

smtpserver = 'localhost'
smtpfrom = gethostname() + '@localhost'
smtpto = ['##EMAIL_OF_RECIPIENT##']

raidstatus = os.popen('/usr/sbin/diskutil listRaid')
status = raidstatus.read()
raidstatus.close()
if status.find('Degraded') > -1:
     smtpsubject = 'WARNING - Raid degraded on ' + gethostname()
     smtp = smtplib.SMTP(smtpserver)
     message = """\
From: %s
To: %s
Subject: %s

%s
""" % (smtpfrom, ", ".join(smtpto), smtpsubject, status)
     smtp.sendmail(smtpfrom, smtpto, message)
     smtp.quit()

Check OSX partitions and email results

I wrote a small Python script which gets a list of partitions and checks each one to make sure the partition is okay, and for the script to email the results.

#!/usr/bin/env python

import os
import string
import smtplib

from socket import gethostname

smtpserver = 'localhost'
smtpfrom = gethostname() + '@localhost'
smtpto = ['##recipient email##']

driveshandle = os.popen('/bin/df -h | grep disk')
drives = driveshandle.read()
driveshandle.close()
drives = drives.split('\n')

for drive in drives:

     diskinfo = ''
     if drive.find('dev') > -1:
          partition = drive.split(' ')
          print 'Drive ' +partition[0]
          fsck = os.popen('/sbin/fsck_hfs -fy ' + partition[0])
          diskinfo = fsck.read()
          fsck.close()
          smtpsubject = 'File System Checks for ' + gethostname() + ', drive ' + drive
          smtp = smtplib.SMTP(smtpserver)
          message = """\
From: %s
To: %s
Subject: %s

%s
""" % (smtpfrom, ", ".join(smtpto), smtpsubject, diskinfo)
          smtp.sendmail(smtpfrom, smtpto, message)
          smtp.quit()

print 'completed'

OSX Command Line Goodies

You can take a screenshot by command:

screencapture -C ~/screen.png

This will save a screenshot into the user’s home folder.

You can get system information:

/usr/sbin/system_profiler SPHardwareDataType

Which would show something like this:
Hardware:

Hardware Overview:

Model Name: iMac
Model Identifier: iMac8,1
Processor Name: Intel Core 2 Duo
Processor Speed: 2.66 GHz
Number Of Processors: 1
Total Number Of Cores: 2
L2 Cache: 6 MB
Memory: 4 GB
Bus Speed: 1.07 GHz
Boot ROM Version: IM81.00C1.B00
SMC Version (system): 1.29f1
Serial Number (system): VM906KT5ZE3
Hardware UUID: FB5DEAE0-DAB6-56DD-A62B-0F38C384654A

Speaking- You can make the computer speak by commandline:

say -v Albert "Hello world"

Albert can be replaced with any of the following:

Agnes,Albert,BadNews,Bahh,Bells,Boing,Bruce,Bubbles,Cellos,Deranged,Fred,GoodNews,Hysterical,Junior,Kathy,Organ,Princess,Ralph,Trinoids,Vicki,Victoria,Whisper,Zarvox

Automatic checking of IMAP subfolders in Thunderbird

Open up the preferences dialog (either Tools -> Preferences or Edit -> Preferences), and click Advanced -> General -> Config Editor.
In the filter box type ‘mail.check’ and double click on mail.check_all_imap_folders_for_new.

That should do it..

The Dreadnought Framework

A friend of mine and I are working on a pretty impressive project which will hopefully fill a niche in the IT market.
Its a pretty impressive amount of work, but we are both up for a challenge, and the chance to sink a few beers on a project meeting will be a necessity.

One major milestone has been completed – the Dreadnought framework. Its basis is two files – index.php and _dreadnought.php. All heavy duty code is held in the dreadnought kernel – accessable through $dreadnought. Its very very simple. No access control, basic MySQL support but has a promising future.

I am developing a ‘blogging’ module into it, and hopefully will move this site from wordpress into Dreadnought in the not too distant future.

The one thing I really love about the way I have designed dreadnought is that the templating system could not be easier. You have a html file and just add the tag <DREADNOUGHT_CONTENT /> and the code does the rest.
I’m hoping to release the framework as open source soonish, but not until some more work has gone into it, including some rudimentary pre-flight checking of modules – ie to prevent a module from killing the framework’s kernel.

Mounting samba permanently

A nice quick and easy one..

Add the following into /etc/fstab, replacing the parts with stars with your information.

//**SERVER**/**SHARE** /mnt/**MOUNTPOINT** smbfs rw,user,username=**USER**,password=**PASS** 0 0

make sure the location /mnt/**MOUNTPOINT** exists, chmodded correctly (eg 666) and then just mount it with:

mount /mnt/**MOUNTPOINT**

Blocking tor through iptables

I keep a database of tor hosts which gets updated quite often, and I have written a script in python which downloads the list and updates iptables so that they are all blocked.

It is recommended that the script gets added to cron so that the machines iptables keep up to date.

This script is ideal for schools and businesses to prevent people from circumventing content filtering, and also useful for servers to prevent abuse from ‘anonymous’ people who use tor for hacking for denial of service.

The script can be found here (http://www.andydixon.com/blocktor.py) and requires python (tested on v2.6)