當前位置:首頁 » 網路設備 » 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