Finding my computer

A couple of days ago I replaced my computer at work with a new one. This morning, working from home, when I tried to log in, it didn't respond.

I realized promptly that I had forgotten to ask the sys admin to update the MAC address on the DHCP server and so it just got some random IP from the pool.

Fortunately, as I had copied my home directory from the old machine, the public key from my home computer was already there. All I had to do was to go over the 512 IPs allocated to the office network and find out on which one I was allowed to log in.

It didn't took me too long to write a script to do that using Net::OpenSSH::Parallel (probably one of my most underrated modules):


use strict;
use warnings;
use 5.010;

use Net::OpenSSH::Parallel;
use Socket;

my $net = shift // die "network missing\n";

my ($ip, $mask) = $net =~ m|^([^/]*)(?:/(\d+))?$|;
my $iaddr = unpack(N => inet_aton($ip));

$mask ||= 24;
$mask < 20 and die "network too big\n";
my $imask = (1 << (32 - $mask)) - 1;
$iaddr &= ~$imask;

my $pssh = Net::OpenSSH::Parallel->new(connections => 64,
reconnections => 0);

for my $i (0..$imask) {
my $host = inet_ntoa(pack(N => $iaddr | $i));
$pssh->add_host($host,
master_stderr_discard => 1,
timeout => 20,
master_opts => ['-oStrictHostKeyChecking=no',
'-oUserKnownHostsFile=/dev/null']);
}

$pssh->all(cmd => 'echo `hostname` "=>" %HOST%');
$pssh->run;

Full version here.

Leave a comment

About Salvador Fandiño

user-pic yaph