Web lists-archives.com

Re: ifconfig/ ifupdown/ ip -




On Mon, Jul 09, 2018 at 09:16:51PM +1000, Zenaan Harkness wrote:
> On Mon, Jul 09, 2018 at 11:01:22AM +0100, Darac Marjal wrote:
> > On Mon, Jul 09, 2018 at 11:52:36AM +1000, Zenaan Harkness wrote:
> > > So I change between two internet connections from time to time.
> > > 
> > > I use /etc/network/interfaces ("/e/n/i")
> > > 
> > > When I modify /e/n/i , I then run a little "reset" script, like so:
> > > 
> > > dev=eth0
> > > ifdown $dev
> > > ifconfig $dev down
> > > ifup $dev
> > > 
> > > Here and there I've had problems.
> > > 
> > > Recently I discovered the ip command.
> > > 
> > > Apparently, after reconfiguring as above, two IP addresses end up
> > > attached to eth0 - one for each (staticly configured in /e/n/i)
> > > ISP network connection.
> > > 
> > > So now I am manually running something like:
> > > 
> > > ifconfig eth0 down
> > > ip address del 10.1.1.30/24 dev eth0
> > > ip address del 192.168.1.30/24 dev eth0
> > > ip address del fe80::f2de:f1ff:fef7:ea96/64 dev eth0
> > > ifup eth0
> > > 
> > > But this (atm) is a very manual process, and it seems to me that I am
> > > not taking down eth0 properly, and that I should not have to
> > > introduce IP address awareness into my eth reset script, just to
> > > properly reset my eth0 static configuration.
> > > 
> > > Any pointers of what I need to read/ what I am missing, would be
> > > really appreciated.
> > > 
> > 
> > ifup and ifdown read the contents of /e/n/i to determine what tasks to perform. So, in particular, ifdown reads that
> > file to determine what addresses to remove from the interface.
> > 
> > That being said, you might find it more suitable to change your script to:
> >  1. ifdown the interface
> >  2. Change the network parameters (either by using sed etc to edit the     file or to move 'canned' files in/out of
> > /etc/network/interfaces.d)
> >  3. ifup the interface
> > 
> > What I would do, in your situation is to have a set of files in /etc/network/interfaces.d called, for example "!home",
> > "!work", "!travel" or whatever makes sense for your locations. The leading "!" character makes the configuration
> > invalid to ifupdown, so will be ignored. Your script can then rename any files that DON'T have a "!" in front to do
> > so, then rename the requested file to NOT have a "!" in front e.g.
> > 
> >  #!/bin/bash
> >  ifdown eth0
> >  prename 's/^/!' /etc/network/interfaces.d/[^\!]*
> >  mv /etc/network/interfaces.d/\!$1 /etc/network/interfaces.d/$1
> >  ifup eth0
> 
> Perfect! Thanks.
> Also perhaps interface map[ping] could work... I used that once years
> ago... need to re-read man interfaces again.

OK, so I learnt a bit more about interfaces mapping stanzas, and bash
parameter suffix removal:

Since I have two mappings ("office" and "internet"), when eth0 is
configured to say office, I want to be able to run my reset bash
script as follows:

 reset eth0=internet

Now "ifdown eth0=internet" when it's configured for office, might not
work, even though "ifup eth0=internet" would be fine.

We don't need to guess the previous state though, we just need to
remove the virtual interfaces specifier ("=internet") like so inside
the reset script:

 neweth="$1" # e.g. "eth0=internet"
 ifdown ${neweth%=*} # remove "=..." suffix
 ifup $neweth # include suffix

Voi la :)

Thanks everyone, really appreciated!