c - How to find out if the eth0 mode is static or dhcp? -
i want use c program if ip of network interface set manually or via dhcp.
i've tried use following code , has worked in debian, hasn't worked in openwrt. want know how write c program doing in openwrt. have tried 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 working in debian, isn't working in openwrt, want know how write program same result.
for openwrt can such information following command:
$uci network.lan.proto
so take program put in question , change command used information:
#include <stdio.h> <br> int main(void) { file *fp; char buffer[80]; fp=popen("uci network.lan.proto","r"); fgets(buffer, sizeof(buffer), fp); printf("%s", buffer); pclose(fp); }
to see network interfaces available in openwrt can use following command:
$uci show network
you can avoid using calling linux command in c using libuci
. libuci
contains c function execute uci commands without passing via popen ( popen
used execute external command shell).
the libuci exist default in development environment of openwrt, not need download it, no need build , no need install on openwrt machine
you can use libuci in 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 compile program have add -luci
in compilation command gcc
Comments
Post a Comment