zoukankan      html  css  js  c++  java
  • linux popen 获取 ip test ok

    任务:unix,linux通过c程序获取本机IP.

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

    2.printf 这类输出函数中“ ”若包含“记得要换成转义字符" 

    [objc] view plain copy print?

    1. #include<stdio.h>

    2. #define sizeofbuf 512    

    3. int main(int argc,char **argv)

    4. {

    5.         char    buf[sizeofbuf];

    6.         FILE    *fp;

    7.         char     ch;

    8.         snprintf(buf,sizeof(buf),"ifconfig |grep -v 127.0.0.1|grep 'inet addr'|awk '{print $2}'|cut -d ":" -f2");

    9.         fp = popen(buf,"r");

    10.         if( NULL == fp)

    11.         {

    12.                  printf("error");

    13.                  return -1;

    14.         }

    15.         printf("var ip = "");

    16.         while( EOF != (ch=fgetc(fp)) )

    17.         {

    18.                 if (ch == ' ')

    19.                         ch = ''; //去除换行符

    20.                 else{

    21.                         fputc(ch,stdout);

    22.                 }

    23.         }

    24.         printf("" ");

    25.         pclose(fp);//close piping 

    26.         return 0;

    27. }

    28. ~           

       编译后运行成功获取本机IP    

  • 相关阅读:
    全局变量引用与声明
    Oracle基础~dg原理
    Oracle基础~dg管理
    Oracle基础~rman克隆
    oracle基础~rman恢复篇
    oracle基础~linux整体性能优化
    oracle基础~报错汇总与解决办法
    oracle基础~用户和权限
    oracle基础~rac-asm
    oracle基础~awr报告
  • 原文地址:https://www.cnblogs.com/yelang-lkg/p/6611339.html
Copyright © 2011-2022 走看看