FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Debian > Debian User

 
 
LinkBack Thread Tools
 
Old 04-12-2008, 02:36 AM
Tyler MacDonald
 
Default (hopefully perl) API to /etc/network/interfaces?

Hi,

I know that reading /etc/network/interfaces has been done over and
over... has an API been created for editing/writing it? If it's available in
perl, or over the commandline, that would be even better :-)

Thanks,
Tyler


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 04-12-2008, 02:27 PM
"Douglas A. Tutty"
 
Default (hopefully perl) API to /etc/network/interfaces?

On Fri, Apr 11, 2008 at 07:36:56PM -0700, Tyler MacDonald wrote:
> I know that reading /etc/network/interfaces has been done over and
> over... has an API been created for editing/writing it? If it's available in
> perl, or over the commandline, that would be even better :-)

Its just a plain text file. Use an editor to edit it.

What is it you are trying to do?

Doug.


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 04-12-2008, 04:15 PM
Tyler MacDonald
 
Default (hopefully perl) API to /etc/network/interfaces?

Douglas A. Tutty <dtutty@porchlight.ca> wrote:
> On Fri, Apr 11, 2008 at 07:36:56PM -0700, Tyler MacDonald wrote:
> > I know that reading /etc/network/interfaces has been done over and
> > over... has an API been created for editing/writing it? If it's available in
> > perl, or over the commandline, that would be even better :-)
>
> Its just a plain text file. Use an editor to edit it.
>
> What is it you are trying to do?

Whenever I order a new server, my ISP puts all of my additional IPs into
the interfaces file (as they generally should). I use these IPs to deploy
xen virtual machines, so I do not want them brought up by the physical
machine. Also, my xen deployments are configured through a YAML file. I'd
like to read the interfaces file, add the IPs to my YAML file on the
management server, and then remove them from the interfaces file.

Yes this could be scripted, I was just wondering if there was an official
API that things like the "System -> Administration -> Network" applet uses
that I could also use.

- Tyler


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 05-26-2011, 10:16 PM
Mike Mestnik
 
Default (hopefully perl) API to /etc/network/interfaces?

In-Reply-To: <20080412023656.GR14599@yi.org>

Hello,
This is an old thread, but I find myself in a similar situation. I'd
like to edit a 4in6 tunnel endpoint and reconfigure the interface.
While I can handle the Apache/sudo parts of this I'm stuck with... OK
now how and I going to change this one value out of this whole file?

My current plan is to convert the file to XML storing away comments and
partial white space as well as the settings. Then I should be able to
edit the setting a write the file back out.

Any better ideas? For this project Perl seams to be the ideal language.

I may contact the ifupdown maintainer to include this in his package, so
I'm looking to get it done right.


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 4DDED155.3050402@mikemestnik.net">http://lists.debian.org/4DDED155.3050402@mikemestnik.net
 
Old 05-26-2011, 10:55 PM
Thierry Chatelet
 
Default (hopefully perl) API to /etc/network/interfaces?

On Friday 27 May 2011 00:16:53 Mike Mestnik wrote:
> In-Reply-To: <20080412023656.GR14599@yi.org>

which is here:
http://lists.debian.org/debian-user/2008/04/msg01249.html

but, sorry, it's way above my knowledge.
Thierry


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 201105270055.34858.tchatelet@free.fr">http://lists.debian.org/201105270055.34858.tchatelet@free.fr
 
Old 05-26-2011, 11:06 PM
John Hasler
 
Default (hopefully perl) API to /etc/network/interfaces?

Mike Mestnik writes:
> My current plan is to convert the file to XML storing away comments
> and partial white space as well as the settings. Then I should be
> able to edit the setting a write the file back out.

Why the XML? Why not just parse the file?
--
John Hasler


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 877h9doyeb.fsf@thumper.dhh.gt.org">http://lists.debian.org/877h9doyeb.fsf@thumper.dhh.gt.org
 
Old 05-31-2011, 02:24 AM
Mike Mestnik
 
Default (hopefully perl) API to /etc/network/interfaces?

On 05/26/11 18:13, William Hopkins wrote:
> On 05/26/11 at 05:16pm, Mike Mestnik wrote:
>> In-Reply-To: <20080412023656.GR14599@yi.org>
>>
>> Hello,
>> This is an old thread, but I find myself in a similar situation. I'd
>> like to edit a 4in6 tunnel endpoint and reconfigure the interface.
>> While I can handle the Apache/sudo parts of this I'm stuck with... OK
>> now how and I going to change this one value out of this whole file?
>>
>> My current plan is to convert the file to XML storing away comments and
>> partial white space as well as the settings. Then I should be able to
>> edit the setting a write the file back out.
>>
>> Any better ideas? For this project Perl seams to be the ideal language.
>>
>> I may contact the ifupdown maintainer to include this in his package, so
>> I'm looking to get it done right.
> Generally, there's no desire for added abstractions to configuration files.
> If you need to have network things done automatically which can't be handled by the file itself, add post-ups and write some scripts. Or write scripts which parse current network info from ifconfig/ip. /etc/network/interfaces is a file used by the debian networking scripts to set up your default interfaces, not the be-all end-all of networking configuration.
>
I hadn't though of that. I was merely trying to automate/script a task
that was being performed manually. The task included changing this
configuration file. The solution may be to abstract parts of this
configuration file that would then be updated by scripts/users doing
things manually.

I believe you have slightly misunderstood me. I need(would like to) to
alter network settings based on CGI scripts from Apache. Can these
variables be abstracted? I don't see how a pre-up script can
effect/alter configuration settings. As an example the address or
gateway settings for a static method. In my experience I'm specifically
trying to alter the "endpoint" setting for a "v4tunnel" method.

I'm just as confused trying to accomplish this as I was when I started.
It's something a user can do simple with an editor, but trying to do it
programing and correctly is my stumbling block. "IF" I could store the
value of this setting in a file that would solve my issue, I can replace
the contents of the file and then proceed as usual.


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 4DE45172.1070507@mikemestnik.net">http://lists.debian.org/4DE45172.1070507@mikemestnik.net
 
Old 05-31-2011, 02:32 AM
William Hopkins
 
Default (hopefully perl) API to /etc/network/interfaces?

On 05/30/11 at 09:24pm, Mike Mestnik wrote:
> On 05/26/11 18:13, William Hopkins wrote:
> > On 05/26/11 at 05:16pm, Mike Mestnik wrote:
> >> In-Reply-To: <20080412023656.GR14599@yi.org>
> >>
> >> Hello,
> >> This is an old thread, but I find myself in a similar situation. I'd
> >> like to edit a 4in6 tunnel endpoint and reconfigure the interface.
> >> While I can handle the Apache/sudo parts of this I'm stuck with... OK
> >> now how and I going to change this one value out of this whole file?
> >>
> >> My current plan is to convert the file to XML storing away comments and
> >> partial white space as well as the settings. Then I should be able to
> >> edit the setting a write the file back out.
> >>
> >> Any better ideas? For this project Perl seams to be the ideal language.
> >>
> >> I may contact the ifupdown maintainer to include this in his package, so
> >> I'm looking to get it done right.
> > Generally, there's no desire for added abstractions to configuration files.
> > If you need to have network things done automatically which can't be handled by the file itself, add post-ups and write some scripts. Or write scripts which parse current network info from ifconfig/ip. /etc/network/interfaces is a file used by the debian networking scripts to set up your default interfaces, not the be-all end-all of networking configuration.
> >
> I hadn't though of that. I was merely trying to automate/script a task
> that was being performed manually. The task included changing this
> configuration file. The solution may be to abstract parts of this
> configuration file that would then be updated by scripts/users doing
> things manually.

Oh, I didn't ascribe any evil intentions to what you were doing. Just trying to point out why there might not be a lot of community response to your offer. However, as always I represent no-one but myself (:

> I believe you have slightly misunderstood me. I need(would like to) to
> alter network settings based on CGI scripts from Apache. Can these
> variables be abstracted? I don't see how a pre-up script can
> effect/alter configuration settings. As an example the address or
> gateway settings for a static method. In my experience I'm specifically
> trying to alter the "endpoint" setting for a "v4tunnel" method.

I don't use IPv6 tunneling myself, so I am unsure. But, for example, you would alter /etc/network/interfaces if you wanted to change a variable permanently. Think of it as a list of saved settings for the interfaces. It's read by the networking scripts, which then run various commands such as ifconfig based on the data there. And for anything the networking scripts can't handle directly, there's the pre/post commands to call external scripts.
If you only wish to change something once, you would simply run the commands directly (ifconfig, for example).

> I'm just as confused trying to accomplish this as I was when I started.
> It's something a user can do simple with an editor, but trying to do it
> programing and correctly is my stumbling block. "IF" I could store the
> value of this setting in a file that would solve my issue, I can replace
> the contents of the file and then proceed as usual.

If you're sure you want to change the permanent setting of something which is currently set in /etc/network/interfaces for setting at boot-time, simply have your script change it. There are a lot of ways to edit text data in linux -- sed is a good option, as are cut, awk, grep, etc.


--
Liam
 
Old 05-31-2011, 08:45 AM
Mike Mestnik
 
Default (hopefully perl) API to /etc/network/interfaces?

On 05/30/11 21:24, Mike Mestnik wrote:
> On 05/26/11 18:13, William Hopkins wrote:
>> On 05/26/11 at 05:16pm, Mike Mestnik wrote:
>>> In-Reply-To: <20080412023656.GR14599@yi.org>
>>>
>>> Hello,
>>> This is an old thread, but I find myself in a similar situation. I'd
>>> like to edit a 4in6 tunnel endpoint and reconfigure the interface.
>>> While I can handle the Apache/sudo parts of this I'm stuck with... OK
>>> now how and I going to change this one value out of this whole file?
>>>
>>> My current plan is to convert the file to XML storing away comments and
>>> partial white space as well as the settings. Then I should be able to
>>> edit the setting a write the file back out.
>>>
>>> Any better ideas? For this project Perl seams to be the ideal language.
>>>
>>> I may contact the ifupdown maintainer to include this in his package, so
>>> I'm looking to get it done right.
>> Generally, there's no desire for added abstractions to configuration files.
>> If you need to have network things done automatically which can't be handled by the file itself, add post-ups and write some scripts. Or write scripts which parse current network info from ifconfig/ip. /etc/network/interfaces is a file used by the debian networking scripts to set up your default interfaces, not the be-all end-all of networking configuration.
>>
> <SNIP>
> I believe you have slightly misunderstood me. I need(would like to) to
> alter network settings based on CGI scripts from Apache. Can these
> variables be abstracted? I don't see how a pre-up script can
> effect/alter configuration settings. As an example the address or
> gateway settings for a static method. In my experience I'm specifically
> trying to alter the "endpoint" setting for a "v4tunnel" method.
>
> I'm just as confused trying to accomplish this as I was when I started.
> It's something a user can do simple with an editor, but trying to do it
> programing and correctly is my stumbling block. "IF" I could store the
> value of this setting in a file that would solve my issue, I can replace
> the contents of the file and then proceed as usual.
>
Here is what I've come up with thus far.

sudoers:
www-data ALL=(root) SETENV: NOPASSWD: /usr/local/sbin/6in4cfg.pl

Current perl run as root:
#!/usr/bin/perl -wT

use strict;
use warnings;
use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use Data:umper;

print header;
warningsToBrowser(1);
print start_html('Me.'), pre(escapeHTML(Dumper(\%ENV))),end_html();


Calling CGI:
/* me.c */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#define ERRORSTR "%sContent-Type: text/html;
charset=ISO-8859-1

<!DOCTYPE html
/html;PUBLIC "-//W3C//DTD XHTML
1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml" lang="en-US"
xml:lang="en-US">
<head>
<title>%s</title>
<meta
http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>
</head>
<body>
<pre>%s</pre>
</body>
</html>"

int main(int argc, char *argv[]) /* , char *envp[]) */
{
char * env;
struct in_addr *n;
env=getenv ("AUTH_TYPE");
if (!env) {
printf(ERRORSTR, "Status: 403 No Authoriz
", "Me.", "No
Authoriz.");
return 0; }
if (strncmp(env,"Negotiate",8)) {
printf(ERRORSTR, "Status: 403 Not Authorized
", env,
"Not Authorized.");
return 0; }
env=getenv ("REMOTE_USER");
if (!env) {
printf(ERRORSTR, "Status: 403 No User
", "Me.", "No User.");
return 0; }
if (strncmp(env,"host/purgatory.mikemestnik.net",30)) {
printf(ERRORSTR, "Status: 403 Wrong User
", env, "Wrong User.");
return 0; }
env=getenv ("REMOTE_ADDR");
if (!env) {
printf(ERRORSTR, "Status: 400 Bad request", "Me.", "Bad request.");
return 0; }
if (inet_aton(env,n)) {
/* printf(ERRORSTR, "", env, "Good."); */
execlp("sudo","sudo","-En","/usr/local/sbin/6in4cfg.pl");
return 0; }
printf(ERRORSTR, "Status: 400 Request Failed
", "Me.", "Request
Failed.");

return 0;
}


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 4DE4AAC4.1080107@mikemestnik.net">http://lists.debian.org/4DE4AAC4.1080107@mikemestnik.net
 
Old 05-31-2011, 09:12 AM
Mike Mestnik
 
Default (hopefully perl) API to /etc/network/interfaces?

On 05/30/11 21:24, Mike Mestnik wrote:
> On 05/26/11 18:13, William Hopkins wrote:
>> On 05/26/11 at 05:16pm, Mike Mestnik wrote:
>>> In-Reply-To: <20080412023656.GR14599@yi.org>
>>>
>>> Hello,
>>> This is an old thread, but I find myself in a similar situation. I'd
>>> like to edit a 4in6 tunnel endpoint and reconfigure the interface.
>>> While I can handle the Apache/sudo parts of this I'm stuck with... OK
>>> now how and I going to change this one value out of this whole file?
>>>
>>> My current plan is to convert the file to XML storing away comments and
>>> partial white space as well as the settings. Then I should be able to
>>> edit the setting a write the file back out.
>>>
>>> Any better ideas? For this project Perl seams to be the ideal language.
>>>
>>> I may contact the ifupdown maintainer to include this in his package, so
>>> I'm looking to get it done right.
>> Generally, there's no desire for added abstractions to configuration files.
>> If you need to have network things done automatically which can't be handled by the file itself, add post-ups and write some scripts. Or write scripts which parse current network info from ifconfig/ip. /etc/network/interfaces is a file used by the debian networking scripts to set up your default interfaces, not the be-all end-all of networking configuration.
>>
> I hadn't though of that. I was merely trying to automate/script a task
> that was being performed manually. The task included changing this
> configuration file. The solution may be to abstract parts of this
> configuration file that would then be updated by scripts/users doing
> things manually.
>
> I believe you have slightly misunderstood me. I need(would like to) to
> alter network settings based on CGI scripts from Apache. Can these
> variables be abstracted? I don't see how a pre-up script can
> effect/alter configuration settings. As an example the address or
> gateway settings for a static method. In my experience I'm specifically
> trying to alter the "endpoint" setting for a "v4tunnel" method.
>
> I'm just as confused trying to accomplish this as I was when I started.
> It's something a user can do simple with an editor, but trying to do it
> programing and correctly is my stumbling block. "IF" I could store the
> value of this setting in a file that would solve my issue, I can replace
> the contents of the file and then proceed as usual.
>
To: William Hopkins <we.hopkins@gmail.com>

I didn't get your reply, but I see it in the archives. Your advice that
perhaps there is another method for effecting the contents of
network/interfaces, for example calling a post-up script, was taken to
hart. This indeed would perhaps be better then continually altering a
system config file.

I'm fumbling a bit with how to actually implement this. I don't see a
clear way to abstract settings, take this example:

auto eth0
iface eth0 inet static
address $(cat /etc/network/eth0-address)
netmask 255.255.255.0
gateway 186.106.158.1
dns-nameservers 8.8.8.8 8.8.4.4

AFAIK there is no simple way to have the above address setting be read
from the /etc/network/eth0-address file. I could go mucking around in
the static scripts to implement this and FAIK this is already supported
and I'm just un-aware.

I'm sure you want to change the permanent setting of something which is
currently set, both the running and boot configurations. I'm a bit
unsure of what command would be needed to change the setting I wish to
alter in the running config... I was going to resort to an ifdown/ifup
combo, but now you have me rethinking that approach as well.

I'll need to adjust the configuration on several servers every time my
DSL modem reboots. The internet is not only packet switch, it's also
address switched.

The concept of using scripts to alter configuration files is where I'd
like to end up, however after carefully reading the man page for the
network/interfaces file it's clear that determining where one block ends
and another begins is a non-trivial task. I have a script that breaks
the config into blocks, though it might not always do it under any
circumstance.

This is why having a script that can convert the configuration to and
from like xml or some other format that's more generically interrogated
and modified is so important. Here might be the beginning of such a script:

#!/usr/bin/perl -w

use warnings;
use strict;
use feature 'switch';
use Data:umper;

sub trim($)
{
my $string = shift;
$string =~ s/^s+//;
$string =~ s/s+$//;
return $string;
}

open (INTER, q{</etc/network/interfaces}) or die "Can't read: $!";

my %opts;
my %ints;
my %maps;
my $ptr;
$opts{'HEAD'}=[];
$ptr = $opts{'HEAD'};
LINE: while (<INTER>) {
chomp;
my $ln=$_;
# A line may be extended across multiple lines by making the last charac-
# ter a backslash.
while ($ln =~ /$/) {
chop $ln; $ln.=<INTER>; chomp $ln; last if (eof INTER);
}
foreach my $rx (qr(auto),qr(allow-auto),qr(allow-[^ ]*),
qr(mapping),qr(iface)) {
if ($ln =~ /^s*($rx)s+(S*)s*(.*)$/) {
$ptr = undef;
given ($1){
when('mapping') { push (@{$maps{$2}}, $3); $ptr = $maps{$2}; }
when('iface') { push (@{$ints{$2}}, $3); $ptr = $ints{$2}; }
default { push (@{$opts{$1}}, trim("$2 $3")); $ptr = $opts{$1}; }
}
next LINE;
}
}
if ($ln =~ /^s*(.*)$/) {
if (defined $ptr) {
push (@$ptr, $1);
next LINE;
}
warn $ln;
next LINE;
}
warn $ln;
}
my @all;
@all=(\%opts, \%maps, \%ints);
warn Dumper @all;


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 4DE4B0F0.1010008@mikemestnik.net">http://lists.debian.org/4DE4B0F0.1010008@mikemestnik.net
 

Thread Tools




All times are GMT. The time now is 08:25 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org