Tuesday, March 19, 2013

Using Bash scripting to automate Network Administrative Tasks

Automating repetitive tasks is an essential skill for any Systems Administrator or Network Administrator.  The following script is a trick I have used for years to push configuration commands to network switches or other network devices that have a telnet interface.  (Yes, there is an assumed risk of the plain text telnet password.)  If you have multiple devices that need the same configuration, you can push these changes to them via a loop in a bash script.  This script accepts input from a text file and then echoes a series of commands to the network device.  The following is an example in which we need to push an SNMP community and access control list to a series of Cisco switches.

#!/bin/sh
cat switches.txt | while read SWITCH
do(
        sleep 1
        echo myusername   
        sleep 1
        echo P@55w0rd
        sleep 1
        echo en
        sleep 1
        echo P@ssword
        sleep 1
        echo config t
        sleep 1
        echo access-list 41 permit 10.10.10.1
        sleep 1
        echo access-list 41 permit 192.168.0.1
        sleep 1
        echo snmp-server community myCommunity RW 41
        sleep 1
        echo wr
) | telnet $SWITCH
done

First the script reads the switch IP address from a text file (the password could be read in too).  The script then uses the "echo" command to send the configuration to the telnet command.  You will see the first several commands initiate the log in and then invoke enable mode (en).  The sleep statements in between each command give the switch time to process the previous command before receiving the subsequent one.  Finally, the script then enters into the configuration terminal (config t) creates an access list with entries, and then assigns it to a read/write SNMP community.  The switches.txt file would have one IP address per line for each switch that would need this configuration.  If you wanted to read in other information like a password, just tab separate the data and another variable after SWITCH in the cat switches.txt... line.  I have also used this method to periodically reboot Blackboard Vending units as well as push configurations to them and to tftp switch configurations back to a storage server.

The above script should work on any Linux machine including a Raspberry Pi, which would make a very inexpensive server to run this script and other similar scripts.





Monday, March 18, 2013

Running a script as a service on Raspbian Linux

If you followed the instructions on Setting up a webcam on the Raspberry Pi, you may also want your feed to launch automatically.  The following process can also be used to run any script at boot after all other services have launched.

These instructions assume you have created broadcast.sh as instructed here.  To launch another script, just use appropriate path and script name.
rc.local script modified to launch broadcast.sh on boot

  1. Open a linux terminal
  2. sudo nano /etc/rc.local
  3. Arrow down to the "fi" of the if statement that prints the address.
  4. Hit enter and then the up arrow.
  5. type "/home/pi/broadcast.sh&" without the quotes-- Note the Ampersand "&" is important.  This allows the rc.local script to launch your script and continue to booting without hanging and waiting for your script to complete.
  6. press ctrl+x and hit "Y" to save.
Now your broadcast will start automatically when the Pi reboots.

Dr. Soriano has the live feed up and running.  The Pi webcam stream is monitoring his Chemical Ecology experiments with the cockroaches and they can be viewed here.

Sunday, March 17, 2013

PERL and Mandelbrot Fractals

Mandelbrot Fractal created on a single Raspberry Pi
As I continue to research parallel computing methods on the raspberry pi cluster, I have been investigating fractal generation, specifically Mandelbrot fractals.  In the process, I found an example PERL script that generated a Mandelbrot fractal.  I wish I had the original link, but I have lost it.  I have rewritten it several times over in an attempt to both understand and simplify the process before I attempt parallelizing it.  You will notice in the image above that I chose to add random colors to the interior instead of making it a solid color.  The current phase of the code can be found here.  I will be working on adding the parallel components to it this week and will add a post with the code when it has been completed.

Friday, March 15, 2013

Migrating File Shares to a New Server

I recently needed to perform a physical to virtual migration (P2V) of  a file server.  I tried using my P2V software to accomplish this, but the virtual server would not boot after migration.  So the question became what do you do when its time to upgrade to a new file server or want to migrate to a virtual server without expensive P2V software.  I have used this method in the past when upgrading a physical file server and it served me well for this process as well.

If your file server is on a domain and all you need are the files, shares, and permissions this can be accomplished rather easily. This process should of course be performed during a scheduled downtime or maintenance windows and the new server needs to be configured with the same drive letters and file paths as the old server.  First, simply backup your files and restore them to the corresponding drive(s) on the new server.  I like to start this process a day or two before the actual migration.  I perform a full image backup of the shares and restore it to the new server ahead of time and follow up with a differential backup and restore at the time of migration. This reduces the time required to complete the migration dramatically.  When performing your backups, include file permissions and disk quotas where applicable.

Next you export two registry keys from the old server and import them into the new server. (This process can be done prior to migration as well as long as no new shares or permissions will be adjusted prior to migration). These keys are SYSTEM\CurrentControlSet\Services\LanmanServer\Shares and SYSTEM\CurrentControlSet\Services\LanmanServer\Shares\Security (more information here)
LanmanServer\Shares Registry Key
Reboot the new server and voila your shares are up and running on the new server.

Finally during your scheduled maintenance window, shutdown the old server.  Then rename and readdress the new server to use the old servers information and you are done.  Alternatively if the the new server needs to have a different name and/or IP address, update your log in scripts to point to the new server.

Thursday, March 14, 2013

Pi Cubed: Calculating Pi on 32 Pi's on Pi Day

Pi Calculated using the Monte Carlo Method and Visualized using GD
In celebration of Pi day I am posting my code for calculating Pi utilizing the MPI libraries in PERL.  This script calculates Pi utilizing the Monte Carlo method (further information here).  I have taken this common calculation and incorporated the GD module to visualize the results for the student. Each member performs a set of calculations and then creates a transparent image plotting the points it calculated.  Points within the circle are blue, those outside are red.  Each member then copies its image to the master where they are merged.  Examples of each members image can be seen below and the final image is posted above.  Code for this project can be found here.

Points plotted on Node27
Points Plotted on Node 30

Raspberry Pi Cam

 

Here is another Raspberry Pi project that I have been working on. Dave Soriano will be using it to conduct Chemical Ecology experiments testing attractants on cockroaches. The pi is in the red case on the left and connected to a webcam overtop of the aquarium. An attractant will be placed in the base and holes have been drilled in the bottom to allow vapors to pass through to the cockroach.

The Pi is setup to stream to bambuser.com using a webcam and ffmpeg. For anyone interested in streaming from a Pi, instructions can be found here.