Solved

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

Posted on 2011-02-14
9
829 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
My MacBook is dogging... 4 56
Unzip files in macOS Sierra - How? 4 60
iCloud vs. iCloud (Archive) - OK to delete - iCloud (Archive) 3 38
powershell function location 6 42
Syslogd is a utility that traps and logs messages sent by running processes. It is configured with the syslog.conf file, which consists of lines containing a pair of fields: "the selector field which specifies the types of messages and priorities to…
We could spend the next millennium discussing the differences of the Mac and Windows platforms. The next century will continue to have fanatics on both side of the equation and neither side will win the war. However, that’s not why we are here. W…
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 …

777 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