LinuxQuestions.org

LinuxQuestions.org (http://www.linuxquestions.org/questions/index.php)
-   Linux - General (http://www.linuxquestions.org/questions/forumdisplay.php?f=1)
-   -   need help writing script for eth0 or em1 devices (http://www.linuxquestions.org/questions/showthread.php?t=4175437870)

rootaccess 11-19-2012 06:14 PM

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

rootaccess 11-19-2012 11:33 PM

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


pan64 11-21-2012 03:45 AM

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.

rootaccess 11-25-2012 02:24 AM

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

pan64 11-26-2012 01:09 AM

[ 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

rootaccess 01-03-2013 01:10 AM

Quote:

Originally Posted by pan64 (Post 4837014)
[ 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

pan64 01-06-2013 08:10 AM

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.


All times are GMT -5. The time now is 11:05 PM.