Apr 072013
 

For our production process (for the upcoming Raspberry Pi media centre edition in a nifty transparent red case) we need to mass-extract the Broadcom SoC’s serial numbers. Doing this manually would be a chore for 50 Raspberry Pis, so we came up with a quick & dirty solution in Ruby.

We’d like to share that with you guys – the code is released into the public domain. 

Download the script:

Setup

karthikeyan_ruby_flatmix

The script is supposed to be run on a master Raspberry Pi. You need to install some prerequisites (nmap, Ruby, Ruby gems) for it to be able to run:

aptitude install ruby
aptitude install nmap

gem install nmap-parser
gem install net-ssh

The script is written in Ruby. Other languages (e.g. Python) could be used, too, of course. I just feel really at home with Ruby. nmap is used to scan the network (you can even scan for ports, etc.). Have a look at this site for some further information about nmap usage to scan a network.

The gems are Ruby extension packages. Ruby has its own package manager “gem” which will download and setup the packages for you. nmap-parser is a wrapper and interface for nmap, net-ssh is an interface to SSH.

Unzip the script, put it into a new folder and make it executable ( chmod +x getips-v1.rb ).

Edit the script to reflect your network (line 35 – replace 192.168.1.0/24 with the network range you want to scan), the expected amount of Raspberry Pis per batch (line 8) you will be processing.

Edit the SSH part to reflect the password setup of your Raspberry Pis (here defaulting to “pi” / “raspberry”) and what should be done on them (line 62 ff).

If you really want to, you could uncomment the “espeak” line (line 12) to enable speech output from your Raspberry as a prompt to setup the next batch, etc. I have disabled it after some experimenting – the script uses a mix of straight “puts” and “speak” (which in turn will also use puts to output to the default output) – you would need to do some more editing for this to be really consistent.

Usage

Start the script ( ./getips-v1.rb ) – it uses the shebang notation, so ruby will be called automagically to parse and execute the script.

The script will show you your master Raspberry’s serial and IP adress – it’s IP and serial will be ignored from now on (no processing done on it).

Afterwards it will enter into an infinite loop (which you can terminate by entering q or x + enter when prompted, or Ctrl + C at any point). It will scan your network for Raspberry Pis (determined by the MAC vendor – so be sure to attach your Raspberrys via their own LAN port, not WiFi – or modify it to work!). If less Pis are found than expected (default is 7), it offers you to rescan the network – allowing for additional Raspberrys to come up if they did not boot fast enough during the first scan. If you want, you can skip the rescan by entering n + enter when prompted.

After the scan, it will connect to every Raspberry in turn, login via SSH with the default password and user (pi / raspberry), get the SoC serial, halt the Raspberry and disconnect.

The serials will be written to a file which will be saved to the same directory.

The serial extraction is just a sample use, of course. You could use it to install packages, modify configuration files, and much, much more.

After having finished the tasks you requested, it will offer you to quit the script (q or x + enter) or read another batch. It will loop forever if you want to.

For more sophisticated requirements

We recommend you to look into Capistrano or similar tools for batch automation. Capistrano is also Ruby based, and uses a “DSL” to describe the automation tasks.

Feel free to contact us, if you need it custom tailored to your requirements – we charge fair rates, especially if the result can be released as open source.

Optimization WordPress Plugins & Solutions by W3 EDGE