首页 > 代码库 > APUE之通过popen,fputc等函数获取本虚拟机网卡eth0的IP
APUE之通过popen,fputc等函数获取本虚拟机网卡eth0的IP
任务: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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。