[opensuse] Grabbing MAC address of interface
Hello, now the question may seem simple ("how to grab the MAC address of a given interface"), but what is actually a "portable" way that does not potentially break when upgrading to a newer openSUSE? I could fiddle with `hwinfo --network` or try parse `ip l sh dev br0` output, but what's actually best? [Using sh.] Jan -- -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Hello,
On Fri, 16 Feb 2007 14:33:12 +0100 (MET)
Jan Engelhardt
Hello,
now the question may seem simple ("how to grab the MAC address of a given interface"), but what is actually a "portable" way that does not potentially break when upgrading to a newer openSUSE?
I could fiddle with `hwinfo --network` or try parse `ip l sh dev br0` output, but what's actually best? [Using sh.]
How about this? % cat /sys/class/net/eth?/address eshsf -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
eshsf wrote:
Hello,
On Fri, 16 Feb 2007 14:33:12 +0100 (MET) Jan Engelhardt
wrote: Hello,
now the question may seem simple ("how to grab the MAC address of a given interface"), but what is actually a "portable" way that does not potentially break when upgrading to a newer openSUSE?
I could fiddle with `hwinfo --network` or try parse `ip l sh dev br0` output, but what's actually best? [Using sh.]
How about this?
% cat /sys/class/net/eth?/address
eshsf
Or the long version: /sbin/ifconfig [interface] | grep HWaddr | tail --bytes=20 | head --bytes=17 I'm sure that could be rewritten to something less ugly to ignore the last three "empty" bytes. This will work on other dists aswell, where as /sys/class/net/eth?/address does not exists on e.g. debian iirc. Best regards Sylvester Lykkehus -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Feb 16 2007 16:15, Sylvester Lykkehus wrote:
I could fiddle with `hwinfo --network` or try parse `ip l sh dev br0` output, but what's actually best? [Using sh.]
How about this?
% cat /sys/class/net/eth?/address
Wonderul, many thanks.
Or the long version: /sbin/ifconfig [interface] | grep HWaddr | tail --bytes=20 | head --bytes=17 ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
That is what I wanted to avoid because it will totally break when the locale changes.
This will work on other dists aswell, where as /sys/class/net/eth?/address does not exists on e.g. debian iirc.
Did you really need to mention the worst example! (Anyway, even debian 3.1r4/2.6.8 has that sysfs file.) Jan -- ft: http://freshmeat.net/p/chaostables/ -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Jan Engelhardt wrote:
On Feb 16 2007 16:15, Sylvester Lykkehus wrote:
I could fiddle with `hwinfo --network` or try parse `ip l sh dev br0` output, but what's actually best? [Using sh.]
How about this?
% cat /sys/class/net/eth?/address
Wonderul, many thanks.
Or the long version: /sbin/ifconfig [interface] | grep HWaddr | tail --bytes=20 | head --bytes=17
^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
That is what I wanted to avoid because it will totally break when the locale changes.
How could a locale change break a grep for HWaddr ? No really, could you give an example, I never did use another locale than EN. Even if it would, couldn't you export the environment variable first ?
This will work on other dists aswell, where as /sys/class/net/eth?/address does not exists on e.g. debian iirc.
Did you really need to mention the worst example! (Anyway, even debian 3.1r4/2.6.8 has that sysfs file.)
Of course I had to! ;-) Always consider "worst" case scenario :)
Jan
Sylvester -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Hi, On Feb 16 2007 21:16, Sylvester Lykkehus wrote:
Or the long version: /sbin/ifconfig [interface] | grep HWaddr | tail --bytes=20 | head --bytes=17
That is what I wanted to avoid because it will totally break when the locale changes.
How could a locale change break a grep for HWaddr ? No really, could you give an example, I never did use another locale than EN. Even if it would, couldn't you export the environment variable first ?
21:39 ichi:~ > ifconfig lo lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:838 errors:0 dropped:0 overruns:0 frame:0 TX packets:838 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:108107 (105.5 Kb) TX bytes:108107 (105.5 Kb) 21:39 ichi:~ > LC_MESSAGES=de_DE ifconfig lo lo Protokoll:Lokale Schleife inet Adresse:127.0.0.1 Maske:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:838 errors:0 dropped:0 overruns:0 frame:0 TX packets:838 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 Sendewarteschlangenlänge:0 RX bytes:108107 (105.5 Kb) TX bytes:108107 (105.5 Kb) As you can see: inet addr:127.0.0.1 Mask:255.0.0.0 inet Adresse:127.0.0.1 Maske:255.0.0.0 and whoops, things like /bin/cut -b or head/tail --bytes are not accurate anymore. Jan -- ft: http://freshmeat.net/p/chaostables/ -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Jan Engelhardt wrote:
Hi,
21:39 ichi:~ > ifconfig lo lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:838 errors:0 dropped:0 overruns:0 frame:0 TX packets:838 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:108107 (105.5 Kb) TX bytes:108107 (105.5 Kb)
21:39 ichi:~ > LC_MESSAGES=de_DE ifconfig lo lo Protokoll:Lokale Schleife inet Adresse:127.0.0.1 Maske:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:838 errors:0 dropped:0 overruns:0 frame:0 TX packets:838 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 Sendewarteschlangenlänge:0 RX bytes:108107 (105.5 Kb) TX bytes:108107 (105.5 Kb)
As you can see:
inet addr:127.0.0.1 Mask:255.0.0.0 inet Adresse:127.0.0.1 Maske:255.0.0.0
and whoops, things like /bin/cut -b or head/tail --bytes are not accurate anymore.
Jan
Hi again, You could have shown an interface which actually has a hardware address ;-), but I get your point; A grep for HWaddr is not valid since it changes name over different locales. However, I still think something like: /sbin/ifconfig [interface] | head -n1 | tail --bytes=20 | head --bytes=17 would always apply, since different locales does not change the position of the output of the address itself (as far as i know) Hence it would always be 1. line, last 20 chars (-3 "empty" at the end). Best regards Sylvester -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Feb 16 2007 23:04, Sylvester Lykkehus wrote:
Jan Engelhardt wrote:
Hi, 21:39 ichi:~ > ifconfig lo lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 [...]
You could have shown an interface which actually has a hardware address ;-)
ifconfig is broken. Heck, it does not even display secondary addresses
that don't have labels. Don't use it.
# ip l sh lo
1: lo:
Please explain what you mean by "portable" ? from SUSE to other SUSE, from SUSE to Debian or from SUSE to Windows XP ? -Alexey -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Friday 16 February 2007 12:40, Jan Engelhardt wrote:
Hi,
...
How could a locale change break a grep for HWaddr ? ...
21:39 ichi:~ > ifconfig lo lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 ...
21:39 ichi:~ > LC_MESSAGES=de_DE ifconfig lo lo Protokoll:Lokale Schleife inet Adresse:127.0.0.1 Maske:255.0.0.0 ...
As you can see:
inet addr:127.0.0.1 Mask:255.0.0.0 inet Adresse:127.0.0.1 Maske:255.0.0.0
and whoops, things like /bin/cut -b or head/tail --bytes are not accurate anymore.
Then perhaps you could fix the locale in the invocation you want to use to extract the MAC address (just as you did to generate this example). Though personally, I think using the system's introspection files (e.g., /sys/class/net/eth?/address), while not portable outside Linux, is a lot cleaner than scraping information from the output of commands that is intended to be read by humans.
Jan
Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Hi Jan, Try this in the command line: ifconfig | awk -F "HWaddr " '{ if ($2!="") print $2 }' If you have more than one NIC you can <grep> the ifconfig output with something like this: ifconfig | grep eth0 | awk -F "HWaddr " '{ if ($2!="") print $2 }' HTH, Pedro -----Original Message----- From: Jan Engelhardt [mailto:jengelh@linux01.gwdg.de] Sent: sexta-feira, 16 de Fevereiro de 2007 13:33 To: opensuse@opensuse.org Subject: [opensuse] Grabbing MAC address of interface Hello, now the question may seem simple ("how to grab the MAC address of a given interface"), but what is actually a "portable" way that does not potentially break when upgrading to a newer openSUSE? I could fiddle with `hwinfo --network` or try parse `ip l sh dev br0` output, but what's actually best? [Using sh.] Jan -- -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (6)
-
Alexey Eremenko
-
eshsf
-
Jan Engelhardt
-
Pedro Marques
-
Randall R Schulz
-
Sylvester Lykkehus