当前位置:首页 » 网络设备 » c获取网卡mac

c获取网卡mac

发布时间: 2021-03-15 07:05:06

㈠ 怎样获得网卡MAC地址

C:\>ipconfig/all

Windows IP Configuration

Host Name . . . . . . . . . . . . : SERVER
Primary Dns Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Unknown
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No

Ethernet adapter 本地连接 3:

Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Realtek RTL8139 Family PCI Fast Ethernet
NIC
Physical Address. . . . . . . . . : 00-0A-EB-FA-3B-61()
DHCP Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 192.168.117.212 (IP地址)
Subnet Mask . . . . . . . . . . . : 255.255.255.0 (子网掩码)
Default Gateway . . . . . . . . . : 192.168.117.114 (网关)
DNS Servers . . . . . . . . . . . : 202.96.128.86 (DNC服务器)

C:\>

㈡ 100分求教,如何用C语言获取板载网卡的MAC地址

#include<winsock2.h>
#include<stdio.h>
#include<stdlib.h>
#include<httpext.h>
#include<windef.h>
#include<Nb30.h>

#pragmacomment(lib,"ws2_32.lib")
#pragmacomment(lib,"netapi32.lib")

voidCheckIP(void)
{
WSADATAwsaData;
charname[155];
char*ip;
PHOSTENThostinfo;
if(WSAStartup(MAKEWORD(2,0),&wsaData)==0)
{
if(gethostname(name,sizeof(name))==0)
{
if((hostinfo=gethostbyname(name))!=NULL)
{
ip=inet_ntoa(*(structin_addr*)*hostinfo->h_addr_list);
printf("IP地址:%s ",ip);
}
}
WSACleanup();
}
}

intgetMAC(char*mac)
{
NCBncb;
typedefstruct_ASTAT_
{
ADAPTER_STATUSadapt;
NAME_BUFFERNameBuff[30];
}ASTAT,*PASTAT;

ASTATAdapter;
typedefstruct_LANA_ENUM
{
UCHARlength;
UCHARlana[MAX_LANA];
}LANA_ENUM;

LANA_ENUMlana_enum;
UCHARuRetCode;
memset(&ncb,0,sizeof(ncb));
memset(&lana_enum,0,sizeof(lana_enum));

ncb.ncb_command=NCBENUM;
ncb.ncb_buffer=(unsignedchar*)&lana_enum;
ncb.ncb_length=sizeof(LANA_ENUM);
uRetCode=Netbios(&ncb);
if(uRetCode!=NRC_GOODRET)
returnuRetCode;

for(intlana=0;lana<lana_enum.length;lana++)
{
ncb.ncb_command=NCBRESET;
ncb.ncb_lana_num=lana_enum.lana[lana];
uRetCode=Netbios(&ncb);
if(uRetCode==NRC_GOODRET)
break;
}
if(uRetCode!=NRC_GOODRET)
returnuRetCode;

memset(&ncb,0,sizeof(ncb));
ncb.ncb_command=NCBASTAT;
ncb.ncb_lana_num=lana_enum.lana[0];
strcpy((char*)ncb.ncb_callname,"*");
ncb.ncb_buffer=(unsignedchar*)&Adapter;
ncb.ncb_length=sizeof(Adapter);
uRetCode=Netbios(&ncb);
if(uRetCode!=NRC_GOODRET)
returnuRetCode;
sprintf(mac,"%02X-%02X-%02X-%02X-%02X-%02X",
Adapter.adapt.adapter_address[0],
Adapter.adapt.adapter_address[1],
Adapter.adapt.adapter_address[2],
Adapter.adapt.adapter_address[3],
Adapter.adapt.adapter_address[4],
Adapter.adapt.adapter_address[5]
);
return0;
}

intmain(void)
{
CheckIP();
charmac[200];
getMAC(mac);
printf("mac地址:%s ",mac);
return0;
}

㈢ 使用C/C++语言如何获取笔记本的无线网卡mac地址

|/*
* mac_addr_dlpi.c
*
* Return the MAC (ie, ethernet hardware) address by using the dlpi api.
*
* compile with: gcc -c -D "OS" mac_addr_dlpi.c
* with "OS" is one of AIX, SunOS, HPUX
*/

/**********************************************************************/
/* this section defines a list of the dlpi capable devices
* this depends on the operating system
*/

#undef DLPI_DEV

#ifdef HPUX
static char *dlpi_dev[] = {"/dev/dlpi", ""};
#define DLPI_DEV
#endif

#ifdef AIX
static char *dlpi_dev[] = {"/dev/dlpi/et", "/dev/dlpi/en",
"/dev/dlpi/tr", "/dev/dlpi/fddi", ""};
#define DLPI_DEV
/* AIX: remember to set up /etc/pse.conf or /etc/dlpi.conf */
#endif

#ifdef SunOS
static char *dlpi_dev[] = {"/dev/hme", "/dev/ie", "/dev/le", ""};
#define DLPI_DEV
#endif

#ifndef DLPI_DEV
static char *dlpi_dev[] = {"/dev/dlpi", ""};
/* unknown OS - hope that this will work ??? */
#define DLPI_DEV
#endif

/**********************************************************************/
/*
* implementation
*/

#define INSAP 22
#define OUTSAP 24

#include <sys/types.h>;
#include <fcntl.h>;
#include <errno.h>;
#include <stdio.h>;
#include <string.h>;
#include <signal.h>;
#include <ctype.h>;
#include <sys/stropts.h>;
#include <sys/poll.h>;
#include <sys/dlpi.h>;

#define b(source, destination, length) memcpy(destination, source, length)

#define AREA_SZ 5000 /*&=&* buffer length in bytes *&=&*/
static u_long ctl_area[AREA_SZ];
static u_long dat_area[AREA_SZ];
static struct strbuf ctl = {AREA_SZ, 0, (char *)ctl_area};
static struct strbuf dat = {AREA_SZ, 0, (char *)dat_area};
#define GOT_CTRL 1
#define GOT_DATA 2
#define GOT_BOTH 3
#define GOT_INTR 4
#define GOT_ERR 128

/*&=&* get a message from a stream; return type of message *&=&*/
static int get_msg(int fd)
{
int flags = 0;
int res, ret;
ctl_area[0] = 0;
dat_area[0] = 0;
ret = 0;
res = getmsg(fd, &ctl, &dat, &flags);
if(res < 0) {
if(errno == EINTR) {
return(GOT_INTR);
} else {
return(GOT_ERR);
}
}
if(ctl.len >; 0) {
ret |= GOT_CTRL;
}
if(dat.len >; 0) {
ret |= GOT_DATA;
} return(ret);
}

/*&=&* verify that dl_primitive in ctl_area = prim *&=&*/
static int check_ctrl(int prim)
{
dl_error_ack_t *err_ack = (dl_error_ack_t *)ctl_area;
if(err_ack->;dl_primitive != prim) {
return GOT_ERR;
} return 0;
}

/*&=&* put a control message on a stream *&=&*/
static int put_ctrl(int fd, int len, int pri)
{
ctl.len = len;
if(putmsg(fd, &ctl, 0, pri) < 0) {
return GOT_ERR;
} return 0;
}

/*&=&* put a control + data message on a stream *&=&*/
static int put_both(int fd, int clen, int dlen, int pri)
{
ctl.len = clen;
dat.len = dlen;
if(putmsg(fd, &ctl, &dat, pri) < 0) {
return GOT_ERR;
} return 0;
}

/*&=&* open file descriptor and attach *&=&*/
static int dl_open(const char *dev, int ppa, int *fd)
{
dl_attach_req_t *attach_req = (dl_attach_req_t *)ctl_area;
if((*fd = open(dev, O_RDWR)) == -1) {
return GOT_ERR;
}
attach_req->;dl_primitive = DL_ATTACH_REQ;
attach_req->;dl_ppa = ppa;
put_ctrl(*fd, sizeof(dl_attach_req_t), 0);
get_msg(*fd);
return check_ctrl(DL_OK_ACK);
}

/*&=&* send DL_BIND_REQ *&=&*/
static int dl_bind(int fd, int sap, u_char *addr)
{
dl_bind_req_t *bind_req = (dl_bind_req_t *)ctl_area;
dl_bind_ack_t *bind_ack = (dl_bind_ack_t *)ctl_area;
bind_req->;dl_primitive = DL_BIND_REQ;
bind_req->;dl_sap = sap;
bind_req->;dl_max_conind = 1;
bind_req->;dl_service_mode = DL_CLDLS;
bind_req->;dl_conn_mgmt = 0;
bind_req->;dl_xidtest_flg = 0;
put_ctrl(fd, sizeof(dl_bind_req_t), 0);
get_msg(fd);
if (GOT_ERR == check_ctrl(DL_BIND_ACK)) {
return GOT_ERR;
}
b((u_char *)bind_ack + bind_ack->;dl_addr_offset, addr,
bind_ack->;dl_addr_length);
return 0;
}

/**********************************************************************/
/*
* interface:
* function mac_addr_dlpi - get the mac address of the "first" interface
*
* parameter: addr: an array of six bytes, has to be allocated by the
caller
*
* return: 0 if OK, -1 if the address could not be determined
*
*/

long mac_addr_dlpi ( u_char *addr)
{
int fd;
int ppa;
u_char mac_addr[25];
int i;

char **dev;

for (dev = dlpi_dev; **dev != '\0'; ++dev) {
for (ppa=0; ppa<10; ++ppa) {
if (GOT_ERR != dl_open(*dev, ppa, &fd)) {
if (GOT_ERR != dl_bind(fd, INSAP, mac_addr)) {
b( mac_addr, addr, 6);
return 0;
}
}
close(fd);
}
} return -1;
}

/**********************************************************************/
/*
* Main (only for testing)
*/
#ifdef MAIN
int main( int argc, char **argv)
{
long stat;
int i;
u_char addr[6];

stat = mac_addr_dlpi( addr);
if (0 == stat) {
printf( "MAC address = ");
for (i=0; i<6; ++i) {
printf("%2.2x", addr);
}
printf( "\n");
}
else {
fprintf( stderr, "can't get MAC address\n");
exit( 1);
} return 0;
}
#endif

㈣ 如何用C语言获取网卡的mac地址

为什么一定要用C语言呢?这个用C语言比较麻烦,需要的知识比较多,完全可以用更简单的办法啊。命令行上这样的命令“ipconfig -all | find "物理地址"”,可以很简单的就得到了。
如果是英文系统, 将“物理地址”换成“Physical Address”即可。
如果非的要用C语言,则可以先调用system函数, system("ipconfig -all | find \"物理地址\" > temp.txt" ); 将mac地址信息存入临时文件temp.txt;然后再从中提取。

㈤ linux c 怎么获得服务器mac 地址

获取服务器mac 复制代码 代码如下: <?php /** 获取网卡的MAC地址原码;目前支持WIN/LINUX系统 获取机器网卡的物理(MAC)地址 **/ class GetmacAddr{ var $result = array(); // 返回带有MAC地址的字串数组 var $macAddr; /*构造*/ function __construct($osType){ switch ( strtolower($osType) ){ case "unix": break; case "solaris": break; case "aix": break; case "linux": { $this->for_linux_os(); }break; default: { $this->for_windows_os(); }break; } $temp_array = array(); foreach($this->result as $value){ if(preg_match("/[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f]/i",$value, $temp_array ) ){ $this->macAddr = $temp_array[0]; break; } } unset($temp_array); return $this->macAddr; } /*linux系统中获取方法*/ function for_linux_os(){ @exec("ifconfig -a", $this->result); return $this->result; } /*win系统中的获取方法*/ function for_windows_os(){ @exec("ipconfig /all", $this->result); if ( $this->result ) { return $this->result; } else { $ipconfig = $_SERVER["WINDIR"]."\system32\ipconfig.exe"; if(is_file($ipconfig)) { @exec($ipconfig." /all", $this->result); } else { @exec($_SERVER["WINDIR"]."\system\ipconfig.exe /all", $this->result); return $this->result; } } } } ?> 获取客户端mac地址: 复制代码 代码如下: @exec("arp -a",$array); //执行arp -a命令,结果放到数组$array中 foreach($array as $value){ //匹配结果放到数组$mac_array if(strpos($value,$_SERVER["REMOTE_ADDR"]) && preg_match("/(:?[0-9A-F]{2}[:-]){5}[0-9A-F]{2}/i",$value,$mac_array)){ $mac = $mac_array[0]; break; } } echo $mac; 注:客户端获取的mac不能在本机测试,只能用别的电脑访问才能输出

㈥ c语言,获取本机mac地址,那位大神解答下。

有个简单的方法,提供给你个思路
system("ipconfig /all > tmp.txt");
然后打开tmp.txt
查找本地连接 再找之后的Physical Address字符串,然后找冒号,
取这个冒号后面的值就是mac了

㈦ C语言如何获取嵌入式linux网卡上的mac地址

||

全对应的

getmacaddress.c

#include<stdio.h>

#ifdefined(WIN32)||(!defined(__GNUC__)&&!defined(__clang__))
#include<winsock2.h>
#include<iphlpapi.h>
#include<stdlib.h>
#pragmacomment(lib,"IPHLPAPI.lib")
#elifdefined(__linux__)
#include<string.h>//strncpy
#include<sys/ioctl.h>
#include<sys/socket.h>
#include<net/if.h>
#else
//bsd
#include<sys/types.h>//FreeBSDu_int
#include<ifaddrs.h>
#include<net/if.h>
#include<net/if_dl.h>
#include<net/if_types.h>
#endif

voidgetmacaddress(char*mac_address){
#ifdefined(WIN32)||(!defined(__GNUC__)&&!defined(__clang__))

PIP_ADAPTER_INFOpAdapterInfo;
PIP_ADAPTER_INFOpAdapter;
ULONGulOutBufLen=sizeof(IP_ADAPTER_INFO);
unsignedchar*addr;

mac_address[0]=0;
pAdapterInfo=(IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO));
if(!pAdapterInfo)return;
if(GetAdaptersInfo(pAdapterInfo,&ulOutBufLen)==ERROR_BUFFER_OVERFLOW){
free(pAdapterInfo);
pAdapterInfo=(IP_ADAPTER_INFO*)malloc(ulOutBufLen);
if(!pAdapterInfo)return;
}

if(GetAdaptersInfo(pAdapterInfo,&ulOutBufLen)==NO_ERROR){
pAdapter=pAdapterInfo;
if(pAdapter){
addr=pAdapter->Address;
sprintf(mac_address,"%02x:%02x:%02x:%02x:%02x:%02x",
addr[0],addr[1],addr[2],addr[3],addr[4],addr[5]);
}
}
free(pAdapterInfo);
#elifdefined(__linux__)

mac_address[0]=0;
structifreq*ifr,*ifend;
structifreqifreq;
structifconfifc;
structifreqifs[16];
intfd;
unsignedchar*addr;

fd=socket(AF_INET,SOCK_DGRAM,0);
ifc.ifc_len=sizeof(ifs);
ifc.ifc_req=ifs;
if(ioctl(fd,SIOCGIFCONF,&ifc)<0){close(fd);return;}

ifend=ifs+(ifc.ifc_len/sizeof(structifreq));
for(ifr=ifc.ifc_req;ifr<ifend;ifr++){
if(ifr->ifr_addr.sa_family==AF_INET){
strncpy(ifreq.ifr_name,ifr->ifr_name,sizeof(ifreq.ifr_name));
if(ioctl(fd,SIOCGIFHWADDR,&ifreq)<0)continue;
addr=ifreq.ifr_hwaddr.sa_data;
sprintf(mac_address,"%02x:%02x:%02x:%02x:%02x:%02x",
addr[0],addr[1],addr[2],addr[3],addr[4],addr[5]);
}
}
close(fd);
#else
//bsd
mac_address[0]=0;
structifaddrs*ifa_list,*ifa;
structsockaddr_dl*dl;
unsignedchar*addr;
if(getifaddrs(&ifa_list)<0)return;
for(ifa=ifa_list;ifa;ifa=ifa->ifa_next){
dl=(structsockaddr_dl*)ifa->ifa_addr;
if(dl->sdl_family==AF_LINK&&dl->sdl_type==IFT_ETHER){
addr=(unsignedchar*)LLADDR(dl);
sprintf(mac_address,"%02x:%02x:%02x:%02x:%02x:%02x",
addr[0],addr[1],addr[2],addr[3],addr[4],addr[5]);
return;
}
}
freeifaddrs(ifa_list);
#endif
}
热点内容
网卡了的原因 发布:2021-03-16 21:18:20 浏览:602
联通客服工作怎么样 发布:2021-03-16 21:17:49 浏览:218
路由器画图 发布:2021-03-16 21:17:21 浏览:403
大网卡收费 发布:2021-03-16 21:16:50 浏览:113
路由器免费送 发布:2021-03-16 21:16:19 浏览:985
孝昌营业厅 发布:2021-03-16 21:15:54 浏览:861
网速增速代码 发布:2021-03-16 21:15:29 浏览:194
怎么黑光纤 发布:2021-03-16 21:14:54 浏览:901
端口增大 发布:2021-03-16 21:14:20 浏览:709
开机没信号是什么原因 发布:2021-03-16 21:13:45 浏览:645