A. 获取ip地址对应的mac地址,c/c++编程实现,linux下
/**
*@send_arp.c
*@questwithLinux*PF_PACKETinterface
*@Author:xuelei
**/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<unistd.h>
#include<netdb.h>
#include<sys/socket.h>
#include<sys/un.h>
#include<sys/ioctl.h>
#include<netinet/in.h>
#include<net/if.h>
#include<sys/types.h>
#include<asm/types.h>
#include<features.h>/*需要里面的glibc版本号*/
#if__GLIBC__>=2&&__GLIBC_MINOR>=1
#include<netpacket/packet.h>
#include<net/ethernet.h>/*链路层(L2)协议*/
#else
#include<asm/types.h>
#include<linux/if_packet.h>
#include<linux/if_ether.h>/*链路层协议*/
#endif
#include<netinet/if_ether.h>
#defineINLEN4
#defineMAC_BCAST_ADDR(uint8_t*)"xffxffxffxffxffxff"
voisage_quit(char*arg0);
intget_ifi(char*dev,char*mac,intmacln,structin_addr*lc_addr,intipln);
voidprmac(u_char*ptr);
intmain(intargc,char**argv)
{
if(argc!=2)
usage_quit(argv[0]);
intfd,salen,n;
u_char*mac;
charrecv_buf[120],rep_addr[16];
structin_addrlc_addr,req_addr;
structsockaddr_llreqsa,repsa;
structarp_pkt{
structether_headereh;
structether_arpea;
u_charpadding[18];
}req;
bzero(&reqsa,sizeof(reqsa));
reqsa.sll_family=PF_PACKET;
reqsa.sll_ifindex=if_nametoindex("eth0");
if((fd=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ARP)))<0){
perror("Socketerror");
exit(1);
}
mac=(char*)malloc(ETH_ALEN);
bzero(&req,sizeof(req));
if(get_ifi("eth0",mac,ETH_ALEN,&lc_addr,INLEN)){
fprintf(stderr,"Error:Gethost’sinformationfailed ");
exit(0);
}
printf("HostMACis:%02x:%02x:%02x:%02x:%02x:%02x ",*mac,*(mac+1),*(mac+2),*(mac+3),*(mac+4),*(mac+5));
printf("HostIPis:%s ",inet_ntop(AF_INET,&lc_addr,rep_addr,1024));
/*填写以太网头部*/
memcpy(req.eh.ether_dhost,MAC_BCAST_ADDR,ETH_ALEN);
memcpy(req.eh.ether_shost,mac,ETH_ALEN);
req.eh.ether_type=htons(ETHERTYPE_ARP);
/*填写arp数据*/
req.ea.arp_hrd=htons(ARPHRD_ETHER);
req.ea.arp_pro=htons(ETHERTYPE_IP);
req.ea.arp_hln=ETH_ALEN;
req.ea.arp_pln=INLEN;
req.ea.arp_op=htons(ARPOP_REQUEST);
memcpy(req.ea.arp_sha,mac,ETH_ALEN);
memcpy(req.ea.arp_spa,&lc_addr,INLEN);
inet_aton(argv[1],req.ea.arp_tpa);
bzero(recv_buf,sizeof(recv_buf));
bzero(&repsa,sizeof(repsa));
salen=sizeof(structsockaddr_ll);
if((n=sendto(fd,&req,sizeof(req),0,(structsockaddr*)&reqsa,sizeof(reqsa)))<=0){
perror("Sendtoerror");
exit(1);
}
printf("Broadcastarprequestof%s,%dbytesbesent ",argv[1],n);
while(1){
if((n=recvfrom(fd,recv_buf,sizeof(req),0,(structsockaddr*)&repsa,&salen))<=0){
perror("Recvfromerror");
exit(1);
}
if(ntohs(*(__be16*)(recv_buf+20))==2&&!memcmp(req.ea.arp_tpa,recv_buf+28,4)){
printf("Responsefrom%s,%dbytesreceived ",argv[1],n);
printf("PeerIPis:%s ",inet_ntop(AF_INET,(structin_addr*)(recv_buf+28),rep_addr,1024));
prmac((u_char*)(recv_buf+22));//prmac((u_char*)(recv_buf+6));
break;
}
else
{
printf("Haverecivedata ");
break;
}
}
free(mac);
}
intget_ifi(char*dev,char*mac,intmacln,structin_addr*lc_addr,intipln)
{
intreqfd,n;
structifreqmacreq;
reqfd=socket(AF_INET,SOCK_DGRAM,0);
strcpy(macreq.ifr_name,dev);
/*获取本地接口MAC地址*/
if(ioctl(reqfd,SIOCGIFHWADDR,¯eq)!=0)
return1;
memcpy(mac,macreq.ifr_hwaddr.sa_data,macln);
/*获取本地接口IP地址*/
if(ioctl(reqfd,SIOCGIFADDR,¯eq)!=0)
return1;
memcpy(lc_addr,&((structsockaddr_in*)(¯eq.ifr_addr))->sin_addr,ipln);
return0;
}
voidprmac(u_char*ptr)
{
printf("PeerMACis:%02x:%02x:%02x:%02x:%02x:%02x ",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5));
}
voisage_quit(char*arg0)
{
fprintf(stderr,"Usage:%s<query_IP> ",arg0);
exit(1);
}
B. 在LINUX 下怎么查看网卡的MAC地址
在LINUX下查看网卡的MAC地址的办法如下:
1、首先在桌面右键选择“打开终端”。
C. linux C语言 取网口MAC地址 类型转换问题 详见内
你的tt取出来是不是类似"B5"这样的16进制数字符串?想要转成单字节uint8_t类型的话,进行如下转换即可:
if((tt[0]>='0')&&(tt[0]<='9'))
{
mm.bssid_ap[0]=((tt[0]-'0')<<4);
}
else if((tt[0]>='a')&&(tt[0]<='z'))
{
mm.bssid_ap[0]=((tt[0]-'a')<<4);
}
else if((tt[0]>='A')&&(tt[0]<='Z'))
{
mm.bssid_ap[0]=((tt[0]-'A')<<4);
}
if((tt[1]>='0')&&(tt[1]<='9'))
{
mm.bssid_ap[0] +=(tt[1]-'0');
}
else if((tt[1]>='a')&&(tt[1]<='z'))
{
mm.bssid_ap[0] +=(tt[1]-'a');
}
else if((tt[1]>='A')&&(tt[1]<='Z'))
{
mm.bssid_ap[0] +=(tt[1]-'A');
}
D. 有谁知道linux系统环境下,怎样在后台才能获取到mac地址
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 与系统相关的一些常用工具方法.
*
* @author stephen
* @version 1.0.0
*/
public class SystemTool {
/**
* 获取当前操作系统名称.
* return 操作系统名称 例如:windows xp,linux 等.
*/
public static String getOSName() {
return System.getProperty("os.name").toLowerCase();
}
/**
* 获取unix网卡的mac地址.
* 非windows的系统默认调用本方法获取.如果有特殊系统请继续扩充新的取mac地址方法.
* @return mac地址
*/
public static String getUnixMACAddress() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("ifconfig eth0");// linux下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息
bufferedReader = new BufferedReader(new InputStreamReader(process
.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
index = line.toLowerCase().indexOf("hwaddr");// 寻找标示字符串[hwaddr]
if (index >= 0) {// 找到了
mac = line.substring(index +"hwaddr".length()+ 1).trim();// 取出mac地址并去除2边空格
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}
return mac;
}
/**
* 获取widnows网卡的mac地址.
* @return mac地址
*/
public static String getWindowsMACAddress() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("ipconfig /all");// windows下的命令,显示信息中包含有mac地址信息
bufferedReader = new BufferedReader(new InputStreamReader(process
.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
index = line.toLowerCase().indexOf("physical address");// 寻找标示字符串[physical address]
if (index >= 0) {// 找到了
index = line.indexOf(":");// 寻找":"的位置
if (index>=0) {
mac = line.substring(index + 1).trim();// 取出mac地址并去除2边空格
}
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}
return mac;
}
/**
* 测试用的main方法.
*
* @param argc
* 运行参数.
*/
public static void main(String[] argc) {
String os = getOSName();
System.out.println(os);
if(os.startsWith("windows")){
//本地是windows
String mac = getWindowsMACAddress();
System.out.println(mac);
}else{
//本地是非windows系统 一般就是unix
String mac = getUnixMACAddress();
System.out.println(mac);
}
}
}
-------------------------------------------------------------------------
本程序可以正确获得本机IP地址和网卡"eth0"的MAC地址,已经在windowsXP和ubuntu-Linux上测试过
(注意:如果有多块网卡,可能出错)
下面给出代码:
import java.net.*;import java.util.*;
public class Test { public static void main(String[] args) { Test t = new Test(); System.out.println(t.getLocalIP()); System.out.println(t.getMacAddr()); }
public String getMacAddr() { String MacAddr = ""; String str = ""; try { NetworkInterface NIC = NetworkInterface.getByName("eth0"); byte[] buf = NIC.getHardwareAddress(); for (int i = 0; i < buf.length; i++) { str = str + byteHEX(buf[i]); } MacAddr = str.toUpperCase(); } catch (SocketException e) { e.printStackTrace(); System.exit(-1); } return MacAddr; }
public String getLocalIP() { String ip = ""; try { Enumeration<?> e1 = (Enumeration<?>) NetworkInterface .getNetworkInterfaces(); while (e1.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) e1.nextElement(); if (!ni.getName().equals("eth0")) { continue; } else { Enumeration<?> e2 = ni.getInetAddresses(); while (e2.hasMoreElements()) { InetAddress ia = (InetAddress) e2.nextElement(); if (ia instanceof Inet6Address) continue; ip = ia.getHostAddress(); } break; } } } catch (SocketException e) { e.printStackTrace(); System.exit(-1); } return ip; }
/* 一个将字节转化为十六进制ASSIC码的函数 */ public static String byteHEX(byte ib) { char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; char[] ob = new char[2]; ob[0] = Digit[(ib >>> 4) & 0X0F]; ob[1] = Digit[ib & 0X0F]; String s = new String(ob); return s; }}
E. 在linux系统中怎样查看mac地址
1、来 首先在桌面右键选择“打开终源端”,或者按ctrl + Alt + T快捷键打开终端
F. linux下如何获取网卡IP地址及MAC地址
一般的linux系统的话输入
/sbin/ifconfig
然后eth0一般就是你的网卡,里面找HWaddr后面就是MAC地址
G. 请教linux 协议栈内如何获取本机MAC地址
用 dev_get_by_name(&init_net,"br0") 可以获取到net_device结构体
里面的 dev_addr 参数就是MAC地址
H. 在linux里面怎么获取mac地址
可以使用ifconfig命令。ifconfig是linux中用于显示或配置网络设备(网络接口卡)的命令,英文全称是network interfaces configuring。它能够显示网卡的IP地址、子网掩码、广播地址、硬件地址等信息。
用法示例:
查看网卡eth0的mac地址
$ ifconfig eth0
mac地址位于上图中的红色方框处。
I. 如何获取linux系统的mac地址
①命令ifconfig -a 其中 HWaddr字段就是MAC地址
②或者使用grep过滤只显示MAC地址:
ifconfig-a|grep-ihw
#只输出当前电脑上所有网卡的mac地址(专不显属示IP等信息)
#eth0Linkencap:EthernetHWaddr******----这是有线网卡的MAC地址
#wlan0Linkencap:EthernetHWaddr******----这是无线网卡的MAC地址