Thursday, February 08, 2007

MAC address gathering fun!

#! /bin/bash
# MAC address collection script

MAC=`ifconfig | grep HWaddr | awk -F 'HWaddr ' '{ print $2 }'`

echo "$HOSTNAME,$MAC,$*" | ssh administrator@ 'cat >> macaddresses.csv'

EDIT:Oops, I didn't add line breaks at first!

Problem: You need to get a list of MAC addresses for a bunch of computers. You can't remotely connect to them because their WINS service is not properly set up to allow you to connect to them by hostname and their IP addresses are assigned randomly by DHCP. A lot of them actually share a hostname because spending the time to name them properly didn't seem to be a useful way to spend an hour when the WINS service isn't working anyhow.

This will unfortunately involve some leg-work. You know how to look up the MAC address at each station, but you don't want to have to write out every MAC address by hand and then type it back in later. (You're lazy, what can you say?)

Solution:The above script! Keep it in /var/www (apache2's web root directory on Ubuntu) of your server, make sure to use YOUR server's ip address. Then you can just:

  1. Log in to a station.
  2. Bring up a terminal.
  3. $ wget
  4. $ bash macgather identifying comment here (wget'ting a file removes execution privileges, pointing bash at the script is faster than chmod'ing and running it. If the station's hostname is unique and helpful in identifying it, you don't really need to add the identifying comment.)
  5. Answer a couple of prompts ("yes" you really do want to connect to the server, and enter the server's "administrator" account password).
  6. Log out.
  7. Lather, rinse, and repeat on every workstation you need.

After you've had a nice tour of where all the stations are (several classrooms in my case) and ran the script on each computer, you'll have a nice comma-delimited file that you can manipulate in just about any spreadsheet program you got. It'll have three columns: the first will be the stations' hostname, the second will be the stations' MAC address, and the third will be the identifying comment you supplied (if any) at that station.

Yeah, I know...

This will likely not be useful to anyone but me, but I spent a while figuring out just the right commands to do this--with the help of my local linux user group's mailing list--so I'm recording it for posterity.

powered by performancing firefox


Blogger Carlos said...

If you want to append to the macaddresses.csv file, I think you need to change the cat command to 'cat >> macaddreses.csv'

2:47:00 PM  
Blogger Simón A. Ruiz said...

You're absolutely right, and that's what I had. Let me edit that by hand, performancing isn't quite translating all the characters to HTML for the post correctly.

Thanks for the heads up!

8:19:00 AM  

Post a Comment

Links to this post:

Create a Link

<< Home