Register
It is currently Wed Apr 23, 2014 6:20 am

bashscipt to rename SUSE ifcfg-eth-id-<MAC ADDRESS> fi


All times are UTC - 6 hours


Post new topic Reply to topic  [ 10 posts ] 
Author Message
 PostPosted: Sun Jul 06, 2008 7:56 pm   

Joined: Sun Jul 06, 2008 7:43 pm
Posts: 6
Hello all, I'm a linux newbie. So scripting is not one of my forte. Anyway, any help is appreciated. I have a SUSE Linux server which use the
ifcfg-eth-id-<MAC ADDRESS> format in /etc/sysconfig/network. What I'd like to do is to rename that file to ifcg-eth0 using a script. I renamed it manually, and it is working. I've developed the following script but the problem is it cannot rename it using this script. I guess I need to "escape" the ":" which do not know. I need the NIC configs rename to ifcg-ethX format as it makes it easy to recognize which NICs I am looking at when I have mulitple NIC configs.


#!/bin/sh
MAC=`ifconfig | grep 'HWaddr '|cut -f 1
echo "$MAC"; ##output is 00:30:18:1D:4C:5A
cd /etc/sysconfig/network
mv ifcfg-eth-id-$MAC ifcfg-eth0

If anyone has a better way of doing this, I would appreciate very much. Thanks in advance.


Top
 Profile YIM  
 PostPosted: Mon Jul 07, 2008 7:02 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 224
Location: London - UK
I suggest instead of using backquotes it's a little better and certainly more readable to use the following format instead...

$( ... )

Now when I run your ifconfig command i do not get the mac address as output...
Code:
[david@david ~]# ifconfig | grep 'HWaddr '|cut -f 1
eth0      Link encap:Ethernet  HWaddr 00:08:02:90:65:11


So I therefore came up with this...

Code:
[david@david ~]# ifconfig | awk '/HWaddr / { print $NF }'
00:08:02:90:65:11


The awk command first process the regex "/HWaddr /" ensuring only that line we want is grabbed (a bit like grep) and then "{ print $NF }" prints the last field in the line, delimited by spaces.

I also suggest you use quotes and braces to ensure variables are interpreted in the way you intend and so I end up with this...

Code:
#!/bin/sh
MAC=$(ifconfig | awk '/HWaddr / { print $NF }')
echo ${MAC}
cd /etc/sysconfig/network
mv "ifcfg-eth-id-${MAC}" 'ifcfg-eth0'


I haven't tested this but it should work ok :)

I note that if you have multiple nics configured this'll still likely not work as intended.

One last thing, as you didn't include the error message that the script produces when it fails it's a little more difficult to debug, however I suggest that you check you have permissions required to move the file, ie you have rwx permissions in /etc/sysconfig/network

I hope this helps


Top
 Profile  
 PostPosted: Mon Jul 07, 2008 10:42 am   

Joined: Tue Apr 01, 2008 10:19 am
Posts: 49
It would be better to rewrite the udev rule to create the device as you want it to be named in the first place. I'm not familiar with /sys internals, but I wouldn't be surprised if just mv'ing it like that would break things.

EDIT: disregard this post, I misread your question entirely.


Top
 Profile  
 PostPosted: Mon Jul 07, 2008 2:46 pm   

Joined: Sun Jul 06, 2008 7:43 pm
Posts: 6
Thanks for the info. I'm afraid it did not run on my SLES box. It appears that I need to "escape" the ":"s in the filename. How exactly do I go about doing that? Thanks again.


Top
 Profile YIM  
 PostPosted: Tue Jul 08, 2008 3:43 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 224
Location: London - UK
kabalen wrote:
Thanks for the info. I'm afraid it did not run on my SLES box. It appears that I need to "escape" the ":"s in the filename. How exactly do I go about doing that? Thanks again.


Are you sure you're running bash? There are some weird shells out there :)

Generally to escape one should use a \ like so...
\:

However I have never come across needing to escape a : and also as the : is within a variable at the time that also should mean it doesn't need escaping. I believe that this is not the cause of the problem, but of course as I can't test this myself it's hard to say. I tried the script on some test files and it worked fine.

I suggest giving us more information, like the output of the script with error message and a "ls -l" of /etc/sysconfig/network , it's much easier to help with more information :)


Top
 Profile  
 PostPosted: Fri Jul 11, 2008 8:45 am   

Joined: Sun Jul 06, 2008 7:43 pm
Posts: 6
Sorry for the late reply. My shell is bash for sure. I am testing on SLES10 SP1. When I do an echo $SHELL, it returns "/bin/bash". I copied the script you provided in this post. When I run it, this is the output:

sles10:~ # ./rename-eth.sh
00:0C:29:30:29:A2
mv: cannot stat `ifcfg-eth-id-00:0C:29:30:29:A2': No such file or directory

I just noticed too that the ` is in the beginning of ifcg and yet the end of the line is a '. Does this make a difference?


Top
 Profile YIM  
 PostPosted: Fri Jul 11, 2008 5:22 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
why not try:
Code:
#!/bin/sh
MAC=$(ifconfig | awk '/HWaddr / { print $NF }')
echo "${MAC}"
mv "/etc/sysconfig/network/ifcfg-eth-id-${MAC}" "/etc/sysconfig/network/ifcfg-eth0"


also... are you sure that that particular device is being loaded by suse? meaning... if the device isn't being loaded... it will not create the file in /etc/sysconfig/network so naturally, the file won't exist.


Top
 Profile  
 PostPosted: Sat Jul 12, 2008 9:59 am   

Joined: Sun Jul 06, 2008 7:43 pm
Posts: 6
I am quite certain that the device is being loaded. However, what I did not realize is the proverbial case sensitivity in Linux... ahhh. Here is what I found out. When you do an ifconfig command, the MAC address (if it has letters) is displayed upper case yet the NIC config file in /etc/sysconfig/network/ifcfg-eth-id(MAC ADDR) is all lower case. No wonder it's file not found. So I am back to square one:-(. I guess, I'll forget about doing this script as it was pointed to me before my logic is still kinda flawed because I am only doing one change for the NIC.

Once I have multiple NICs I need to change it to ifcfg-eth0 then ifcfg-eth1 for however many NICs I will install. Since I do not do a lot of server installs, I can just do it manually.

Thanks everyone, I really appreciate the help.


Top
 Profile YIM  
 PostPosted: Sun Jul 13, 2008 11:58 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
that's an easy fix:

Code:
#!/bin/sh
MAC=$(ifconfig | awk '/HWaddr / { print $NF }')
newMAC=`echo "${MAC}" | tr [:upper:] [:lower:]`
mv "/etc/sysconfig/network/ifcfg-eth-id-${newMAC}" "/etc/sysconfig/network/ifcfg-eth0"


see if that works for ya


Top
 Profile  
 PostPosted: Sun Jul 13, 2008 8:24 pm   

Joined: Sun Jul 06, 2008 7:43 pm
Posts: 6
Awesome, jbsnake:-)! That script did it flawlessly...


Top
 Profile YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP