LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 11-19-2012, 06:14 PM   #1
rootaccess
Member
 
Registered: Mar 2012
Posts: 211

Rep: Reputation: Disabled
need help writing script for eth0 or em1 devices


Hi all. I am having trouble writing a script. This is to be used in the post-scripts section of kickstarts to determine the MAC address (HWaddr) of either eth0 or em1. I mention both because RHEL6 will default to either one. I am trying to set the network-scripts/ifcfg-X files correctly after kickstarting the machine. I forget if the HWaddr line is automatically generated when doing an ethernet install. I havent been able to do one since I dont have access to the router, just wireless for now. Studying to be a RHCE doesn't help without access to a router.

I am guessing it doesn't have it in there so I am writing a script to include it. I havent done the second half (appending the line with sed, I can do that later), but this is what I have and it doesn't seem to be working. I am trying to go based off the exit status and I don't know why it doesn't work.

Code:
#!/bin/bash
EX=$?

/sbin/ifconfig em1 > /dev/null

if [ $EX -eq 0 ]; then

        /sbin/ifconfig em1 | grep "HWaddr" | sed 's/.* HWaddr //'
else

        /sbin/ifconfig eth0 | grep "HWaddr" | sed 's/.* HWaddr //'

fi
Thanks,
Shawn
 
Old 11-19-2012, 11:33 PM   #2
rootaccess
Member
 
Registered: Mar 2012
Posts: 211

Original Poster
Rep: Reputation: Disabled
I actually reversed the fields but I dont know if it would fail if eth0 did not exist. Either way I finished the last part of the ifconfig command to append to the ifcfg-X files.

Code:
#!/bin/bash
EX=$?

/sbin/ifconfig eth0 > /dev/null

if [ $EX -eq 0 ]; then
	
	/sbin/ifconfig eth0 | grep "HWaddr" | sed 's/.* HWaddr //' | awk '{print "HWADDR="$0}' >> /etc/sysconfig/network-scripts/ifcfg-eth0

else
	/sbin/ifconfig em1 | grep "HWaddr" | sed 's/.* HWaddr //' | awk '{print "HWADDR="$0}' >> /etc/sysconfig/network-scripts/ifcfg-em1

fi
 
Old 11-21-2012, 03:45 AM   #3
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,641

Rep: Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250
EX=$? has no any meaning at the beginning of the script. $? is the return value of the last executed command, probably you need to move that line after ifconfig.
 
Old 11-25-2012, 02:24 AM   #4
rootaccess
Member
 
Registered: Mar 2012
Posts: 211

Original Poster
Rep: Reputation: Disabled
Thanks. It did seem to work, sort of. I did put that line after ifconfig. Actually i removed the variable and just used '[ if $? -eq 0 ]' but it still throws an error to me with em1 but nonetheless it still sends the HWADDR for eth0 if em1 is not recognized on the system, so it still works, but why would I still receive this error?:

[root@centos scripts]# bash macaddress.sh
em1: error fetching interface information: Device not found
HWADDR=00:11:22:33:44:55

BTW I removed the appending to /etc/sysconfig/network-scripts/ifcfg-eth0 and just wanted the output. The mac that you see there is my eth0 mac

Last edited by rootaccess; 11-25-2012 at 02:26 AM.
 
Old 11-26-2012, 01:09 AM   #5
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,641

Rep: Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250
[ if $? -eq 0 ] will never work, probably you only mistyped. That should be: if [ $? -eq 0 ].
To hide that error message you need to redirect stderr to /dev/null too. This message was reported by ifconfig, so you will need:

/sbin/ifconfig eth0 > /dev/null 2>/dev/null

Furthermore you can simplify your chain (of ifconfig|grep|sed|awk) by:
ifconfig eth0 | awk ' /HWaddr/ { print "HWADDR="$NF}'

finally you can also use a variable for your interface:
Code:
#!/bin/bash
IF=eth0
/sbin/ifconfig eth0 > /dev/null 2>&1 || IF=em1
# here remove comment if you want to redirect to file
/sbin/ifconfig $IF | awk ' /HWaddr/ { print "HWADDR="$NF}' # >> /etc/sysconfig/network-scripts/ifcfg-$IF
(not tested)
please press YES if you really want to say thanks
 
1 members found this post helpful.
Old 01-03-2013, 01:10 AM   #6
rootaccess
Member
 
Registered: Mar 2012
Posts: 211

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
[ if $? -eq 0 ] will never work, probably you only mistyped. That should be: if [ $? -eq 0 ].
To hide that error message you need to redirect stderr to /dev/null too. This message was reported by ifconfig, so you will need:

/sbin/ifconfig eth0 > /dev/null 2>/dev/null

Furthermore you can simplify your chain (of ifconfig|grep|sed|awk) by:
ifconfig eth0 | awk ' /HWaddr/ { print "HWADDR="$NF}'

finally you can also use a variable for your interface:
Code:
#!/bin/bash
IF=eth0
/sbin/ifconfig eth0 > /dev/null 2>&1 || IF=em1
# here remove comment if you want to redirect to file
/sbin/ifconfig $IF | awk ' /HWaddr/ { print "HWADDR="$NF}' # >> /etc/sysconfig/network-scripts/ifcfg-$IF
(not tested)
please press YES if you really want to say thanks
I didnt look at this post til now because I am studying bash and wanted to see if I could solve it and you not only solved it for me but opened my eyes to a couple new things I learned. Couldnt have been at a more perfect time. I learned that if a certain command works, instead of checking for an exit status, I just use '||' to assign the same variable as the original one I attempted. This couldn't be a better solution for what I am aiming to do. Genius idea

I am also learning all this standard error, standard out , /dev/null redirection which confuses me a bit. I know /dev/null is like trash or a black hole so in this case I am omitting any error messages from doing '/sbin/ifconfig eth0' so I understand that. What I don't get is why is /dev/null even being redirected to yet another standard error or stdout? what is '2>&1' really supposed to do here? I know that stderror is 2 and stdout is 1. but if are ommitting error messages, why even bother with this 2>&1 ? This redirection of stderror and stdout seems very confusing

Last edited by rootaccess; 01-03-2013 at 01:11 AM.
 
Old 01-06-2013, 08:10 AM   #7
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,641

Rep: Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250
glad to hear that, happy new year and happy new scripting...
actually there are a lot of documentation on the net about redirecting, so you can choose what you prefer.
For example: http://www.tldp.org/LDP/abs/html/io-redirection.html or http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html

2>&1 means you want to send stderr to the same "place" where stdout is currently sent, but you would better read normal explanations or tutorials.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [Fedora 15] em1 has disappeared after system update Flang3r Linux - Newbie 5 10-27-2011 07:48 AM
eth0 - eth9 devices getting clobbered hollane Mandriva 1 12-08-2005 02:37 AM
No writing to 90% of my devices drache777 Linux - Hardware 6 02-01-2005 10:52 PM
Bcast ever different on two eth0:* devices? cormander Linux - Networking 2 01-05-2005 06:25 AM
floppy - writing to devices is not supported nouvellebie Linux - Newbie 4 03-13-2004 01:01 PM


All times are GMT -5. The time now is 06:54 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration