码迷,mamicode.com
首页 > 其他好文 > 详细

APUE之通过popen,fputc等函数获取本虚拟机网卡eth0的IP

时间:2014-12-26 23:04:23      阅读:412      评论:0      收藏:0      [点我收藏+]

标签:apue   unix   c语言   网卡   

任务:unix环境通过c程序获取本虚拟机网卡eth0的IP.


总结:

1. 标准I/O库函数相对于系统调用的函数多了个缓冲区(,buf),安全性上通过buf 防溢出。

2.用system函数输出是标准输出,进一步理解fork函数和exec函数重新开启一个进程运行程序;

3.printf 这类输出函数中“ ”若包含“记得要换成转义字符\"            资料链接:   http://blog.csdn.net/ce123_zhouwei/article/details/9074121

/*********************************************************************************
 *      Copyright:  (C) 2014 songyong<handy_skyoutlook.com>
 *                  All rights reserved.
 *
 *       Filename:  eth0.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(2014年12月26日)
 *         Author:  sky <handy_sky@outlook.com>
 *      ChangeLog:  1, Release initial version on "2014年12月26日 18时38分51秒"
 *                 
 ********************************************************************************/
#include<stdio.h>
#define sizeofbuf 512

int main(int argc,char **argv)
{
        char    buf[sizeofbuf];
        FILE    *fp;
        char     ch;

    snprintf(buf,sizeof(buf),"ifconfig eth0|grep 'inet addr'|awk '{print $2}'|cut -d \":\" -f2");
    fp = popen(buf,"r");
       if( NULL == fp)
            {
                 printf("error");
                 exit(-1);
            }
            while( EOF != (ch=fgetc(fp)) )
                {
                  fputc(ch,stdout); 
                } 
}
// 编译后运行成功获取本机IP   
[pikaqiu@centos6 practice_sky]$ ./a.out 
192.168.119.128




APUE之通过popen,fputc等函数获取本虚拟机网卡eth0的IP

标签:apue   unix   c语言   网卡   

原文地址:http://blog.csdn.net/u010944778/article/details/42177243

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!