Getting MAC address in a shell script
This works: mac=$(ifconfig eth0 | grep -Eo '..:..:..:..:..:..' | sed -e 's/://g') .. but I'm sure there's a better way. Suggestions? Also, if I have something like: echotemplate="This is my MAC: (MAC)" mac=$(ifconfig eth0 | grep -Eo '..:..:..:..:..:..' | sed -e 's/://g') echo $(echo $echotemplate | sed -e "s/(MAC)/$mac/") .. is there a better way to do the substitution in the last line? I can't swap the top two lines around (one is a user-defined setting at the top of the script, the value of $mac is calculated in the body of the script), otherwise I would just use echo "This is my MAC: $mac" .. which would be nice and easy! But I'm using this as a learning exercise. -- Mark Rogers // More Solutions Ltd (Peterborough Office) // 0844 251 1450 Registered in England (0456 0902) 21 Drakes Mews, Milton Keynes, MK8 0ER
On 19 Jun 10:27, Mark Rogers wrote:
This works: mac=$(ifconfig eth0 | grep -Eo '..:..:..:..:..:..' | sed -e 's/://g')
.. but I'm sure there's a better way. Suggestions?
Also, if I have something like: echotemplate="This is my MAC: (MAC)" mac=$(ifconfig eth0 | grep -Eo '..:..:..:..:..:..' | sed -e 's/://g') echo $(echo $echotemplate | sed -e "s/(MAC)/$mac/")
.. is there a better way to do the substitution in the last line? I can't swap the top two lines around (one is a user-defined setting at the top of the script, the value of $mac is calculated in the body of the script), otherwise I would just use echo "This is my MAC: $mac" .. which would be nice and easy! But I'm using this as a learning exercise.
How about: mac=$(ip link show dev eth0 | sed -e '/link\/ether/ { s#[ ][ ]*link/ether[ ][ ]*##; s# .*$##; p }; d;') echo "This is my MAC: ${mac//:}" Done. (works in bash, won't work in dash) -- Brett Parker
On 19/06/12 11:42, Brett Parker wrote:
How about: mac=$(ip link show dev eth0 | sed -e '/link\/ether/ { s#[ ][ ]*link/ether[ ][ ]*##; s# .*$##; p }; d;')
Thanks, nice to have an alternative that makes better use of sed and loses grep. What is the meaning/reasoning behind "[ ][ ]*"? My interpretation of it is just "one or more spaces", is that correct? Is "[ ]" just a style preference to make it easier to read than " *", or am I missing something? Other than that I have managed to parse it in my head after a couple of attempts! At the moment I've adapted it to: mac=$(ip link show dev eth0 | sed -e '/link\/ether/ { s# *link/ether *##; s# .*$##; p }; d;') .. as I don't see "zero-or-more spaces" being too general, but that makes assumptions about my understanding of your version....
echo "This is my MAC: ${mac//:}"
The problem with this is that it puts the "template" at the point in the code where it's used, where I want to be able to define the template in the user-config part of the script at the top, and # Config section of script template="This is my MAC: ${mac//:}" # Main script, do not edit # Lots of stuff in here..... mac=$(ip link show dev eth0 | sed -e '/link\/ether/ { s#[ ][ ]*link/ether[ ][ ]*##; s# .*$##; p }; d;') echo $template doesn't work as the variable expansion happens before it is set. Mark -- Mark Rogers // More Solutions Ltd (Peterborough Office) // 0844 251 1450 Registered in England (0456 0902) 21 Drakes Mews, Milton Keynes, MK8 0ER
On Tuesday 19 June 2012 10:27:59 Mark Rogers wrote:
This works: mac=$(ifconfig eth0 | grep -Eo '..:..:..:..:..:..' | sed -e 's/://g')
.. but I'm sure there's a better way. Suggestions?
Also, if I have something like: echotemplate="This is my MAC: (MAC)" mac=$(ifconfig eth0 | grep -Eo '..:..:..:..:..:..' | sed -e 's/://g') echo $(echo $echotemplate | sed -e "s/(MAC)/$mac/")
.. is there a better way to do the substitution in the last line? I can't swap the top two lines around (one is a user-defined setting at the top of the script, the value of $mac is calculated in the body of the script), otherwise I would just use echo "This is my MAC: $mac" .. which would be nice and easy! But I'm using this as a learning exercise.
How about: /sbin/ifconfig eth0 | grep HWaddr | cut -d\ -f11 -- Stuart Bailey BSc (hons) CEng CITP MBCS LinuSoft (Managing Director) Linux Specialist & Software Developer ~~~~~~~~~~~~~~~~~~~~~~~ Phone: (0845) 658 3563 Direct: +44 (0) 1953 878162 Fax: +44 (0) 1603 858583 ~~~~~~~~~~~~~~~~~~~~~~~ http://www.linusoft.co.uk __________ Information from ESET Mail Security, version of virus signature database 7231 (20120619) __________ The message was checked by ESET Mail Security. http://www.eset.com
On 19/06/12 11:58, Stuart Bailey wrote:
How about: /sbin/ifconfig eth0 | grep HWaddr | cut -d\ -f11
Another neat approach, thanks. Mark -- Mark Rogers // More Solutions Ltd (Peterborough Office) // 0844 251 1450 Registered in England (0456 0902) 21 Drakes Mews, Milton Keynes, MK8 0ER
On Tue, Jun 19, 2012 at 10:27:59AM +0100, Mark Rogers wrote:
This works: mac=$(ifconfig eth0 | grep -Eo '..:..:..:..:..:..' | sed -e 's/://g')
.. but I'm sure there's a better way. Suggestions?
cat /sys/class/net/eth0/address Adam -- Klytus, I'm bored. What play thing can you offer me today? An obscure body in the S-K System, your majesty. The inhabitants refer to it as the planet Earth.
On Tue, 19 Jun 2012 22:16:55 +0100 Adam Bower <adam@thebowery.co.uk> allegedly wrote:
On Tue, Jun 19, 2012 at 10:27:59AM +0100, Mark Rogers wrote:
This works: mac=$(ifconfig eth0 | grep -Eo '..:..:..:..:..:..' | sed -e 's/://g')
.. but I'm sure there's a better way. Suggestions?
cat /sys/class/net/eth0/address
Adam
Cool. I looked around /proc thinking I might find something like that. And of course got nowhere. That is useful. Mick --------------------------------------------------------------------- blog: baldric.net fingerprint: E8D2 8882 F7AE DEB7 B2AA 9407 B9EA 82CC 1092 7423 ---------------------------------------------------------------------
On 19 Jun 22:16, Adam Bower wrote:
On Tue, Jun 19, 2012 at 10:27:59AM +0100, Mark Rogers wrote:
This works: mac=$(ifconfig eth0 | grep -Eo '..:..:..:..:..:..' | sed -e 's/://g')
.. but I'm sure there's a better way. Suggestions?
cat /sys/class/net/eth0/address
Surely, you mean... mac=$(</sys/class/net/eth0/address) That does rely on the sys filesystem though... -- Brett Parker
On 20/06/12 09:49, Brett Parker wrote:
Surely, you mean... mac=$(</sys/class/net/eth0/address)
Thanks Brett and Adam, that's much nicer. Can I combine that with the removal of ":" characters in a single command?
That does rely on the sys filesystem though...
Is that ever likely to be an issue on a Debian-based install? The boxes I'm playing with are mostly ARM processors (my current tests are on a Pi) running Debian 6, although I'll need things to also work on Ubuntu Server installs too. Mark -- Mark Rogers // More Solutions Ltd (Peterborough Office) // 0844 251 1450 Registered in England (0456 0902) 21 Drakes Mews, Milton Keynes, MK8 0ER
On 20 Jun 11:29, Mark Rogers wrote:
On 20/06/12 09:49, Brett Parker wrote:
Surely, you mean... mac=$(</sys/class/net/eth0/address)
Thanks Brett and Adam, that's much nicer.
Can I combine that with the removal of ":" characters in a single command?
That does rely on the sys filesystem though...
Is that ever likely to be an issue on a Debian-based install? The boxes I'm playing with are mostly ARM processors (my current tests are on a Pi) running Debian 6, although I'll need things to also work on Ubuntu Server installs too.
Unlikely on anything even remotely modern to be a problem. Thought you needed it both with and without the colons? mac=$(OLDIFS="$IFS";IFS=":"; for thing in $(<sys/class/net/eth0/address); do echo -n $thing; done) Not, erm, pretty - but functional. -- Brett Parker
participants (5)
-
Adam Bower -
Brett Parker -
Mark Rogers -
mick -
Stuart Bailey