LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-15-2010, 07:37 AM   #1
cricketlong
Member
 
Registered: Dec 2008
Posts: 52

Rep: Reputation: 15
How to get the MAC of a specified NIC?


I am writing a sniifer, when the program starts user have to select an interface, then get the hwaddr of the selected interface, I want one way rather than "ifconfig > log" and read the file log.
 
Old 02-15-2010, 07:53 AM   #2
logicalfuzz
Member
 
Registered: Aug 2005
Distribution: Arch Linux
Posts: 291

Rep: Reputation: 48
i don't know if i understand the question completely, but here's what i'll use to get the MAC address of a specified NIC using command line:
Code:
ifconfig eth0 | head -n1 | awk '{print $5}'
 
Old 02-15-2010, 12:42 PM   #3
cricketlong
Member
 
Registered: Dec 2008
Posts: 52

Original Poster
Rep: Reputation: 15
thanks, logicalfuzz
I am coding in C, so I want one way using some API or network protocol rather than reading from a file or pipe.
I have ever try to send an ARP request packet from eth1 and send to eth1, but no reply. All the computers in my room are connected by a switch.My IP of the interface I was using is 192.168.1.101.
The packet I used is shown below:
struct ethernet_header
{
unsigned char dst_MAC[6];
unsigned char src_MAC[6];
unsigned short eth_type;
};

struct arp_header
{
unsigned short hardware_type;
unsigned short proto_type;
unsigned char hwaddr_len;
unsigned char proto_len;
unsigned short option;
unsigned char src_addr[6];
unsigned long src_ip;
unsigned char dst_addr[6];
unsigned long dst_ip;
unsigned char padding[18];
};

struct arp_packet
{
struct ethernet_header eth;
struct arp_header arp;
};

struct arp_packet arppkt;
memset(arppkt.eth.dst_MAC, 0xFF, 6);
memset(arppkt.eth.src_MAC, 0xAA, 6);
arppkt.eth.eth_type = htons(0x0806);

arppkt.arp.hardware_type = htons(0x0001);
arppkt.arp.proto_type = htons(0x0800);
arppkt.arp.hwaddr_len = 6;
arppkt.arp.proto_len = 4;
arppkt.arp.option = htons(0x0001);
memcpy(arppkt.arp.src_addr, arppkt.eth.src_MAC, 6);
arppkt.arp.src_ip = inet_addr("192.168.1.2");
memcpy(arppkt.arp.dst_addr, arppkt.eth.dst_MAC, 6);
arppkt.arp.dst_ip = inet_addr("192.168.1.101");

Last edited by cricketlong; 02-15-2010 at 12:44 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
why NIC needs to be down to change mac wizardcpu0 Linux - Networking 3 10-30-2009 02:13 PM
Get MAC address of all NIC Johnny Ljunggren Linux - Networking 9 06-17-2008 07:34 AM
MAC aliases on a NIC!! vishamr2000 Linux - Networking 1 07-29-2007 06:38 AM
Getting NIC MAC adress thar Linux - Networking 3 06-28-2004 11:38 PM
MAC of NIC sts_cat Linux - General 3 04-05-2001 12:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration