Use ifconfi and awk/gawk to extract network information.
For Linux:
Usage: ifconfig | awk -f linuxNICParser.awk
linuxNICParser.awk
BEGIN {
FS = "\n"
}
{
# nics[1,1]:nicName, [1,2]:macAddress, [1,3]:ipAddress, [1,4]:netMask
newNicLine = "[A-Za-z].*encap.*"
ipLine = ".*inet.*"
zeroLine = $0
isFirstSection = 1
nicCount = 0
newSec = match(zeroLine, newNicLine)
if (newSec) {
split (zeroLine, variableArray, " ")
nicName = variableArray[1]
# ignore loop nic
if ( ! match(nicName, "lo*") ) {
nicCount = nicCount + 1
nics[nicCount,1] = nicName
nics[nicCount,2] = variableArray[5]
} else
isFirstSection = 10
}
while (( getline variableLine ) > 0)
{
newSec = match(variableLine, newNicLine)
if (newSec) {
isFirstSection == 0
split (variableLine, variableArray, " ")
nicName = variableArray[1]
# ignore loop nic
if ( ! match(nicName, "lo*") ) {
nicCount = nicCount + 1
nics[nicCount,1] = nicName
nics[nicCount,2] = variableArray[5]
} else
isFirstSection = 10
}
#ignore loop device
if (isFirstSection == 10)
continue
# Get ip/net mask
if (match(variableLine, ipLine)) {
split (variableLine, variableArray, ":")
split (variableArray[2], tmpArray, " ")
nics[nicCount,3] = tmpArray[1]
nics[nicCount,4] = variableArray[4]
}
}
}
END {
for (i = 1; i <= nicCount; i++) {
print width
for (j =1; j<=4; j++) {
print nics[i,j]
}
}
}
FS = "\n"
}
{
# nics[1,1]:nicName, [1,2]:macAddress, [1,3]:ipAddress, [1,4]:netMask
newNicLine = "[A-Za-z].*encap.*"
ipLine = ".*inet.*"
zeroLine = $0
isFirstSection = 1
nicCount = 0
newSec = match(zeroLine, newNicLine)
if (newSec) {
split (zeroLine, variableArray, " ")
nicName = variableArray[1]
# ignore loop nic
if ( ! match(nicName, "lo*") ) {
nicCount = nicCount + 1
nics[nicCount,1] = nicName
nics[nicCount,2] = variableArray[5]
} else
isFirstSection = 10
}
while (( getline variableLine ) > 0)
{
newSec = match(variableLine, newNicLine)
if (newSec) {
isFirstSection == 0
split (variableLine, variableArray, " ")
nicName = variableArray[1]
# ignore loop nic
if ( ! match(nicName, "lo*") ) {
nicCount = nicCount + 1
nics[nicCount,1] = nicName
nics[nicCount,2] = variableArray[5]
} else
isFirstSection = 10
}
#ignore loop device
if (isFirstSection == 10)
continue
# Get ip/net mask
if (match(variableLine, ipLine)) {
split (variableLine, variableArray, ":")
split (variableArray[2], tmpArray, " ")
nics[nicCount,3] = tmpArray[1]
nics[nicCount,4] = variableArray[4]
}
}
}
END {
for (i = 1; i <= nicCount; i++) {
print width
for (j =1; j<=4; j++) {
print nics[i,j]
}
}
}
For Solaris:
Usage: ifconfig -a|gawk -f solarisNICParser.awk
solarisNICParser.awk
BEGIN {
FS = "\n"
}
{
# nics[1,1]:nicName, [1,2]:macAddress, [1,3]:ipAddress, [1,4]:netMask
newNicLine = "[A-Za-z].*flags.*"
ipLine = "inet*"
macLine = "ether*"
zeroLine = $0
isFirstSection = 1
nicCount = 0
newSec = match(zeroLine, newNicLine)
if (newSec) {
split (zeroLine, variableArray, ":")
nicName = variableArray[1]
# ignore loop nic
if ( ! match(nicName, "lo*") ) {
nicCount = nicCount + 1
nics[nicCount,1] = nicName
} else
isFirstSection = 10
}
while (( getline variableLine ) > 0)
{
newSec = match(variableLine, newNicLine)
if (newSec) {
isFirstSection = 0
split (variableLine, variableArray, " ")
nicName = variableArray[1]
# ignore loop nic
if ( ! match(nicName, "lo*") ) {
nicCount = nicCount + 1
nics[nicCount,1] = nicName
}
}
#ignore loop device
if (isFirstSection == 10)
continue
# Get ip/net mask
if (match(variableLine, ipLine)) {
split (variableLine, variableArray, " ")
nics[nicCount,3] = variableArray[2]
nics[nicCount,4] = variableArray[6]
}
# Get mac address
if (match(variableLine, macLine)) {
split (variableLine, variableArray, " ")
nics[nicCount,2] = variableArray[2]
}
}
}
END {
for (i = 1; i <= nicCount; i++) {
for (j =1; j<=4; j++) {
print nics[i,j]
}
}
}
FS = "\n"
}
{
# nics[1,1]:nicName, [1,2]:macAddress, [1,3]:ipAddress, [1,4]:netMask
newNicLine = "[A-Za-z].*flags.*"
ipLine = "inet*"
macLine = "ether*"
zeroLine = $0
isFirstSection = 1
nicCount = 0
newSec = match(zeroLine, newNicLine)
if (newSec) {
split (zeroLine, variableArray, ":")
nicName = variableArray[1]
# ignore loop nic
if ( ! match(nicName, "lo*") ) {
nicCount = nicCount + 1
nics[nicCount,1] = nicName
} else
isFirstSection = 10
}
while (( getline variableLine ) > 0)
{
newSec = match(variableLine, newNicLine)
if (newSec) {
isFirstSection = 0
split (variableLine, variableArray, " ")
nicName = variableArray[1]
# ignore loop nic
if ( ! match(nicName, "lo*") ) {
nicCount = nicCount + 1
nics[nicCount,1] = nicName
}
}
#ignore loop device
if (isFirstSection == 10)
continue
# Get ip/net mask
if (match(variableLine, ipLine)) {
split (variableLine, variableArray, " ")
nics[nicCount,3] = variableArray[2]
nics[nicCount,4] = variableArray[6]
}
# Get mac address
if (match(variableLine, macLine)) {
split (variableLine, variableArray, " ")
nics[nicCount,2] = variableArray[2]
}
}
}
END {
for (i = 1; i <= nicCount; i++) {
for (j =1; j<=4; j++) {
print nics[i,j]
}
}
}
No comments:
Post a Comment