Find my (R)Pi

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

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

Permalink Leave a Comment

How Many Notes in Mozart’s Requiem?

December 7, 2009 at 9:14 pm (computers, music) (, , , )

When some bright spark suggests a ‘how many notes in the score’ competition, I thought what piece could be better than Mozart’s Requiem?  4 choral parts, orchestra, I could have the vocal score on hand for reference and estimating, etc, etc.  One problem – just how many notes are then in Mozart’s Requiem?

Well, when Google failed me, and I counted how many notes in one bar of the score (more than 40), I decided extrapolating from a sample of bars to the whole piece was impractical, I thought I’d look for help of a more technical kind.

Googling eventually found me a site with the complete Requiem as a series of MIDI files.  Great – thought I’d just load them into a sequencer and that would do the job.  But no, the sequencer’s I have and tried didn’t have a ‘total notes’ statistic anywhere that I could find.

Well, this is the point where the geek takes over … MIDI is just a series of instructions ‘turn this note on, turn this note off, change to violin here’ and so on.  So, armed with the MIDI files, I just have to find something that would count the ‘note on’ events in each track in each file.  Well, after a bit of playing around, and looking for help, I found the MIDI perl library.  This enabled the following script (stored here in case someone else has the same whimsical, insane idea and needs to do something similar).

#!/usr/bin/perl
use MIDI;

my $opus = MIDI::Opus->new({'from_file' => $ARGV[0]});
my $notes = 0;
my @tracks = $opus->tracks();
foreach my $track (@tracks)
{
   foreach my $event ($track->events())
   {
      if ($event->[0] eq 'note_on')
      {
         $notes++;
      }
   }
}
print "Note count: $notes\n";

And the number of notes? Ah, well that would be telling … lets just say that whoever said Mozart used ‘too many notes’ really just didn’t know what they were talking about …

Kevin.

Permalink 4 Comments