Find my (R)Pi

March 17, 2014 at 8:42 pm (computers) (, , , )

I like tinkering with my Raspberry Pi, but I also tend to have them configured for remote SSH to save dragging out a keyboard and screen. I also use the small Edimax WiFi USB dongles, but I also just like to let the pi find its own address on the network using DHCP … so I often need to either hook up to my routers admin/status page to see what nodes it has found or just attempt to connect to a few nodes in the range I know will be used for the pi.

Eventually I got fed up with doing this, so instead I wrote a very simple perl script to use Net::Ping to scan a small range of addresses and report on which ones are responding, and then see if port 22 (SSH) or 80 (web) are open.  Then I know where my pi is.

Net::Ping can use the tcp for ping, which has the advantage that you don’t need privileges for the script to use icmp.  The disadvantage is that most things won’t respond to the tcp echo port.  Consequently, I use Net::Ping in two modes – first with icmp to see if the host is alive, then with tcp to probe the ports I’m interested in.  This does require it to be run with administrator privileges, but that is fine for me.

I don’t scan a whole range, that would take too long for this very simplistic case, so I just scan 20 or so addresses that I am interested in.

There are a wealth of network scanning and management applications out there, but this has the advantage of being very simple.  Just what I needed.  Below is the code.  Nothing exciting – about as simple as it gets, but it works. 

To run from Windows, I use a simple batch file to call the perl interpreter with the script, then pause afterwards (to give me a chance to read the output).  This can then be used by right-click -> run as administrator.

Kevin

#!/usr/bin/perl -w
use Net::Ping;

my $subnet="192.168.0.";
my @ports = (22, 80);
my $timeout = 1;

my $p_icmp = Net::Ping->new ("icmp", $timeout) or die "Can't start icmp ping";
my $p_tcp = Net::Ping->new ("tcp", $timeout) or die "Can't start tcp ping";
for ($i = 50; $i<70; $i++) {
    my $host = $subnet.$i;
    print "$host: ";
    if ($p_icmp->ping ($host)) {
        print "ok ";
        foreach my $port (@ports) {
            $p_tcp->port_number($port);
            if ($p_tcp->ping ($host)) {
                print "$port ";
            }
        }
        print "\n";
    } else {
        print "nok\n";
    }
}
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: