Avatar of lolaferrari
Flag for United Kingdom of Great Britain and Northern Ireland

asked on 

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

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
* BashLinux OS Dev

Avatar of undefined
Last Comment

8/22/2022 - Mon