如何找出如果eth0的方式是静态或DHCP? [英] How to find out if the eth0 mode is static or dhcp?

查看:496
本文介绍了如何找出如果eth0的方式是静态或DHCP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个C程序获取如果网络接口的IP手动或通过DHCP设置。

我已经尝试使用下面的code和它在Debian中工作过,但还没有在工作的OpenWrt。我想知道如何编写一个C程序中的OpenWrt这样做。
我曾尝试使用这样的:

 的#include<&stdio.h中GT;
INT主要(无效)
{
    FILE * FP;
    字符缓冲区[80];
    FP =的popen(猫的/ etc /网络/接口| grep的^ IFACE \\\\ BR-LAN | awk的-F'''{打印$ 4}',R);
    与fgets(缓冲区,缓冲区尺寸,FP);
    的printf(%S,缓冲区);
    pclose函数(FP);
}

这code是工作在Debian的,但它不是在的OpenWrt正常工作,所以我想知道如何编写一个程序来获得相同的结果。


解决方案

对于的OpenWRT 你可以用下面的命令一个这样的信息:

  $ UCI GET network.lan.proto

所以我需要你把你的问题的程序,我只更改用于获取信息的命令:

 的#include<&stdio.h中GT; < BR>
INT主要(无效)
{
    FILE * FP;
    字符缓冲区[80];
    FP =的popen(UCI GET network.lan.proto,R);
    与fgets(缓冲区,缓冲区尺寸,FP);
    的printf(%S,缓冲区);
    pclose函数(FP);
}

要看到你的OpenWRT所有可用的网络接口,可以使用下面的命令:

  $ UCI秀网

您可以使用 libuci 避免在c。使用调用linux命令。在 libuci 包含C函数没有通过的popen传递(的popen 是用来执行从壳外部命令)来执行UCI命令

该libuci默认情况下的OpenWRT的发展环境中存在,不需要下载,不需要构建它,也不需要你的OpenWRT机器上安装它。

您可以用此方式使用libuci

 的#include< uci.h>
无效的主要()
{
    焦炭路径[] =network.lan.proto;
    字符缓冲区[80];
    结构uci_ptr PTR;
    结构uci_context * C = uci_alloc_context();    如果回报(C!);    如果((uci_lookup_ptr(C,和放大器;!PTR,路径,真实)= UCI_OK)||
        (ptr.o == NULL || ptr.o-> v.string == NULL)){
        uci_free_context(C);
        返回;
    }    如果(ptr.flags&安培; UCI_LOOKUP_COMPLETE)
            的strcpy(缓冲区,ptr.o-> v.string);    uci_free_context(C);    的printf(%S \\ n,缓冲区);
}

(未测试)

当你编译程序,你必须在编译命令 GCC -luci >

I want to use a C program to get if the ip of the network interface is set manually or via dhcp.

I've tried to use the following code and it has worked in Debian, but it hasn't worked in OpenWrt. I want to know how to write a C program doing this in OpenWrt. I have tried to use this:

#include <stdio.h>
int main(void)
{
    FILE *fp;
    char buffer[80];
    fp=popen("cat /etc/network/interfaces |grep ^iface\\ br-lan | awk -F ' ' '{print $4}'","r");
    fgets(buffer, sizeof(buffer), fp);
    printf("%s", buffer);
    pclose(fp);
}

This code is working in Debian, but it isn't working normally in OpenWrt, so I want to know how to write a program to get the same result.

解决方案

for OpenWRT you can get a such information with the following command:

$uci get network.lan.proto

so I take the program you put in your question and I change only the command used to get information:

#include <stdio.h> <br>
int main(void)
{
    FILE *fp;
    char buffer[80];
    fp=popen("uci get network.lan.proto","r");
    fgets(buffer, sizeof(buffer), fp);
    printf("%s", buffer);
    pclose(fp);
}

to see all network interfaces available in your OpenWRT you can use the following command:

$uci show network

You can avoid using calling linux command in your c by using the libuci. The libuci contains C function to execute uci commands without passing via popen ( popen is used to execute external command from shell).

The libuci exist by default in the development environment of OpenWRT, not need to download it, no need to build it and no need to install it on your OpenWRT machine

You can use libuci in this way

#include <uci.h>
void main()
{
    char path[]="network.lan.proto";
    char buffer[80];
    struct  uci_ptr ptr;
    struct  uci_context *c = uci_alloc_context();

    if(!c) return;

    if ((uci_lookup_ptr(c, &ptr, path, true) != UCI_OK) ||
        (ptr.o==NULL || ptr.o->v.string==NULL)) { 
        uci_free_context(c);
        return;
    }

    if(ptr.flags & UCI_LOOKUP_COMPLETE)
            strcpy(buffer, ptr.o->v.string);

    uci_free_context(c);

    printf("%s\n", buffer);
}

(Not tested)

and when you compile your program you have to add the -luci in the compilation command gcc

这篇关于如何找出如果eth0的方式是静态或DHCP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆