Solved

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

Posted on 2011-02-14
9
834 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
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…
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 …

738 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