How to find out if eth0 mode is static or dhcp?

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

I tried using the following code and it worked in Debian, but it did not work in OpenWrt. I want to know how to write a C program in OpenWrt. I tried using 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 works in Debian, but it does not work fine in OpenWrt, so I want to know how to write a program to get the same result.

+5
source share
3 answers

for OpenWRT, you can get this information with the following command:

$uci get network.lan.proto

, , , :

#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);
}

, OpenWRT, :

$uci show network

linux c libuci. libuci C uci popen (popen ).

libuci OpenWRT, , OpenWRT.

libuci

#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);
}

( )

, -luci gcc

+7

, , . ( Linux, , GNU/Linux) , ( ), , , . OpenWRT GNU, -.

+2

AFAIK.

: , .

"" DBUS, . , dhclient. /etc, .

I think the most reliable option would be a layered thing: check out the entire set of hints to come up with an answer.

Another option: send a DHCP control packet to the dhcp server to verify the address. If you do not receive a response, although it may be that the network is disconnected, it was turned on when the address was allocated.

+1
source

All Articles