#!/usr/bin/perl 
#*************************************************************************
#
#   Program:    getmac
#   File:       getmac.pl
#   
#   Version:    V1.0
#   Date:       08.07.13
#   Function:   Get the first MAC address for a machine
#   
#   Copyright:  (c) Dr. Andrew C. R. Martin, UCL, 2013
#   Author:     Dr. Andrew C. R. Martin
#   Address:    Institute of Structural and Molecular Biology
#               Division of Biosciences
#               University College
#               Gower Street
#               London
#               WC1E 6BT
#   EMail:      andrew@bioinf.org.uk
#               
#*************************************************************************
#
#   This program is not in the public domain, but it may be copied
#   according to the conditions laid out in the accompanying file
#   COPYING.DOC
#
#   The code may be modified as required, but any modifications must be
#   documented so that the person responsible can be identified. If 
#   someone else breaks this code, I don't want to be blamed for code 
#   that does not work! 
#
#   The code may not be sold commercially or included as part of a 
#   commercial product except as described in the file COPYING.DOC.
#
#*************************************************************************
#
#   Description:
#   ============
#
#*************************************************************************
#
#   Usage:
#   ======
#
#*************************************************************************
#
#   Revision History:
#   =================
#   V1.0    08.07.13   Original   By: ACRM
#
#*************************************************************************
use strict;


$::ifconfig = "/sbin/ifconfig";

my $mac  = macaddr::ReadMAC();
print "$mac\n";

package macaddr;
#*************************************************************************
#
#   Program:    Abysis
#   File:       macaddr.pm
#   
#   Version:    V1.1
#   Date:       18.10.13
#   Function:   Obtain MAC addresses for this machine
#   
#   Copyright:  (c) Dr. Andrew C. R. Martin, UCL, 2013
#   Author:     Dr. Andrew C. R. Martin
#   Address:    Institute of Structural and Molecular Biology
#               Division of Biosciences
#               University College
#               Gower Street
#               London
#               WC1E 6BT
#   EMail:      andrew@bioinf.org.uk
#               
#*************************************************************************
#
#   This program is not in the public domain, but it may be copied
#   according to the conditions laid out in the accompanying file
#   COPYING.DOC
#
#   The code may be modified as required, but any modifications must be
#   documented so that the person responsible can be identified. If 
#   someone else breaks this code, I don't want to be blamed for code 
#   that does not work! 
#
#   The code may not be sold commercially or included as part of a 
#   commercial product except as described in the file COPYING.DOC.
#
#*************************************************************************
#
#   Description:
#   ============
#   Provides simple authentication of a licence file for Abysis
#
#*************************************************************************
#
#   Usage:
#   ======
#
#*************************************************************************
#
#   Revision History:
#   =================
#   V1.0   18.10.13  Original moved out of auth.pm and modified  By: ACRM
#
#*************************************************************************
# $mac = ReadMAC();
# -----------------
# Obtains the MAC address for the first Ethernet port by using the 
# ifconfig command. Deals with ifconfig on Fedora 17 and CentOS 5 and 6
#
# 05.07.13 Original   By: ACRM
# 08.07.13 Modified for Fedora 17 and upcases MAC address
# 18.07.13 Now calls ReadMACs() and just returns the first one
#
sub ReadMAC
{
    my @macs = ReadMACs();
    return($macs[0]);
}


#*************************************************************************
# @macs = ReadMACs();
# ------------------
# Obtains an array of all MAC address using the ifconfig command.
# Deals with ifconfig on Fedora 17 and CentOS 5 and 6 and BSD
#
# 29.07.13 Original based on ReadMAC()   By: ACRM
# 18.10.13 Handles BSD and MAC-OSX and removes MAC addresses of
#          00:00:00:00:00:00, FE:FF:FF:FF:FF:FF, FF:FF:FF:FF:FF:FF
#
sub ReadMACs
{
    # CentOS4, CentOS5, CentOS6, Fedora 14
    my $ifcfg = `$::ifconfig | grep -i ethernet | grep -i hwaddr`;
    chomp $ifcfg;

    if($ifcfg eq "")
    {
        # Fedora 17+
        $ifcfg = `$::ifconfig | grep -i ethernet | grep -i ' ether '`;
        chomp $ifcfg;

        if($ifcfg eq "")
        {
            # BSD
            $ifcfg = `$::ifconfig | grep -i 'lladdr '`;
            chomp $ifcfg;

            if($ifcfg eq "")
            {
                # MAC
                $ifcfg = `$::ifconfig | grep -i 'ether '`;
                chomp $ifcfg;
            }
        }

    }

    my @macs = ();
    while($ifcfg =~ /(..:..:..:..:..:..)/)
    {
        my $mac = $1;
        $mac = "\U$mac";
        if(($mac ne "00:00:00:00:00:00") &&
           ($mac ne "FE:FF:FF:FF:FF:FF") &&
           ($mac ne "FF:FF:FF:FF:FF:FF"))
        {
            push @macs, $mac;
        }
        $ifcfg =~ s/..:..:..:..:..:..//;
    }
    return(@macs);
}


1;
