APUE之从eth0中获取ip地址

1, 使用pipe创建一个管道,并使用fork()创建一个子进程;
2, 父进程关闭管道的写端,子进程关闭管道的读端(思考为什么?)
3, 子进程重定向标准输出为管道的写端(想想上一次布置的任务怎么做重定向)
4, 子进程执行ifconfig eth0命令,这时命令的输出将会输出到到管道上;
5, 父进程从管道里读取字符串,并用字符串解析函数获取到IP地址


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>

#define maxsize     1024
/********************************************************************************
 *  Description:
 *   Input Args:
 *  Output Args:
 * Return Value:
 ********************************************************************************/
int main (int argc, char **argv)
{
    size_t     n;
    int     i = 0, m = 0;
    int     fd[2];
    pid_t   pid;
    FILE    *fp;
    char    line[maxsize];
    char    *pipe_buf = NULL;
    char    *get_buf[maxsize];
    char    *addr_buf = NULL;
    char    *ipaddr[2];

    if (pipe(fd) < 0)
    {
        printf("pipe failure: %s\n", strerror(errno));
        exit(-1);
    }

    if ((pid = fork()) < 0)
    {
        printf("fork() failure: %s\n", strerror(errno));
        exit(-1);
    }

    else if (pid > 0)
    {
        close(fd[1]); /* close write */
        memset(line, 0, maxsize);
        if ((n = read(fd[0], line, maxsize)) < 0)
        {
            printf("read failure: %s\n", strerror(errno));
            exit(-1);

        }

        pipe_buf = line; /* read string from pipe */
        while ((get_buf[i] = strtok(pipe_buf, "  ")) != NULL)
        {
            i++;
            pipe_buf = NULL;
        }

        i = 0;
        while (get_buf[i] != NULL)
        {
            if ((addr_buf = strstr(get_buf[i], "addr:")) !=NULL)
            {
                 ipaddr[m++] = addr_buf;
            }
            i++;
        } /* analysis string "addr:xxx.xxx.xxx.xxx" from eth0 */


        i = 0;
        addr_buf = ipaddr[0];
        while ((ipaddr[i] = strtok(addr_buf, ":")) != NULL)
        {
            i++;
            addr_buf = NULL;
        } /* analysis ipaddr "xxx.xxx.xxx.xxx"  from "addr:xxx.xxx.xxx.xxx"*/

        addr_buf = ipaddr[1]; /* ipaddr */
        printf("%s\n", addr_buf);

        close(fd[0]);
        if (waitpid(pid, NULL, 0) < 0)
        {
            printf("wait for child process failure: %s\n", strerror(errno));
            exit(-1);
        }

        exit(0);
       } /* else if */

    else /*client  */
    {

        close(fd[0]); /* close read */
        if (fd[1] != STDOUT_FILENO)
        {
            if (dup2(fd[1],STDOUT_FILENO) != STDOUT_FILENO)
            {
                printf("copy STDOUT_FILENO to fd[1] failure: %s\n", strerror(errno));
                return(-1);
            }
            close(fd[1]);
        }

        if (execl("/sbin/ifconfig", "ifconfig", "eth0", (char *)0 ) < 0)
        {
            printf("execl failure: %s\n", strerror(errno));
            exit(-1);
        }

    }
    return 0;
} /* ----- End of main() ----- */

基本功能能实现,就是代码有点繁琐.菜鸟一只.以后还要多加努力.本来是要求弄成函数的,但是自己懒,不想去弄了. 要想弄成函数直接返回buf_addr就可以了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值