User avatar
XueHai8
Posts: 70
Joined: Mon Jul 24, 2017 12:19 pm

Distributable Bash Scripts with Predictable Network Interface Names?

Wed Sep 13, 2017 8:32 am

I realize the latest patch for Stretch disables Predictable Network Interface Names (aka MAC Address Base Network Naming), but this only postpones the inevitable. So my question has to do with writing Bash scripts that can be distributed to any Stretch Pi (PNIN or not) that references a Ethernet or Wireless adapter.

For instance, in pre-Stretch, this could work on just about any Pi with an Ethernet adapter:

Code: Select all

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
But under Predictable Network Names, the Ethernet adapter Name is different on every device. So, my two (2) questions are as follows:

1) How do you code a Bash script to use whatever network naming is being used where it is run - without the user having to muck with it?
2) Where do you push a Bash script to execute on Boot such that it runs after the network names have been assigned?

Thank you.

hippy
Posts: 7728
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Distributable Bash Scripts with Predictable Network Interface Names?

Wed Sep 13, 2017 9:44 am

I Googled but couldn't find any reference to anything which would specifically list what predictable interface names were in use or allow what they were to be easily identified. Best I could come up with was to take the output of 'ifconfig -a' and parse that with some Python.

I guess you could do something like that, determine what's most likely to be the interface name to use, stick it in a shell variable and use variable substitution in your bash script.

Code: Select all

#!/usr/bin/python
import os

def PrintInterfaceList():
  lst = os.popen("ifconfig -a").read().split("\n")
  for s in lst:
    s = s.rstrip()
    if not s.startswith(" "):
      n = s.find(": ")
      if n > 0 :
        print s[:n]

if __name__ == "__main__":
  PrintInterfaceList() 
I believe this shows how a predictable name is determined -

https://github.com/systemd/systemd/blob ... n-net_id.c

That should help identify what an interface is from its name; eg starts with "en" for ethernet, and one can then determine if it's a USB interface or something else etc.

The Pi LAN chip may get a reasonably constant prefix across systems, and if there isn't a wired NIC then look for a USB NIC.

Things won't work well with more than one NIC, but it didn't work well with the older convention either when it couldn't be guaranteed which was eth0 or eth1.
Last edited by hippy on Wed Sep 13, 2017 10:27 am, edited 1 time in total.

mikerr
Posts: 2825
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK
Contact: Website

Re: Distributable Bash Scripts with Predictable Network Interface Names?

Wed Sep 13, 2017 10:26 am

I find it darkly hilarious that "predicable" interface names aren't as easily predicable as before :roll:
-mainly due to the fallback into MAC based names.

The following one liner should return the interface name:

Code: Select all

ifconfig -a | grep -m1 -o "^\w*\b"
enxb827eb2320b9

The -m1 limits it to first interface, remove that and you get all interface names


so in a script:

Code: Select all

eth0=(ifconfig -a | grep -m1 -o "^\w*\b")
echo $eth0
Last edited by mikerr on Wed Sep 13, 2017 10:36 am, edited 2 times in total.
Android app - Raspi Card Imager - download and image SD cards - No PC required !

User avatar
RaTTuS
Posts: 10559
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Distributable Bash Scripts with Predictable Network Interface Names?

Wed Sep 13, 2017 10:29 am

see also
viewtopic.php?f=28&t=193142#p1210463
so new versions will not have this issue
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

hippy
Posts: 7728
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Distributable Bash Scripts with Predictable Network Interface Names?

Wed Sep 13, 2017 10:46 am

mikerr wrote:
Wed Sep 13, 2017 10:26 am
I find it darkly hilarious that "predicable" interface names aren't as easily predicable as before :roll:
IMO that "predictable" is a bad choice of term; "well defined" might have been better.

Perception of the change is slanted because many users will only ever have one NIC so "eth0" is entirely predictable to them; they knew how the NIC would be named and they knew what "eth0" would refer to.

Have two or more NIC's and things are no longer predictable at all.

But most people using "eth0" probably never realised that and never fully considered the situation beyond having only one NIC. What they call "eth0" may not be that on someone else's system, and might not be if they add a USB NIC themselves.

But "so what do I put in place of eth0?" is a valid question and a stumbling block for everyone who only uses one NIC.

Things would be greatly alleviated if there were a well defined and catered for means of referring to 'the only NIC I have' which is how "eth0" was often taken to mean.

hippy
Posts: 7728
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Distributable Bash Scripts with Predictable Network Interface Names?

Wed Sep 13, 2017 11:05 am

RaTTuS wrote:
Wed Sep 13, 2017 10:29 am
see also
viewtopic.php?f=28&t=193142#p1210463
so new versions will not have this issue
Though anyone writing a bash script to share will have to consider that others may have re-enabled predictable naming.

It is not clear to me what "Disable predictable network interface names for Ethernet devices" means; that predictable naming will be impossible to turn on, or merely that Raspbian will be provided with it turned off by default ?

And does it mean predictable naming is disabled only for that which would have got an "en" prefix under the predictable naming scheme or is it wider reaching than that ?

As an aside; no one should be distributing scripts which refer to "eth0" unless they have checked that there is one and only one NIC otherwise they cannot guarantee what that "eth0" will refer to.

hippy
Posts: 7728
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Distributable Bash Scripts with Predictable Network Interface Names?

Wed Sep 13, 2017 11:26 am

hippy wrote:
Wed Sep 13, 2017 10:46 am
Things would be greatly alleviated if there were a well defined and catered for means of referring to 'the only NIC I have' which is how "eth0" was often taken to mean.
Perhaps it falls to application and utility coders to accept and handle wildcards, eg 'eth*', 'en*' or even 'e*' which should handle both old and new conventions, determine if that can only be a single interface entity and use that or error report any ambiguity.

After all, that is what we are effectively trying to do here; determine what interface "e*" now is.

I have arrived at the conclusion that I don't mind predictable naming, actually see it as a good thing. It's just that it's not easy to use that predictable naming, requires all users to know things that many don't care to know.

User avatar
XueHai8
Posts: 70
Joined: Mon Jul 24, 2017 12:19 pm

Re: Distributable Bash Scripts with Predictable Network Interface Names?

Thu Sep 14, 2017 1:29 am

I'm still unsure where to put a bash script such that on boot it can read and determine the network interface name.
rc.local doesn't work - it runs too early (before the network names have been assigned).
And @boot in crontab also executes before network names have been assigned.
At the moment, I have a 'sleep 30' in my bash script to wait a while before getting network interface names on boot - and that's an ugly hack.

P.S. Thank you everyone for your advice on getting the network interface names - that helps me a lot!

Return to “Troubleshooting”