CentOS 普通用户执行ipconfig 系统会提示未知命令,其实带路径 /sbin/ifconfig 执行就能成功,利用ipconfig命令,通过简单的代码就能获得系统IP地址列表,代码如下:
- //CentOS
-
#define IPADDRESS_LEN 32
-
#define GET_IP_CMD "/sbin/ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'"
-
-
void get_host_ipaddr (char ip[][IPADDRESS_LEN], int maxip)
-
{
-
char *pi, buff[1024];
-
int ret, idx;
-
-
memset (buff, 0, sizeof(buff));
-
-
ret = exec_cmd (GET_IP_CMD, buff, sizeof(buff)-1);
-
-
if (ret <= 0)
-
return;
-
-
idx = 0;
-
pi = strtok (buff, "\n");
-
while (pi)
-
{
-
strcpy (ip[idx], pi);
-
-
idx++;
-
-
if (idx >= maxip)
-
break;
-
-
pi = strtok (NULL, "\n");
-
}
-
}
阅读(3818) | 评论(0) | 转发(0) |