Latest Entries

Converting OpenSSH keys to PuTTY format via command line

The information on converting to and from PuTTY key formats without starting up the PuTTYgen GUI is quite few and far between, as I’ve found.

The key bit is that PuTTY is available as a package for most Linux distros, and comes with PuTTYgen as well.

To convert your existing key, simply use:

puttygen openssh_private_key -o output_filename.ppk

And if you’d like to generate new OpenSSH and PuTTY keys, use something like:

username=testy
fullname="Testy McTest"
ssh-keygen -t rsa -b 2048 -C "$fullname's Key" -N "" -f $username && \
puttygen $username -o $username.ppk

Which will generate files testy, testy.pub, and testy.ppk.

I’d imagine that the PuTTYgen utility will accept the same command format on Windows as well if you’ve already got your OpenSSH keys handy there.

Create and mount a file as a disk in Linux

If you need to test something where a disk needs to be mounted, but don’t want to go through the hassle of actually attaching a physical disk or provisioning a virtual one you can simply create a ‘loopback’ device in linux.

I’ve been working on project where in the future I might want to export a big pile of disks via NFS, but I don’t feel like provisioning a bunch of temporary devices for it. There are a few other tutorials out there on how to do this, but they either have extraneous commands [ie losetup] or don’t deal with actually creating the image. Continue reading…

Encoding and Decoding Large Numbers in PHP

I found an interesting question on StackOverflow today. Not necessarily because I have any interest in BitCoins [I don't], but because the asker needed to convert the format of some very large numbers. In this case the number turned out to be 58 digits in base 10, and PHP has a tendency to crap out somewhere after 11 to 19 digits.

The first problem I tackled was writing a proper base58 encoding function since the ones floating around the internet seem to be spotty at best. But why limit it to just one encoding? I’ve written the same function a handful of times already, all with different, strange, and sometimes scrambled bases. I’d definitely been doing it wrong.

This is what I came up with:

function arb_encode($num, $basestr) {
	$base = strlen($basestr);
	$rep = '';

	while($num > 0) {
		$rem = $num % $base;
		$rep = $basestr[$rem] . $rep;
		$num = ($num - $rem) / $base;
	}
	return $rep;
}

Continue reading…

Copy and Paste Files Between SSH Sessions

I’ve found that the permissions in the directories on the servers I’ve been working on recently are not very friendly to using scp or rsync, [root owns the dir, but sshd PermitRootLogin = No] but I need to copy files around regularly. I’ve used a simple cat file | base64 to embed file contents in scripts before, so why not pair it up with tar to move many files?

I’ll save the sob story where I found that tar by default pads with a LOT of null bytes, but that’s why -z and -b 1 are your friends.
Continue reading…

SSH to an EC2 instance Without Knowing the Hostname

If you’re not ponying up an extra few bucks per month for an EC2 Elastic IP it can be a pain in the ass to reach your EC2 instances, particularly after they’ve rebooted and gotten a new IP and hostname. Fortunately it is possible, though not obvious, to retrieve the public DNS name of an EC2 instance based on its Instance ID and Availability Zone.

To this end I’ve created two scripts, a Python script [ec2-get-dns] that uses boto to retrieve the hostname, and a bash script [ec2-ssh] that gets the hostname and connects via SSH.

Shortcuts

Oh wait, you say it’s a pain in the ass to remember instance IDs? Really? I always know that i-35213355 is the web server and i-54632856 is the database server, but OK. This also reads named shortcuts stored in a file [.ec2list] in your home directory.

#shortcut-name:region:instance-id[:username]
web-server:us-west-2:i-35213355
db-server:us-west-2:i-54632856:db-user

As you can see above the username is optional. Connecting to the ‘web-server’ entry will default to ‘ec2-user’ while ‘db-server’ will use ‘db-user’. Continue reading…



Copyright © 2009–2010. All rights reserved.

RSS Feed.