Create random password in a shell script

Here’s a quick one-liner to make up a random password using only shell commands.

MYPASSWORD=`cat /dev/urandom | tr -dc a-z0-9 | head -c 10`

If you want to customize this a bit, there are two parts you can change, first is the filter which specified what range of characters are allowed in the password. a-z0-9 restricts the password to lower-case letters and digits only. You can add extra ranges and characters here, for example, to add a few punctuation characters, try a-z0-9$^&

The other customization is the length of the password which is the number at the end. In my case, the password will be 10 characters long, you can change it to anything you like.

Quick and dirty fix for corrupted mailbox

I run an old mail server using exim4 which stores its files in mbox format.

My inbox isn’t exactly small these days and I get a large volume of mail, mostly spam unfortunately. Sometimes exim4 has a glitch and corrupts the mailbox. The symptoms are always the same, the start of the mbox file is messed up with some random binary garbage. It always occurs at the very start of the file like this example:

LÍfO@+Yðe
~Ó¹ôêÞ:[çknñPùJ꣺U´-ëüµOi;õ¨ÿ-R$!8öµD¨%wÇßC`
(MʱÔ)wiÎ}1Þméõx¤¾GÊ6DFx£MVLZ¦B{TåQ}­ÃÈÍ ©F!¿ã¶Ùìããé!÷EúýD»Ü>W~sÃ.131.81])
        by adamserver with esmtp (Exim 4.69)
        (envelope-from )
        id 1O7eM3-000229-SH
        for adam@siliconsparrow.com; Fri, 30 Apr 2010 10:45:20 +1000
Received: by p14.nnj.simupt.com (Postfix, from userid 33)
        id 41A4CA7E0; Fri, 30 Apr 2010 00:36:38 +0000 (UTC)
To: adam@siliconsparrow.com
Subject: Hello
...

Now trying to edit a 2GB mbox file using vi or nano is akin to torture so here’s my quick and dirty method which simply deletes the first message in the inbox using the commandline tool awk. I have used this technique 5 or 6 times in the last year with success.

NOTE: Make sure you shut down your SMTP server, POP3 and IMAP servers before you run this.

cd /var/spool/mail
mv adam adam-corrupt
tail -c +500 adam-corrupt | awk '/^From /,0' > adam
chown adam adam

How to cycle through the values of an Enum in Java

Here’s a quick little trick. I needed to advance my variable to the next value in an Enum. I found this can be done using the values and ordinal built-in members. This version wraps around to the beginning when you reach the end.

Here’s the code:

private enum Direction { NORTH, EAST, SOUTH, WEST;
    public Direction getNext() {
        return values()[(ordinal() + 1) % values().length];
    }
};

Here’s an example of how to use it:

private Direction currentDirection = Direction.NORTH;

public void turn() {
    currentDirection = currentDirection.getNext();
}

Steve the Weightlifter

SteveMy kids are heavily into Minecraft at the moment. Since I don’t have much to write about this week, let’s have a look at the thing that impressed me most about this game – the amazing weight-lifting skills of the game’s main character, Steve.

Steve is a pretty strong guy. Not only can he smash through trees and mountains with his bare fists, he’s always strolling around carrying big blocks of stone, whole trees and a small armoury of tools and weapons. Just for laughs, let’s work out the maximum weight he can carry.

Minecraft Inventory ScreenAll the stuff Steve is currently carrying is shown in his personal inventory.

Steve’s personal inventory has 36 slots and each slot can hold up to 64 items, this means he can carry a maximum of 2304 items.

So what is the heaviest item he can carry? Well the heaviest material in the game is gold so how about a cubic metre of gold? How much would that weigh?

Gold has a density of 19.3 grams per cubic centimetre, there are a million cubic centimetres in a cubic metre. So a cubic metre of gold would weigh 19.3 metric tonnes.

Just one of these gold blocks would defeat (and squash flat) the world’s best weightlifters. The current world weight lifting record stands at 263kg.

But Steve can carry 2304 items. If each gold block weighs 19.3 tonnes, that’s a total of 44,467 tonnes!

And even with all that weight in his pockets, it doesn’t even slow him down.

All that gold by the way would be worth about $2.6 trillion at today’s prices, you could probably buy Norway for that amount of cash, and Steve wouldn’t have to worry about anyone stealing it as nobody could possibly lift it.

An Olympic gold medal only contains 6 grams of gold, so you could make 7.4 billion of them by emptying Steve’s pockets. That’s an Olympic gold medal for everyone on the planet.

Hyperlinks for your twitter posts

Here’s a quick Javascript to take a string and automatically add hyperlinks to any twitter tags it sees. This could be useful if you were writing a web page for users to enter tweets. It will take a string such as “I gave @siliconsparrow a hug”, detect the at-tag and wrap it in the appropriate hyperlink.

Here’s a demo page where you can see it in action.

function addTwitterLinks(text) {
	return text.replace(/[@#]([a-zA-z0-9_]*)/g,
		function(m,m1) {
			var t = '<a href="http://twitter.com/';
			if(m.charAt(0) == '#')
				t += 'hashtag/';
			return t + encodeURI(m1) + '" target="_blank">' + m + '</a>';
		});
}

More design refinements to the Baud Rate Converter

The latest batch of RS232 Bit Rate Converters have a few improvements over the previous releases. The new model is called RS004. I’ve actually been selling these for a few months now so anyone who ordered some recently already has these new features:

  • Programmable time-delay in each direction so you can delay your packets.
  • Support for an even wider variety of baud rates from as low as 11bps up to 120,000bps – check the baud rate calculator
  • Improved power efficiency – It now only draws 25mA when idle, a bit more than that when busy but much less than the 120mA consumed by the previous design
  • Reverse polarity protection in case you accidentally wire the power backwards
  • I now outsource the circuit board manufacturing so I don’t have to build them myself. The boards are robot-assembled which results in a much neater build with less mistakes.

New units are now ready for sale, head on over to serialgadget.com for more detail.

New Toy

In the past I’ve written a couple of posts on how to get better magnification out of a digital camera. Well, you can get some pretty reasonably priced digital microscopes these days – and now I have one, the DigiTech QC3247.

It has two modes, a 20X mode which is fairly useful and a 400X mode which is ridiculous.

Here’s a capture from 20X mode. To give you an idea of scale, the brown chip capacitors in the picture are 0603 – ah, that is 1.6mm long and 0.8mm wide. Click on the picture to get the full-res 800×600 image.

Now here’s a capture of one of those same chip capacitors at 400X. It took me ages to position and focus it as the frame is less than 1mm from side to side. This mode is of limited use I think unless I build a mount for the microscope. It is impossible to hand-hold it at this magnification and the supplied stand is too flimsy to hold it in position without wobbling.

All in all, this will be a nice addition to my toolbox.

Class D Amplifier is Working

A few posts ago I mentioned that I was having trouble with my Class-D amplifier design. It’s been sitting on the shelf for a few weeks and I think it’s time to have another look at it.

It’s amazing how you can notice new things when you take a fresh look at something. The problem is obvious now. I was missing a ground trace which introduced some noise into the input. Also the input impedance was way too high which made the amp very sensitive to that noise. The result was some nasty buzzing and it had nothing to do with the power supply as I first thought.

So now it’s working. Yay! I also upgraded it to the Texas Instruments TPA3110 which is completely pin-compatible and has 15 Watts of power. My ears are suitably hurting after running some tests at full volume and it didn’t even get warm.

So next I need to correct the PCB in light of these changes and it is done.