title: Retrieve the name of a Network Interface using an IP Address and AWK Bash
body: <p>I am trying to use Bash on CentOS 6.4 to retrieve the network interface name attached to an IP address using AWK. I have a bit of command from a Solaris box, but I'm not sure how to convert it to Linux output.</p>
  
  <p>The command looks like this:</p>
  
  <pre><code>ifconfig -a | awk '
      $1 ~ /:/    {split($1,nic,":"); 
                       lastif=sprintf("%s:%s",nic[1],nic[2]);}
      $2 == "'$1'"    { print lastif ; exit; }
  
      '
  </code></pre>
  
  <p>Its part of a script, so it takes commandline argument like <code>monitor.sh x.x.x.x y.y.y.y</code> and it uses the first x.x.x.x to get the interface name, then makes $1 == $2 so then it can ping y.y.y.y later. I'm guessing that in Solaris the <code>ifconfig -a</code> output is different than CentOS. I can get the interface name if the IP and interface are on the same line, but in linux, they're on two different lines. Any ideas. </p>