Perl Script to Modify Hostname and IP

I have taken on Perl scripting as a directed study. One of the projects going on at work is to take our 32 RaspberryPi’s and turn them into a “super computer” of sorts. We have the master and node images captured, but I have been working on a script that we can run to modify the hostnames and IP addresses of all the nodes post-imaging.

Below is my script which accepts user input for the Hostname and IP.

# This needs to be run with sudo
#
print "Please enter the new hostname: n";
$newName = ;
chomp($newName);

#Attempt to edit the hostname file
$hostFile = "/etc/hostname";

open (FILE1, ">$hostFile") or die "Can't open $hostFile: $! n"; 
print FILE1 "$newNamen";
close FILE1;

print "Hostname file has been modified! n";

#Edit the hosts file and replaces the hostname

$hostsFile = "/etc/hosts";

open(FILE, "<$hostsFile") || die "Can't open $hostsFile: $! n";
my @lines = ;
close(FILE);

my @newlines;
foreach(@lines) {
   $_ =~ s/NodePi01/$newName/g;                       
   push(@newlines,$_);
}
print "Hosts orginal contents have been modified with new Hostname... n";
open(FILE2, ">$hostsFile") || die "Can't open $! n";
print FILE2 @newlines;
close(FILE2);
print "Hosts file has been successfully modified! n";

#Does the IP configuration....
print "Please enter the new IP Address: n";
$newIP = ;
chomp($newIP);
$dhcp = "dchp";
$static = "static";
$ipFile = "/etc/network/interfaces";

open(IP, "<$ipFile") || die "Can't open $ipFile: $! n";
my @lines2 = ;
close(IP);

my @newlines2;
foreach(@lines2) {
   $_ =~ s/iface eth0 inet dhcp/iface eth0 inet $staticn address $newIPn netmask 255.255.255.0n gateway 192.168.0.1n/g;                       
   push(@newlines2,$_);
}
print "Interface file orginal contents have been modified with new settings... n";
open(FILE2, ">$ipFile") || die "Can't open $ipFile $! n";
print FILE2 @newlines2;
close(FILE2);
print "IP Address and network settings have been successfully modified! n";

#Code to reboot the node 
$reboot = '/sbin/init 6';
print "You must reboot to apply changes, do you want to reboot now? (yes/no)n";
chomp($input = );

if($input eq "yes") {
print "Now rebooting ....n";
system $reboot;
	}
else{
print "Bye !n";
}
Continue Reading

Automatic Ad-Hoc in Windows 7

This How-To will assume you have marginal experience in navigating the Windows 7 operating system. First I will cover setting up the host machine, then I will cover setting up the clients that connect to it.

Setting up the Host:

  1. Decide on a network name, security type, encryption type and the key of the ad-hoc network.
  2. Disconnect your connection to any wireless network and switch off/disable your wireless device. Turn on/enable your wireless device but don’t connect to any network.
  3. Click on the network icon in the taskbar.
  4. Click Open Network and Sharing Center > Manage Wireless Networks > Add.
  5. Click “Create an ad hoc network”.
  6. Read the information and click next.
  7. Enter the network name (SSID), security type, encryption type (if any) and the key that you decided in step 1. Make sure you check the box to “Save this network”!
  8. The ad hoc network will then be created.

Now that the host has been setup with the ad hoc network, we must configure it to broadcast that network at login in order for clients to be able to connect to it. To enable automatic connection:

  1. Launch the Task scheduler.
  2. In the task pane on the left, right click on “Task Scheduler (Local)” and click “Create Task…”
  3. Under the General tab, name the task “Automatic ad hoc”. Adjust Security options to meet your needs if necessary.
  4. Under the Triggers tab, click “New…”
  5. In the New Trigger windows select to begin the task at log on. (Important)
  6. Under the Actions tab, click “New…”
  7. The action should be Start a Program, and the program you want to run is netsh.exe (which can be browsed to).
  8. In the add arguments box, type: “wlan connect NetworkName” (Without the quotes). NetworkName must be substituted with the SSID of your ad-hoc network.
  9. Click Okay.
  10. If you are using a laptop, go to the Conditions tab and uncheck the box that says “Start the task only if the computer is on AC power” That will prevent the connection from starting if using battery power. If you aren’t using a laptop, skip this step.
  11. Click Ok to save the task.
  12. Log out and back in to see if it works.

Now you must set up the clients to connect to the host. Repeat these steps on all the machines that you will connect to the wireless ad hoc network. To connect a client to the host network:

  1. Disconnect your connection to any wireless network and switch off/disable your wireless device. Turn on/enable your wireless device but don’t connect to any network.
  2. Click on the network icon in the taskbar.
  3. Click Open Network and Sharing Center > Manage Wireless Networks > Add.
  4. Click “Manually create a network profile”.
  5. Enter the network name (SSID), security type, encryption type, and the key that you set up on the network host.
  6. Uncheck both checkboxes, “Start this connection automatically” and “Connect even if the network is not broadcasting”.
  7. Click Next > Close. Your connection must appear in the list with the icon suggesting infrastructure (two big screens connected to a tube).
  8. Launch a Command Prompt, type in “netsh wlan set profileparameter name=NetworkName ConnectionType=IBSS” without the quotes, exactly as shown. You must substitute NetworkName with the SSID (network name) of your ad-hoc network set up on the host.
  9. If everything went well, you’ll see that the icon has now changed to an ad-hoc icon (three small screens connected to each other) in the Manage Wireless Networks window.
  10. Try connecting to the ad hoc network. You will notice that you no longer need to enter the network key.
  11. Repeat the task scheduling process above exactly as described to automate the connection process.

You should now be set up with automatically connecting machines using an ad hoc network!

Continue Reading