Ifconfig

From Attie's Wiki
(Difference between revisions)
Jump to: navigation, search
m
m
 
Line 1: Line 1:
 
== awk ==
 
== awk ==
To extract the configuration settings for an interface:
+
This awk script will extract the configuration settings for an interface from the <code>/etc/network/interfaces</code> file.
<source lang="bash">
+
 
awk -v iface=eth2 \
+
'''Contents of 'ifaceExtract':'''
  'BEGIN {
+
<source lang="text">
    i = 0;
+
BEGIN {
    got = 0;
+
  i = 0;
    if (iface = "") iface="eth0";
+
  got = 0;
    addr = "192.168.1.1";
+
  if (iface = "") iface="eth0";
    mask = "255.255.255.0";
+
  addr = "192.168.1.1";
  }
+
  mask = "255.255.255.0";
  {
+
}
    if ($1 == "iface") {
+
{
      if ($2 == iface) {
+
  if ($1 == "iface") {
        i=1;
+
    if ($2 == iface) {
        got=1;
+
      i=1;
      } else {
+
      got=1;
        i=0;
+
    } else {
      }
+
      i=0;
    }
+
    if (i) {
+
      if ($1 == "address") addr = $2;
+
      else if ($1 == "netmask") mask = $2;
+
 
     }
 
     }
 
   }
 
   }
   END {
+
   if (i) {
     if (got) printf "ifconfig %s %s netmask %s\n", iface, addr, mask;
+
     if ($1 == "address") addr = $2;
    else print "echo No interface " iface "..."
+
    else if ($1 == "netmask") mask = $2;
  }' \
+
  }
  /etc/network/interfaces
+
}
 +
END {
 +
  if (got) printf "ifconfig %s %s netmask %s\n", iface, addr, mask;
 +
  else print "echo No interface " iface "..."
 +
}
 
</source>
 
</source>
Set the iface variable to the interface you wish to probe
+
 
 +
<source lang="bash">
 +
awk -v iface=eth2 -f ifaceExtract /etc/network/interfaces
 +
</source>
 +
 
 +
Set the '<code>iface</code>' variable to the interface you wish to probe
  
 
Expected output is as follows:
 
Expected output is as follows:
Line 39: Line 44:
 
echo No interface eth3...
 
echo No interface eth3...
 
</pre>
 
</pre>
This means that it is possible to surround the awk script in backticks (`), and execute its output!
+
This means that it is possible to surround the awk script in backticks (``), and execute its output!

Latest revision as of 16:26, 1 November 2011

[edit] awk

This awk script will extract the configuration settings for an interface from the /etc/network/interfaces file.

Contents of 'ifaceExtract':

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 "..."
}
awk -v iface=eth2 -f ifaceExtract /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