Skip to content

Instantly share code, notes, and snippets.

@aniruddha-a
Last active August 29, 2015 14:08
Show Gist options
  • Save aniruddha-a/ec2bc24622550963bdef to your computer and use it in GitHub Desktop.
Save aniruddha-a/ec2bc24622550963bdef to your computer and use it in GitHub Desktop.
augeas crud on /etc/network/interfaces
// Test CRUD of an iface in /etc/network/interfaces ( on Ubuntu)
// using the Augeas lib
// Aniruddha. A (aniruddha.a@gmail.com)
// Nov 1, 2014
#include <stdio.h>
#include <assert.h>
#include <stdarg.h>
#include <stddef.h>
#include <limits.h>
#include <string.h>
#include <augeas.h>
// using the .so installed from libaugeas-dev :
// gcc -I /usr/include/libxml2/ augparse.c -laugeas
//
// to run, just install libaugeas0 ( minus dev files )
#define LENS_PATH "/usr/share/augeas/lenses/dist/"
void if_create (augeas *aug, const char *ifname, int nfields, ...)
{
char key[128] = {0}, val[128] = {0};
char *p = NULL;
char *kv = NULL;
va_list ap;
int ret, i;
char iffield_path[256]= {0};
assert(aug && ifname && (nfields > 0));
ret = aug_set(aug, "/files/etc/network/interfaces/iface[last()+1]", ifname);
assert(ret == 0);
va_start(ap, nfields);
for (i = 0; i < nfields; i++) {
kv = va_arg(ap,char*);
p = strchr(kv, '=');
if (!p) continue;
strncpy(key, kv, (ptrdiff_t)(p - kv));
key[p - kv] = '\0';
strcpy(val, p + 1);
sprintf(iffield_path, "/files/etc/network/interfaces/iface[last()]/%s",
key);
ret = aug_set(aug, iffield_path, val);
assert(ret == 0);
}
va_end(ap);
}
void if_update (augeas *aug, const char *ifname, int nfields, ...)
{
char key[128] = {0}, val[128] = {0};
char *p = NULL;
char *kv = NULL;
va_list ap;
int ret, i;
char iffield_path[256]= {0};
assert(aug && ifname && (nfields > 0));
va_start(ap, nfields);
for (i = 0; i < nfields; i++) {
kv = va_arg(ap,char*);
p = strchr(kv, '=');
if (!p) continue;
strncpy(key, kv, (ptrdiff_t)(p - kv));
key[p - kv] = '\0';
strcpy(val, p + 1);
sprintf(iffield_path, "/files/etc/network/interfaces/iface[. = '%s']/%s",
ifname, key);
ret = aug_set(aug, iffield_path, val);
assert(ret == 0);
}
va_end(ap);
}
void if_rm (augeas *aug, const char *ifname)
{
int ret;
char ifpath[256]= {0};
assert(aug && ifname);
sprintf(ifpath, "/files/etc/network/interfaces/iface[. = '%s']", ifname);
ret = aug_rm(aug, ifpath);
assert(ret != -1);
}
// TEST
int main (int argc, char *argv[])
{
augeas *aug = NULL;
aug = aug_init("/", LENS_PATH, AUG_SAVE_BACKUP|AUG_NO_ERR_CLOSE);
if (!aug || (aug_error(aug) != AUG_NOERROR)) {
fprintf(stderr, "Init failed: %s\n", aug_error_message(aug));
exit(1);
}
if_create(aug, "eth2", 5,
"family=inet", "method=static",
"address=1.1.1.1", "netmask=255.255.0.0",
"mtu=1500" );
aug_save(aug);
system("cat /etc/network/interfaces");
if_update(aug, "eth2", 1, "address=2.2.2.2");
aug_save(aug);
system("cat /etc/network/interfaces");
if_rm(aug, "eth2");
aug_save(aug);
system("cat /etc/network/interfaces");
aug_close(aug);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment