在Linux下用c编程怎么获取网卡序列号和硬盘序列号

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Linux下用c编程怎么获取网卡序列号和硬盘序列号相关的知识,希望对你有一定的参考价值。

其中wifi-name为无线网卡的名字,比如网卡eth0就是系统默认的网卡名字, 参考技术A 网卡还有序列号?有Mac地址吧?

求助在linux下用c语言取得双网卡的网关地址

参考技术A 多给我来点财富值哦:

[code=C/C++]
#define RTF_UP 0x0001
#define RTF_GATEWAY 0x0002
#define RTF_HOST 0x0004
#define _PATH_PROCNET_ROUTE "/proc/net/route"

struct addr
struct sockaddr_in addr;
char *name;
int host;
struct addr *next;
;

static struct addr *INET_nn = NULL; /* addr-to-name cache */

struct aftype
char *name;
char *title;
int af;
int alen;
char *(*print) (unsigned char *);
char *(*sprint) (struct sockaddr *, int numeric);
int (*input) (int type, char *bufp, struct sockaddr *);
void (*herror) (char *text);
int (*rprint) (int options);
int (*rinput) (int typ, int ext, char **argv);

/* may modify src */
int (*getmask) (char *src, struct sockaddr * mask, char *name);

int fd;
char *flag_file;
;
struct aftype inet_aftype;

struct sockaddr snet_gateway;

char *proc_gen_fmt(char *name, int more, FILE * fh,...)

char buf[512], format[512] = "";
char *title, *head, *hdr;
va_list ap;

if (!fgets(buf, (sizeof buf) - 1, fh))
return NULL;
strcat(buf, " ");

va_start(ap, fh);
title = va_arg(ap, char *);
for (hdr = buf; hdr;)
while (isspace(*hdr) || *hdr == '|')
hdr++;
head = hdr;
hdr = strpbrk(hdr, "| \t\n");
if (hdr)
*hdr++ = 0;

if (!strcmp(title, head))
strcat(format, va_arg(ap, char *));
title = va_arg(ap, char *);
if (!title || !head)
break;
else
strcat(format, "%*s"); /* XXX */

strcat(format, " ");

va_end(ap);

if (!more && title)
fprintf(stderr, "warning: %s does not contain required field %s\n",
name, title);
return NULL;

return strdup(format);


static int INET_getsock(char *bufp, struct sockaddr *sap)

char *sp = bufp, *bp;
unsigned int i;
unsigned val;
struct sockaddr_in *sin;

sin = (struct sockaddr_in *) sap;
sin->sin_family = AF_INET;
sin->sin_port = 0;

val = 0;
bp = (char *) &val;
for (i = 0; i < sizeof(sin->sin_addr.s_addr); i++)
*sp = toupper(*sp);

if ((*sp >= 'A') && (*sp <= 'F'))
bp[i] |= (int) (*sp - 'A') + 10;
else if ((*sp >= '0') && (*sp <= '9'))
bp[i] |= (int) (*sp - '0');
else
return (-1);

bp[i] <<= 4;
sp++;
*sp = toupper(*sp);

if ((*sp >= 'A') && (*sp <= 'F'))
bp[i] |= (int) (*sp - 'A') + 10;
else if ((*sp >= '0') && (*sp <= '9'))
bp[i] |= (int) (*sp - '0');
else
return (-1);

sp++;

sin->sin_addr.s_addr = htonl(val);

return (sp - bufp);
static int INET_resolve(char *name, struct sockaddr_in *sin, int hostfirst)

struct hostent *hp;
struct netent *np;

/* Grmpf. -FvK */
sin->sin_family = AF_INET;
sin->sin_port = 0;

/* Default is special, meaning 0.0.0.0. */
if (!strcmp(name, "default"))
sin->sin_addr.s_addr = INADDR_ANY;
return (1);

/* Look to see if it's a dotted quad. */
if (inet_aton(name, &sin->sin_addr))
return 0;

#ifdef EMBED
return(-1);
#else
/* If we expect this to be a hostname, try hostname database first */
#ifdef DEBUG
if (hostfirst) fprintf (stderr, "gethostbyname (%s)\n", name);
#endif
if (hostfirst &&
(hp = gethostbyname(name)) != (struct hostent *) NULL)
memcpy((char *) &sin->sin_addr, (char *) hp->h_addr_list[0],
sizeof(struct in_addr));
return 0;

/* Try the NETWORKS database to see if this is a known network. */
#ifdef DEBUG
fprintf (stderr, "getnetbyname (%s)\n", name);
#endif
if ((np = getnetbyname(name)) != (struct netent *) NULL)
sin->sin_addr.s_addr = htonl(np->n_net);
return 1;

if (hostfirst)
/* Don't try again */
errno = h_errno;
return -1;

#ifdef DEBUG
res_init();
_res.options |= RES_DEBUG;
#endif

#ifdef DEBUG
fprintf (stderr, "gethostbyname (%s)\n", name);
#endif
if ((hp = gethostbyname(name)) == (struct hostent *) NULL)
errno = h_errno;
return -1;

memcpy((char *) &sin->sin_addr, (char *) hp->h_addr_list[0],
sizeof(struct in_addr));

return 0;
#endif


static int INET_input(int type, char *bufp, struct sockaddr *sap)

switch (type)
case 1:
return (INET_getsock(bufp, sap));
case 256:
return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
default:
return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));


char *safe_strncpy(char *dst, const char *src, size_t size)

dst[size-1] = '\0';
return strncpy(dst,src,size-1);

static int INET_rresolve(char *name, size_t len, struct sockaddr_in *sin,
int numeric, unsigned int netmask)

struct hostent *ent;
struct netent *np;
struct addr *pn;
unsigned long ad, host_ad;
int host = 0;

/* Grmpf. -FvK */
if (sin->sin_family != AF_INET)
errno = EAFNOSUPPORT;
return (-1);

ad = (unsigned long) sin->sin_addr.s_addr;
if (ad == INADDR_ANY)
if ((numeric & 0x0FFF) == 0)
if (numeric & 0x8000)
safe_strncpy(name, "default", len);
else
safe_strncpy(name, "*", len);
return (0);


if (numeric & 0x0FFF)
safe_strncpy(name, inet_ntoa(sin->sin_addr), len);
return (0);


if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
host = 1;
pn = INET_nn;
while (pn != NULL)
if (pn->addr.sin_addr.s_addr == ad && pn->host == host)
safe_strncpy(name, pn->name, len);
return (0);

pn = pn->next;


host_ad = ntohl(ad);
np = NULL;
ent = NULL;
#ifndef EMBED
if (host)
ent = gethostbyaddr((char *) &ad, 4, AF_INET);
if (ent != NULL)
safe_strncpy(name, ent->h_name, len);

#ifndef __UC_LIBC__
else
np = getnetbyaddr(host_ad, AF_INET);
if (np != NULL)
safe_strncpy(name, np->n_name, len);

#endif
#endif
if ((ent == NULL) && (np == NULL))
safe_strncpy(name, inet_ntoa(sin->sin_addr), len);
pn = (struct addr *) malloc(sizeof(struct addr));
pn->addr = *sin;
pn->next = INET_nn;
pn->host = host;
pn->name = (char *) malloc(strlen(name) + 1);
strcpy(pn->name, name);
INET_nn = pn;

return (0);

static char *INET_sprint(struct sockaddr *sap, int numeric)

static char buff[128];

if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
return safe_strncpy(buff, "[NONE SET]", sizeof(buff));

if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
numeric, 0xffffff00) != 0)
return (NULL);

return (buff);


int main()

int ext=1,numeric=0;
char buff[1024], iface[16], flags[64];
char gate_addr[128], net_addr[128];
char mask_addr[128];
int num, iflags, metric, refcnt, use, mss, window, irtt;
FILE *fp = fopen(_PATH_PROCNET_ROUTE, "r");
char *fmt;
if (!fp)
perror(_PATH_PROCNET_ROUTE);
printf("INET (IPv4) not configured in this system.\n");
return 1;


irtt = 0;
window = 0;
mss = 0;

fmt = proc_gen_fmt(_PATH_PROCNET_ROUTE, 0, fp,
"Iface", "%16s",
"Destination", "%128s",
"Gateway", "%128s",
"Flags", "%X",
"RefCnt", "%d",
"Use", "%d",
"Metric", "%d",
"Mask", "%128s",
"MTU", "%d",
"Window", "%d",
"IRTT", "%d",
NULL);
/* "%16s %128s %128s %X %d %d %d %128s %d %d %d\n" */

if (!fmt)
return 1;

while (fgets(buff, 1023, fp))

num = sscanf(buff, fmt,
iface, net_addr, gate_addr,
&iflags, &refcnt, &use, &metric, mask_addr,
&mss, &window, &irtt);
if (num < 10 || !(iflags & RTF_UP))
continue;

INET_input(1, gate_addr, &snet_gateway);

strcpy(gate_addr, INET_sprint(&snet_gateway, numeric | 0x4000));
gate_addr[15] = '\0';
if (iflags & RTF_GATEWAY)
printf("==gateway=======%s===%s=====\n",gate_addr,iface);



free(fmt);
(void) fclose(fp);
return (0);

[/code]
参考技术B ifconfig吗

以上是关于在Linux下用c编程怎么获取网卡序列号和硬盘序列号的主要内容,如果未能解决你的问题,请参考以下文章

C语言获取硬件信息(CPU序列号,硬盘序列号,网卡IPMAC地址是否插入网线)

centos怎么获取硬盘序列号

Python获取电脑硬件配置的封装类,可以获取CPU序列号主板序列号BIOS序列号硬盘序列号和网卡MAC地址

Python获取电脑硬件配置的封装类,可以获取CPU序列号主板序列号BIOS序列号硬盘序列号和网卡MAC地址

windows x下的c编程,获取电脑硬件序列号(cpu 硬盘 bios 主版)!很着急!邮箱wangyu850722@163.com

Python获取电脑CPU序列号主板序列号BIOS序列号硬盘序列号列表网卡MAC地址