Solved

Terminal Command to replace text in a text file with the MAC address of the machine

Posted on 2011-02-14
9
836 Views
Last Modified: 2012-05-11
Hello,

I have a text document that has a machine's MAC address in it. I need to copy this file to multiple machines and would like to have a terminal script/command to replace the old MAC with the MAC of the new machine. I am using OSX 10.6 but I'm sure most Linux commands would work as well. I don't want to use a perl script or anything like that if I can avoid it. A basic UNIX command would be great. Can anyone help me or steer me in the right direction?

Thanks for all your help.
0
Comment
Question by:WindhamSD
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 9

Expert Comment

by:AriMc
ID: 34890897
If your textfile /tmp/test.txt has a placeholder MACADDR for the MAC-address, then this could work:

export MACADDR=`ifconfig | grep eth | awk '{print $5};'`
sed -i "s/MACADDR/$MACADDR/g" /tmp/test.txt

You might need to experiment if your NICs show up as something else than ethX or if your Linux-tools output formats are different from my Debian.

0
 
LVL 9

Expert Comment

by:AriMc
ID: 34890919
Oh, one more thing: make sure your ifconfig command directory is in your path-variable or use an absolute reference (/sbin/ifconfig in Debian).

0
 

Author Comment

by:WindhamSD
ID: 34890996
Thanks Ari,

Here is the part of the document that I want to autochange in bold italics:

<key>en_address</key>
      <array>
            <string>00:25:00:cf:2d:8e</string>
      </array>
      <key>generateduid</key>
      <array>
            <string>B9BDD8EA-4FD2-4522-9FF6-9152D617A5B0</string>
      </array>
      <key>ip_address</key>
      <array>
            <string>127.0.0.1</string>
      </array>
      <key>mcx_flags</key>
      <array>

Here is the output of my ifconfig:

lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
      inet6 ::1 prefixlen 128
      inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
      inet 127.0.0.1 netmask 0xff000000
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
      ether 00:25:00:a1:05:1a
      inet6 fe80::225:ff:fea1:51a%en0 prefixlen 64 scopeid 0x4
      inet 10.64.130.99 netmask 0xffff0000 broadcast 10.64.255.255
      media: autoselect (100baseTX <full-duplex>)
      status: active

I always want it to update with the en0 address. Will the same commands you posted work in this instance. I use terminal all the time to issue commands, but not at the level of making and replacing text.

Thanks!
0
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

 
LVL 9

Expert Comment

by:AriMc
ID: 34891093
I think this would work in your case:

export MACADDR=`ifconfig | grep ether | awk '{print $2};'`
sed -i "s/00:25:00:cf:2d:8e/$MACADDR/g" /tmp/test.txt

0
 

Author Comment

by:WindhamSD
ID: 34896357
I think we are getting close Ari,

This is what I entered and the error I received:

~$ export MACADDR=`ifconfig | grep ether | awk '{print $2};'`
~$ sed -i "s/00:25:00:cf:2d:8e/$MACADDR/g" Desktop/testmanaged.plist
sed: 1: "Desktop/testmanaged.plist": extra characters at the end of D command

Any ideas? Thanks again.
0
 
LVL 3

Expert Comment

by:berniep
ID: 34901343
Looks like it's reading the filename Desktop/... as an argument.  Try this without the -i:

sed "s/00:25:00:cf:2d:8e/$MACADDR/g" Desktop/testmanaged.plist >newfile

If that works, then move or copy the newfile over the original:

sed "s/00:25:00:cf:2d:8e/$MACADDR/g" Desktop/testmanaged.plist >newfile &&
cp -f newfile Desktop/testmanaged.plist
0
 
LVL 9

Accepted Solution

by:
AriMc earned 500 total points
ID: 34901600
You could also try making the file path more absolute like this:

~$ sed -i "s/00:25:00:cf:2d:8e/$MACADDR/g" ~/Desktop/testmanaged.plist

0
 

Author Closing Comment

by:WindhamSD
ID: 34906458
Bingo! Absolute path...this wouldn't have been an issue because the real location of the file is buried in the /var/ directory, but I was testing it on my desktop. Appreciate all the help!
0
 

Author Comment

by:WindhamSD
ID: 34906464
Thank You for your input too burniep
0

Featured Post

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The error "There was an error performing the update" occurred on a Mac OS X client workstation running  Symantec AntiVirus for Mac (http://www.symantec.com/business/products/purchasing.jsp?pcid=pcat_security&pvid=825_1) - the Enterprise product vers…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question