linux获取网络信息函数
2017-05-27 22:42
603 查看
获取IP地址
int sys_getIP(char *ip_addr)
{
char ip_sys[80] =
{“ifconfig eth0 | grep inet | cut -d: -f2 | cut -d’ ’ -f1 > ipaddr.txt”};
FILE *ip_fp = NULL;
int error_sys;
if((error_sys = system(ip_sys)) !=0)
{
fprintf(stderr, “[get_eth] ip_sys : 0x%x\n”, error_sys);
}
if ((ip_fp=fopen(“ipaddr.txt”, “r”)) != NULL)
{
fgets(ip_addr, 39, ip_fp);
}
else
{
perror (“fread”);
return -1;
}
fclose (ip_fp);
unlink(“ipaddr.txt”);
return 0;
}
获取子网掩码
int sys_getMask(char *mask_addr)
{
char ip_sys[80] =
{“ifconfig eth0 | grep Mask | cut -dk -f2 | cut -d: -f2 > /tmp/ipaddr.txt”};
FILE *ip_fp = NULL;
int error_sys;
if((error_sys = system(ip_sys)) !=0)
{
fprintf(stderr, “[get_eth] ip_sys : 0x%x\n”, error_sys);
}
if ((ip_fp=fopen(“/tmp/ipaddr.txt”, “r”)) != NULL)
{
fgets(mask_addr, 39, ip_fp);
}
else
{
perror (“fread”);
return -1;
}
fclose (ip_fp);
unlink(“/tmp/ipaddr.txt”);
return 0;
}
获取网关
int sys_getGW(char *gw_addr)
{
FILE *fp;
char buf[512];
char cmd[128];
char *tmp;
strcpy(cmd, “ip route”);
fp = popen(cmd, “r”);
if(NULL == fp)
{
perror(“popen error”);
return “”;
}
while(fgets(buf, sizeof(buf), fp) != NULL) //stroe output one line!!
{
tmp =buf;
while(*tmp && isspace(*tmp))
++ tmp;
if(strncmp(tmp, “default”, strlen(“default”)) == 0)
break;
}
sscanf(buf,”%*s%*s%s”, gw_addr);
printf(“default gateway:%s\n”, gw_addr);
pclose(fp);
return 0;
}
获取mac地址
int sys_getHW(char *hw_addr)
{
char ip_sys[80] =
{“ifconfig | sed -e ‘/.HWaddr /!d;s///;s/ .//’ > hwaddr.txt”};
FILE *ip_fp = NULL;
int error_sys;
if((error_sys = system(ip_sys)) !=0)
{
fprintf(stderr, “[get_eth] ip_sys : 0x%x\n”, error_sys);
}
if ((ip_fp=fopen(“hwaddr.txt”, “r”)) != NULL)
{
fgets(hw_addr, 39, ip_fp);
}
else
{
perror (“fread”);
return -1;
}
fclose (ip_fp);
unlink(“hwaddr.txt”);
return 0;
}
int sys_getIP(char *ip_addr)
{
char ip_sys[80] =
{“ifconfig eth0 | grep inet | cut -d: -f2 | cut -d’ ’ -f1 > ipaddr.txt”};
FILE *ip_fp = NULL;
int error_sys;
if((error_sys = system(ip_sys)) !=0)
{
fprintf(stderr, “[get_eth] ip_sys : 0x%x\n”, error_sys);
}
if ((ip_fp=fopen(“ipaddr.txt”, “r”)) != NULL)
{
fgets(ip_addr, 39, ip_fp);
}
else
{
perror (“fread”);
return -1;
}
fclose (ip_fp);
unlink(“ipaddr.txt”);
return 0;
}
获取子网掩码
int sys_getMask(char *mask_addr)
{
char ip_sys[80] =
{“ifconfig eth0 | grep Mask | cut -dk -f2 | cut -d: -f2 > /tmp/ipaddr.txt”};
FILE *ip_fp = NULL;
int error_sys;
if((error_sys = system(ip_sys)) !=0)
{
fprintf(stderr, “[get_eth] ip_sys : 0x%x\n”, error_sys);
}
if ((ip_fp=fopen(“/tmp/ipaddr.txt”, “r”)) != NULL)
{
fgets(mask_addr, 39, ip_fp);
}
else
{
perror (“fread”);
return -1;
}
fclose (ip_fp);
unlink(“/tmp/ipaddr.txt”);
return 0;
}
获取网关
int sys_getGW(char *gw_addr)
{
FILE *fp;
char buf[512];
char cmd[128];
char *tmp;
strcpy(cmd, “ip route”);
fp = popen(cmd, “r”);
if(NULL == fp)
{
perror(“popen error”);
return “”;
}
while(fgets(buf, sizeof(buf), fp) != NULL) //stroe output one line!!
{
tmp =buf;
while(*tmp && isspace(*tmp))
++ tmp;
if(strncmp(tmp, “default”, strlen(“default”)) == 0)
break;
}
sscanf(buf,”%*s%*s%s”, gw_addr);
printf(“default gateway:%s\n”, gw_addr);
pclose(fp);
return 0;
}
获取mac地址
int sys_getHW(char *hw_addr)
{
char ip_sys[80] =
{“ifconfig | sed -e ‘/.HWaddr /!d;s///;s/ .//’ > hwaddr.txt”};
FILE *ip_fp = NULL;
int error_sys;
if((error_sys = system(ip_sys)) !=0)
{
fprintf(stderr, “[get_eth] ip_sys : 0x%x\n”, error_sys);
}
if ((ip_fp=fopen(“hwaddr.txt”, “r”)) != NULL)
{
fgets(hw_addr, 39, ip_fp);
}
else
{
perror (“fread”);
return -1;
}
fclose (ip_fp);
unlink(“hwaddr.txt”);
return 0;
}
相关文章推荐
- Linux中获取本机网络信息的几个函数及应用
- Linux进程的管理 获取进程信息函数
- 在vb中使用Iphlpapi.dll获取网络信息 第四章 第二节 一个重要函数
- 《ASCE1885的网络编程》---Winsock APIの网络信息获取函数
- linux中获取MP3的相关信息的函数,包括总长时间等
- Linux 网络编程基础(2)-- 获取主机信息
- Linux下通过ioctl系统调用来获取和设置网络信息
- linux系统下 python获取 内存、cpu、负载、网络流量、硬盘等信息
- Linux下通过ioctl系统调用来获取和设置网络信息
- Linux stat函数讲解(获取文件详细信息)
- 网络信息获取函数和网卡信息获取
- Linux下获取第一个网络接口名称的函数实现
- 【转】Linux下获取第一个网络接口名称的函数实现
- Linux下利用ioctl函数获取网卡信息
- Linux网络编程常用信息 结构 格式化 函数 等
- 《ASCE1885的网络编程》---Winsock APIの网络信息获取函数
- Linux下通过ioctl系统调用来获取和设置网络信息
- linux获取网络信息
- 转载:linux系统下 python获取 内存、cpu、负载、网络流量、硬盘等信息
- Linux stat函数讲解(获取文件详细信息)