Solved

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

Posted on 2011-02-14
9
819 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
  • 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

If you other experts are anything like me you are always looking into and testing out new features. While I was doing some research one day I ran across an app that I installed on my Mac and used as a security system. Mac OS X: SecureHome uses your …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now