Omnis Technical Note TNLI0005 December 2009

How to determine the MAC address under Linux

For Omnis Studio 4/5
By Dr Michael Hufschmidt

The network card in every individual computer has its own unique 48 bit (6 bytes) long identification number, called the Media Access Control address (MAC address). The MAC address can be used to uniquely identify a computer, so you could, for example, use it to implement a copy protection scheme for your application. In this case, you could read the MAC address of the computer, and encrypt it together with the Omnis runtime serial number to create a unique identifier to protect your application.

Under Windows you can determine the MAC address with a DLL call, and under MAC OS X either with a modified Shell script or with an AppleScript. This tech note describes how you can read the MAC address under Linux.

How to determine the MAC address

Under Linux you can use the command 'ifconfig' to determine many attributes of network interfaces. This command is normally located in the system directory '/sbin' and in many Linux distributions is hidden to the normal user. The return of '/sbin/ifconfig' contains several lines of information, for example:

Eth0 Protocol:Ethernet Hardware Address 00:50:8D:B3:24:56
inet Address:192.168.0.4 Bcast:192.168.0.255
Mask:255.255.255.0
inet6 Address: fe80::250:8dff:feb3:2456/64
G¸ltigkeitsbereich:Verbindung
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:42845 errors:0 dropped:0 overruns:0 frame:0
TX packets:27906 errors:0 dropped:0 overruns:0
carrier:0
collisions:0 Sendewarteschlangenl‰nge:1000
RX bytes:34662307 (33.0 Mb) TX bytes:3442994 (3.2 Mb)
Interrupt:247 BasisAddress:0x2000

The MAC address is at the end of the first line, the interface eth0. To extract it you can use the following Shell script:

#! /bin/bash
PATH=$PATH:/sbin
ifconfig eth0 | grep 'Hardware' | awk
'{ print $5 }'

The first part of the command fetches the complete network information, the second part of the command extracts the line with the keyword 'hardware', and the third part extracts the 5th parameter.

The Omnis method call

In Omnis you can call a Shell script with the 'Launch program' command. In the following code example the example Shell script is first created dynamically and made executable. Then it is called and its return value stored in the variable 'address'. Lastly, the script is deleted, in order to cover all tracks. The variables script, scriptfile, address, and command are of the type Char, the variable called file is an instance of the FileOps class.

Calculate script as con(
'#!/bin/bash',kLf,'PATH=$PATH:/sbin',kLf,
"ifconfig eth0 | grep 'Hardware' | awk '{ print $5 }'",kLf)
Calculate scriptfile as '/tmp/get_mac_adress.sh'
Do file.$createfile(scriptfile)
Do file.$writefile(script)
Do file.$closefile()
Calculate command as con('chmod 700 ',scriptfile)
Launch program [command]
;; make it executable
Launch program [scriptfile] Returns address (Do not quit Omnis)
Calculate address as left(address,17)
;; drop any erroneous chars after the address
Do FileOps.$deletefile(scriptfile)