troubleshooting Question

BASH SCRIPT QUESTION: Determine which NIC out of multiple NICs the server hostname runs on.

Avatar of lolaferrari
lolaferrariFlag for United Kingdom of Great Britain and Northern Ireland asked on
* BashLinux OS Dev
2 Comments1 Solution97 ViewsLast Modified:
I need to script finding which of the NICS on the server is the one the IP the hostname of the server runs on my Redhat 5 and Redhat 6 x86 servers. This is only part of the script since I'll use the variable for comparison later on in the script but below is how I'm going to do it. Do you think there is a better way to do it or do you think this is good enough? What I'm relying on is that $(hostname) actually matches the entry in /etc/hosts but sometimes hostname has FQDN and sometimes not.


HOSTIP=$(cat /etc/hosts | grep ${host} | awk ‘{print $1}’)
if [  ! -z ${HOSTIP} ]; then
          HOSTNIC=$(/sbin/ifconfig -a | grep -B2  ${HOSTIP} )
         printf “HOST NIC IS UNKNOWN”
         exit 1
David Favor
Fractional CTO
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros