您的位置:首页 > 运维架构 > Linux

linux c 命令 获取eth0 ip地址

2016-01-09 09:51 369 查看
<span style="white-space:pre">	</span>char tmpbuf[64]={0};
char myip[16] = "0.0.0.0";
char* renewCh = NULL;
FILE *fpRead;

char* command=(char*)"ifconfig eth0|grep 'inet addr'";
fpRead = popen(command, "r");
fgets(tmpbuf, 64, fpRead);
if(fpRead != NULL)
<span style="white-space:pre">		</span>pclose(fpRead);
renewCh = strstr(tmpbuf,"inet addr:");
if(renewCh)
{
strncpy(myip,renewCh+sizeof("inet addr:")-1,16);
myip[15] = '\0';
}
printf("----------eth0 ip=%s\n", myip);

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: