Sunday, September 4, 2011

MAC address using c

Mac Address-->from Wikipedia
A Media Access Control address (MAC address) is a unique identifier assigned to network interfaces for communications on the physical network segment. MAC addresses are used for numerous network technologies and most IEEE 802 network technologies including Ethernet. Logically, MAC addresses are used in the Media Access Control protocol sub-layer of the OSI reference model.
MAC addresses are most often assigned by the manufacturer of a network interface card (NIC) and are stored in its hardware, the card's read-only memory, or some other firmware mechanism. If assigned by the manufacturer, a MAC address usually encodes the manufacturer's registered identification number and may be referred to as the burned-in address. It may also be known as an Ethernet hardware address (EHA), hardware address or physical address. A network node may have multiple NICs and will then have one unique MAC address per NIC.

Now the question is How can we find the Mac Address using C language....
There may be many methods to do that but  i am using the sytem() function which is used to run the DOS command using C and the FILE handling to get the MAC address or pghysical address of the pc...

As we know we can get the physical or MAC address using the DOS command "ipconfig/all" ..u can get the mac address using this command from the dos prompt as shown in the figure below..
As you can se in the figure the Physical Address is 00-1C-C0-1A-6E-C0
Now mout ask is only to show this address as theoutput of our C program....
What firstly Strike in the mind,the idea is :
"To get the ipconfig/all command executed using sytem funtion and redirecting the output to the string or taking the output to the string and then getting the substring form that main string starting from the Physical Address that will be easy and simple....."
I also look for that and tried but was not able to do the same....any help in this regard will be appriciated
now what i have done to get the only MAC address or the physical addres,i just rediredced the output of the system funtion() with the inconfig/all command to a file ,read that file back line by line which is very necessary then search for the substring starting with Physical Address or containg the Physical Address,get that printed and get out of the loop using break....
You can see the code below...
#include <stdio.h>
#include<conio.h>
int main ()
{
  FILE *fp;
  clrscr();
  system ("ipconfig/all>f:\macid.txt");
   fp=fopen("f:\macid.txt","r");
   if(fp!=NULL)
   {
   char line[128];
   while(fgets(line,sizeof line,fp)!=NULL)
   {
   char *nwln=strchr(line,'\n');
   char *ptr;
   if(nwln!=NULL)
   *nwln='\0';
   ptr=strstr(line,"Physical Address");
   if(ptr!=NULL)
   {
   printf("%s\n",ptr);
   break;
   }
  }
   }
  getch();
  return 0;
} 
NOTE:Run the program using DOS Prompt instead of turboc ide....
Your comments and query are invited .....

3 comments:

Feel free to comment......