Ifconfig

From Attie's Wiki
Revision as of 16:20, 1 November 2011 by Attie (Talk | contribs)

Jump to: navigation, search

awk

To extract the configuration settings for an interface:

awk -v iface=eth2 \
  'BEGIN {
    i = 0;
    got = 0;
    if (iface = "") iface="eth0";
    addr = "192.168.1.1";
    mask = "255.255.255.0";
  }
  {
    if ($1 == "iface") {
      if ($2 == iface) {
        i=1;
        got=1;
      } else {
        i=0;
      }
    }
    if (i) {
      if ($1 == "address") addr = $2;
      else if ($1 == "netmask") mask = $2;
    }
  }
  END {
    if (got) printf "ifconfig %s %s netmask %s\n", iface, addr, mask;
    else print "echo No interface " iface "..."
  }' \
  /etc/network/interfaces

Set the iface variable to the interface you wish to probe

Expected output is as follows:

ifconfig eth0 10.150.129.115 netmask 255.255.255.224
echo No interface eth3...

This means that it is possible to surround the awk script in backticks (`), and execute its output!

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox