Multiple VLANs on 1 network port with Ubuntu / Debian

If you have a network with multiple VLANs and you want to be able to connect on more than one, you can set up virtual nics, one for each vlan.

To do this, you need to install the vlan package:
sudo apt-get -y install vlan

You then need to edit your /etc/network/interfaces to add the additional nics. For each vlan you wish to access, add the following lines, replacing information where necessary:

auto vlan5
iface vlan5 inet static
address 10.5.1.2
netmask 255.255.255.0
network 10.5.0.0
broadcast 10.5.255.255
gateway 10.5.0.1
mtu 1500
vlan_raw_device eth0

Once saved, you can just give your machine a quick reboot and all will be peachy.

Clearing MAILER-DAEMONS from Mail Queue

This little gem will certainly help get rid of pending bounce messages resulting from spam hitting your mail server:

mailq | tail -n +2 | grep -v ‘^ *(’ | awk ‘BEGIN { RS = “” }{ if ($7 == “MAILER-DAEMON” ) print $1 }’ | tr -d ‘*!’ | postsuper -d -

You cant really get simpler than this…

Getting a List of Tor server Nodes in perl

In 2008, I wrote a module in perl called Net::Tor::servers (which can be found in CPAN). Below is the code (albeit not as complicated as the module) which will get a list of Tor servers into an array called @torarray.

One use of this module is a script I wrote which adds all the Tor servers into an IP blacklist on Smoothwall firewalls. I used to run the community version at home last year but got rid of it when the box died.
The script can be found here:
http://www.cpan.org/modules/by-authors/id/A/AJ/AJDIXON/torblacklister.pl

The code is thus:

#/usr/bin/perl
use LWP::Simple
my @torarray=();
my @arrayrecord=();
my $router;
my @rarray;
my $torserver = "128.31.0.34";
my $port = 9031;
my $content = get("http://$torserver:$port/tor/status/all");
my @lines = split(/\n/,$content);

foreach $router (@lines) {
@rarray = split(/\ /,$router);
if($rarray[0] =~ /^r$/) {
my $ip=$rarray[6];
my $hostname=$rarray[1];
my $orport = $rarray[7];
my $dirrepport = $rarray[8];
@torarray = (@torarray,[$ip,$hostname,$orport,$dirrepport]);
}
}

Updated: oops, forgot the use clause…

Repairing OSX 10.4 Mail Server Mailboxes

This script could easily be adapted to work for any postfix server, but at this moment, I dont really have the time to do it. If someone can, then please let me know at andy at andydixon.com

You run this script in this way:

repair (username) – eg: repair ajd
(where ajd is a user on the mail server)

The script is thus:


#!/bin/bash

imapUser="cyrusimap"
reconstructUser="cyrusimap"
username="$1"

userDirDest=`sudo -u ${imapUser} /usr/bin/cyrus/bin/mbpath user/$username`

echo -e "Reconstructing folder structure.."
sudo -u ${reconstructUser} /usr/bin/cyrus/bin/reconstruct -r -f user/$username

echo -e "Fixing folder permissions and ownership.."
find $userDirDest -type d -print0 | xargs -0 -n 1 chmod 0700
find $userDirDest -type d -print0 | xargs -0 -n 1 /usr/sbin/chown ${imapUser}:mail

echo -e "Fixing file permissions and ownership.."
find $userDirDest -type f -print0 | xargs -0 -n 1 chmod 0600
find $userDirDest -type f -print0 | xargs -0 -n 1 /usr/sbin/chown ${imapUser}:mail

echo "Repair Completed"

Mounting Samba (Windows) shares on the commandline

There are two ways of mounting a share, depending on what the server you are connecting to is.

For anything apart from Windows Server 2003 onwards, (this is XP, Windows 2000 server, NT server, other samba servers, etc):

mount -t smbfs -o username=username //windows.andydixon.com/share-name /mnt/windows-share-name

Otherwise, for Windows 2003 server onwards, you need to change smbfs to cifs. Either way, you’ll be prompted for a password..

Backing up with netcat

Netcat is a simple program which listens on a port of your choice and can send any data to a file. This is useful for backups.

I like using tar for backing up lots of files, and using the network to copy from one server to another, so on the destination machine, I open up a port:

nc -l -p 1337 > backup.tar.gz

Then on the source machine, I can use netcat to send the data across the network to the destination:

tar zlcvPpf - /location/to/backup | nc backup-destination.andydixon.com 1337
(where backup-destination.andydixon.com is the backup location, either IP address or FQDN)

The weird tar variables are (in order):
z – use gzip
l – packup only one file system (miss proc, cd-rom, nfs, samba and other mounts)
c – create the archive
v – verbose (see whats happening)
P – keep the leading / from paths
p – Keep file permissions
f – file to write to (the hyphen means that you are writing to stdout, which is piped to netcat)

If you want to recover data:

On the backup device:
nc -l -p 1337 < backup.tar.gz
This will spit the contents of the compressed archive to any open connections

On the target machine:

nc backup-destination.andydixon.com 1337 | tar -xz

Easy as pie.

Quick Passwordless SSH tutorial

I keep forgetting how to do this so I have finally written it down..

On the machine which you are ssh-ing from:
ssh-keygen -t rsa

You should see:
Generating public/private rsa key pair.
Enter file in which to save the key (/home/andy/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
(Just hit enter here)
Enter same passphrase again:
(enter again)
Your identification has been saved in /home/andy/.ssh/id_rsa.
Your public key has been saved in /home/andy/.ssh/id_rsa.pub.
The key fingerprint is:
f6:61:a8:27:35:cf:4c:6d:13:22:70:cf:4c:c8:a0:23 andy@leviathan

To copy your key to the target machines (eg a server) you can use:
cat ~/.ssh/id_rsa.pub | ssh (username)@(server) ' cat >>~/.ssh/authorized_keys'

That should be it. If it does not work, try this on the target machine:
chmod 644 ~/.ssh/authorized_keys

UFO sighting last night

Last night I was out in my garden and I saw some strange lights, so naturally I took some photos on a camera which were not really suited for in-the-dark photography. It was really bizarre watching them go across the sky..

ufocloseup
noflash

The close-up one was taken when I was resting the camera on something to keep it steady.

After an hour or so after tweeting the pictures, I had a reply from @andytoots who has experienced a similar display of orange lights in the sky, and his images can be found here: http://andytoots.wordpress.com/2009/09/12/the-aliens-are-coming/

See if people you follow are following you in twitter

This is a very quickly hacked together script, but it does its job. Careful though, after a certain number of requests, your IP gets blocked. I’ve done it, and I have begged for it to be whitelisted…

Anyhow, for your enjoyment, here is the script. Its nothing spectacular and needs a lot of work. Consider it version 0.00.00.00.01a:


<?php
$username
=$_REQUEST['user'];

$friendsurl='http://twitter.com/statuses/friends/'.$username.'.xml';
$friends=xml2array(file_get_contents($friendsurl));

foreach ($friends['users']['user'] as $friend){
    echo 
'<strong>@'.$friend['screen_name'].'</strong> ('.$friend['name'].') ';
    
$result=file_get_contents('http://twitter.com/friendships/exists.xml?user_b='.$username.'&user_a='.$friend['screen_name']);
    if (
eregi('true',$result)) { 
        echo 
'is following you.';
    } elseif (
eregi('false',$result)) {
        echo 
'is <strong>not</strong> following you.';
    } else {
            echo 
htmlspecialchars($result);
        }
    echo 
'<br>';
}

/** 
 * xml2array() will convert the given XML text to an array in the XML structure. 
 * Link: http://www.bin-co.com/php/scripts/xml2array/ 
 * Arguments : $contents - The XML text 
 *                $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value. 
 *                $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance. 
 * Return: The parsed XML in an array form. Use print_r() to see the resulting array structure. 
 * Examples: $array =  xml2array(file_get_contents('feed.xml')); 
 *              $array =  xml2array(file_get_contents('feed.xml', 1, 'attribute')); 
 */ 
function xml2array($contents$get_attributes=1$priority 'tag') { 
    if(!
$contents) return array(); 

    if(!function_exists('xml_parser_create')) { 
        
//print "'xml_parser_create()' function not found!"; 
        
return array(); 
    } 

    //Get the XML parser of PHP - PHP must have this module for the parser to work 
    
$parser xml_parser_create(''); 
    
xml_parser_set_option($parserXML_OPTION_TARGET_ENCODING"UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss 
    
xml_parser_set_option($parserXML_OPTION_CASE_FOLDING0); 
    
xml_parser_set_option($parserXML_OPTION_SKIP_WHITE1); 
    
xml_parse_into_struct($parsertrim($contents), $xml_values); 
    
xml_parser_free($parser); 

    if(!$xml_values) return;//Hmm... 

    //Initializations 
    $xml_array = array(); 
    
$parents = array(); 
    
$opened_tags = array(); 
    
$arr = array(); 

    $current = &$xml_array//Refference 

    //Go through the tags. 
    $repeated_tag_index = array();//Multiple tags with same name will be turned into an array 
    
foreach($xml_values as $data) { 
        unset(
$attributes,$value);//Remove existing values, or there will be trouble 

        //This command will extract these variables into the foreach scope 
        // tag(string), type(string), level(int), attributes(array). 
        extract($data);//We could use the array by itself, but this cooler. 

        $result = array(); 
        
$attributes_data = array(); 
         
        if(isset(
$value)) { 
            if(
$priority == 'tag'$result $value
            else 
$result['value'] = $value//Put the value in a assoc array if we are in the 'Attribute' mode 
        

        //Set the attributes too. 
        
if(isset($attributes) and $get_attributes) { 
            foreach(
$attributes as $attr => $val) { 
                if(
$priority == 'tag'$attributes_data[$attr] = $val
                else 
$result['attr'][$attr] = $val//Set all the attributes in a array called 'attr' 
            

        } 

        //See tag status and do the needed. 
        
if($type == "open") {//The starting of the tag '<tag>' 
            
$parent[$level-1] = &$current
            if(!
is_array($current) or (!in_array($tagarray_keys($current)))) { //Insert New tag 
                
$current[$tag] = $result
                if(
$attributes_data$current[$tag'_attr'] = $attributes_data
                
$repeated_tag_index[$tag.'_'.$level] = 1

                $current = &$current[$tag]; 

            } else { //There was another element with the same tag name 

                if(isset($current[$tag][0])) {//If there is a 0th element it is already an array 
                    
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result
                    
$repeated_tag_index[$tag.'_'.$level]++; 
                } else {
//This section will make the value an array if multiple tags with the same name appear together 
                    
$current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array 
                    
$repeated_tag_index[$tag.'_'.$level] = 2
                     
                    if(isset(
$current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well 
                        
$current[$tag]['0_attr'] = $current[$tag.'_attr']; 
                        unset(
$current[$tag.'_attr']); 
                    } 

                } 
                $last_item_index $repeated_tag_index[$tag.'_'.$level]-1
                
$current = &$current[$tag][$last_item_index]; 
            } 

        } elseif($type == "complete") { //Tags that ends in 1 line '<tag />' 
            //See if the key is already taken. 
            
if(!isset($current[$tag])) { //New Key 
                
$current[$tag] = $result
                
$repeated_tag_index[$tag.'_'.$level] = 1
                if(
$priority == 'tag' and $attributes_data$current[$tag'_attr'] = $attributes_data

            } else { //If taken, put all things inside a list(array) 
                
if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array... 

                    // ...push the new element into that array. 
                    $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result
                     
                    if(
$priority == 'tag' and $get_attributes and $attributes_data) { 
                        
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data
                    } 
                    
$repeated_tag_index[$tag.'_'.$level]++; 

                } else { //If it is not an array... 
                    
$current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value 
                    
$repeated_tag_index[$tag.'_'.$level] = 1
                    if(
$priority == 'tag' and $get_attributes) { 
                        if(isset(
$current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well 
                             
                            
$current[$tag]['0_attr'] = $current[$tag.'_attr']; 
                            unset(
$current[$tag.'_attr']); 
                        } 
                         
                        if(
$attributes_data) { 
                            
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data
                        } 
                    } 
                    
$repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken 
                

            } 

        } elseif($type == 'close') { //End of tag '</tag>' 
            
$current = &$parent[$level-1]; 
        } 
    } 
     
    return(
$xml_array); 
}

?>


Bankruptcy Radio Interview

I did a radio interview on BBC Hereford and Worcester this evening talking about my experiences of going bankrupt. I thought it may be a telephone interview, but I went into the studio. This may not sound like much to people, but for me, it was quite a big thing. I am not a great one for public speaking (from what many people have said, I ummed too much at the beginning).

It was very interesting to see how things worked, and it was good to be able to put across how I managed to get into so much debt, and how I tried to reason with banks to pay a smaller amount until I was able to get a more sound financial footing, and subsequently how their greed pushed me into bankruptcy.

I would like to have thought that I have given people the reassurance that going bankrupt is not scary. I would have liked to put across the fact that I had nothing taken from me in the course of my bankruptcy, I got a discount on my petition for bankruptcy because I was receiving family credit (if I recall correctly), and that the hardest and most daunting part was completing the petition (which I did online) which took 45 minutes.

Going into the judge’s chambers and being deemed bankrupt took me all of five minutes.

I have read your petition for bankruptcy and see that bankruptcy is your only real option…

The judge signed a document, and I was led into the deeper bowels of the court where I spoke to the Insolvency Agency which took a few details and that was it. I had a telephone interview with them at a slightly later date, sent them all my bank statements and credit card statements, and that was it! Nobody came around to inspect my assets, and the nicest feeling was that when I got harassed on the phone by the banks, I knew they could do nothing.

Bank: So are you going to pay?

Me: No

Bank: You have an obligation to pay your debts

Me: nope. I dont

Bank: why?

Me: I dont want to

Bank: Thats not a good reason not to pay

Me: Why?

Bank: You have signed a contract stating that yadda yadda yadda (I zoned out at this time)

Me: (noticing that the line was quiet) Oh yeah, I forgot, I was made bankrupt 20 minutes ago.

I gave them the court’s details and that was it! I heard no more from the banks.

Even though I blame myself for getting myself into the mess, I still blame the banks for constantly offering me credit. These banks are:

Halifax, Lloyds TSB, Household Bank, Cahoot, First Direct. They are all evil.

I got a bank account with the Co-Operative Bank a day or so after going bankrupt and never looked back. No credit, and UK call centers! Couldn’t ask for any more…!

I would like to think that people could contact me if they wanted some more information about the bankruptcy process that I went through to help alleviate any fears, so if you did want to, you can email me at andy-at-andydixon-dot-com