Printing network details


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Printing network details
# 1  
Old 11-18-2019
Printing network details

Hi ALL,

I am trying to collect and print all the network interface details in linux shell script.
operating system : centos 7

Below is my shell script :

Code:
#!/usr/bin/env bash
for i in $(ip link show | awk -F: '$1>0 {print $2}')


do

if [ $i == lo ]; then


echo -e "{\"'name'\":\"'"$i"'\",\"'ip_address'\":'"$(ifconfig $i |grep inet |grep -v inet6 |awk '{print $2}')"',\"'packet_max_size'\":'"$(ifconfig $i |grep mtu | awk '{print $NF}')"',\"'ip_subnet'\":'"$(ifconfig $i |grep inet |grep -v inet6 |awk '{print $4}')"',\"'dns_server'\":'"$(cat /etc/resolv.conf |grep -i '^nameserver'|head -n1|cut -d ' ' -f2)"',\"'received_packets'\":'"$(ifconfig $i |grep -i RX |head -1 |awk '{print $3}')"',\"'type'\":'"$(ls -lrt /sys/class/net/ |grep $i |awk ' {print $11}' |awk -F "/" ' {print $4}')"',\"'Flg'\":'"$(ifconfig -s $i |grep -v Flg |awk '{print $11}')"',\"'sent_packets'\":'"$(ifconfig $i |grep  TX |head -1 |awk '{print $3}')"'}"
fi
if [ $i != lo ]; then


echo -e "{\"'name'\":\"'"$i"'\",\"'ip_address'\":'"$(ifconfig $i |grep inet |grep -v inet6 |awk '{print $2}')"',\"'mac_address'\":'"$(ifconfig $i |grep ether |awk '{print $2}')"',\"'dhcp_enabled'\":'"$(cat  /etc/sysconfig/network-scripts/ifcfg-$i |grep BOOTPROTO |awk -F "=" '{print $2}')"',\"'packet_max_size'\":'"$(ifconfig $i |grep mtu | awk '{print $NF}')"',\"'speed'\":'"$(sudo ethtool $i |grep Speed |awk '{print $2}')"',\"'ip_subnet'\":'"$(ifconfig $i |grep inet |grep -v inet6 |awk '{print $4}')"',\"'dns_server'\":'"$(cat /etc/resolv.conf |grep -i '^nameserver'|head -n1|cut -d ' ' -f2)"',\"'received_packets'\":'"$(ifconfig $i |grep -i RX |head -1 |awk '{print $3}')"',\"'type'\":'"physical"',\"'Flg'\":'"$(ifconfig -s $i |grep -v Flg |awk '{print $11}')"',\"'sent_packets'\":'"$(ifconfig $i |grep  TX |head -1 |awk '{print $3}')"'}"


fi

done

Ouput :
Code:
{"'name'":"'lo'","'ip_address'":'127.0.0.1',"'packet_max_size'":'65536',"'ip_subnet'":'255.0.0.0',"'dns_server'":'8.8.8.8',"'received_packets'":'39651',"'type'":'virtual',"'Flg'":'LRU',"'sent_packets'":'39651'}
{"'name'":"'eno16780032'","'ip_address'":'192.168.20.120',"'mac_address'":'00:0c:29:df:1e:7a',"'dhcp_enabled'":'dhcp',"'packet_max_size'":'1500',"'speed'":'10000Mb/s',"'ip_subnet'":'255.255.255.0',"'dns_server'":'8.8.8.8',"'received_packets'":'55599',"'type'":'physical',"'Flg'":'BMRU',"'sent_packets'":'65397'}

I want to expand this shell script based on the flg variable ouput.

if flg variable ouput is LRU i want to add two more variables(description and status)
if flg variable ouput is BMRU i want to add two more variables(description and status)

Desired output :
if flg variable ouput is LRU
Code:
{"'name'":"'lo'","'status'":'up',"'ip_address'":'127.0.0.1',"'description'":'it is a loop back device',"'packet_max_size'":'65536',"'ip_subnet'":'255.0.0.0',"'dns_server'":'8.8.8.8',"'received_packets'":'39651',"'type'":'virtual',"'Flg'":'LRU',"'sent_packets'":'39651'}

if flg variable ouput is BMRU
Code:
{"'name'":"'eno16780032'","'status'":'up',"'description'":'it is a broadcast address supporting multicast',"'ip_address'":'127.0.0.1',"'ip_address'":'192.168.20.120',"'mac_address'":'00:0c:29:df:1e:7a',"'dhcp_enabled'":'dhcp',"'packet_max_size'":'1500',"'speed'":'10000Mb/s',"'ip_subnet'":'255.255.255.0',"'dns_server'":'8.8.8.8',"'received_packets'":'55599',"'type'":'physical',"'Flg'":'BMRU',"'sent_packets'":'65397'}

Can someone please help.

Moderator's Comments:
Mod Comment Please do wrap your samples, code(s) in CODE TAGS as per forum rules.

Last edited by RavinderSingh13; 11-18-2019 at 12:39 PM..
# 2  
Old 11-19-2019
Quote:
Originally Posted by balu1234
Code:
#!/usr/bin/env bash
for i in $(ip link show | awk -F: '$1>0 {print $2}')


do

if [ $i == lo ]; then


echo -e "{\"'name'\":\"'"$i"'\",\"'ip_address'\":'"$(ifconfig $i |grep inet |grep -v inet6 |awk '{print $2}')"',\"'packet_max_size'\":'"$(ifconfig $i |grep mtu | awk '{print $NF}')"',\"'ip_subnet'\":'"$(ifconfig $i |grep inet |grep -v inet6 |awk '{print $4}')"',\"'dns_server'\":'"$(cat /etc/resolv.conf |grep -i '^nameserver'|head -n1|cut -d ' ' -f2)"',\"'received_packets'\":'"$(ifconfig $i |grep -i RX |head -1 |awk '{print $3}')"',\"'type'\":'"$(ls -lrt /sys/class/net/ |grep $i |awk ' {print $11}' |awk -F "/" ' {print $4}')"',\"'Flg'\":'"$(ifconfig -s $i |grep -v Flg |awk '{print $11}')"',\"'sent_packets'\":'"$(ifconfig $i |grep  TX |head -1 |awk '{print $3}')"'}"
fi
if [ $i != lo ]; then


echo -e "{\"'name'\":\"'"$i"'\",\"'ip_address'\":'"$(ifconfig $i |grep inet |grep -v inet6 |awk '{print $2}')"',\"'mac_address'\":'"$(ifconfig $i |grep ether |awk '{print $2}')"',\"'dhcp_enabled'\":'"$(cat  /etc/sysconfig/network-scripts/ifcfg-$i |grep BOOTPROTO |awk -F "=" '{print $2}')"',\"'packet_max_size'\":'"$(ifconfig $i |grep mtu | awk '{print $NF}')"',\"'speed'\":'"$(sudo ethtool $i |grep Speed |awk '{print $2}')"',\"'ip_subnet'\":'"$(ifconfig $i |grep inet |grep -v inet6 |awk '{print $4}')"',\"'dns_server'\":'"$(cat /etc/resolv.conf |grep -i '^nameserver'|head -n1|cut -d ' ' -f2)"',\"'received_packets'\":'"$(ifconfig $i |grep -i RX |head -1 |awk '{print $3}')"',\"'type'\":'"physical"',\"'Flg'\":'"$(ifconfig -s $i |grep -v Flg |awk '{print $11}')"',\"'sent_packets'\":'"$(ifconfig $i |grep  TX |head -1 |awk '{print $3}')"'}"


fi

done


You may want to first disentangle this kludge of a script before you start to add functionality. I don't know your monitor but i am quite sure the echo-lines are not fitting onto it.On my terminal it spans ten lines and i refuse to read beyond the first.



How about:


Code:
#!/usr/bin/env bash



get_ipaddress ()
{
name="$1"
printf "%s\n" "$(ifconfig $name | grep  inet[^6] | awk '{print  $2}')"


return 0
}




get_packetmaxsize ()
{
name="$1"
print "%s\n" $(ifconfig $i |grep mtu | awk '{print  $NF}')


return 0
}





for i in $(ip link show | awk -F: '$1>0 {print $2}') ; do
      if [ $i == "lo" ]; then
           print "\"%s\":\"%s\"" "$i" $(get_ipaddress "$i")
           print "\"%s\":\"%s\"" "packet_max_size" $(get_packetmaxsize "$i")
           [....]
           printf "\n"

      else
            print "\"%s\":\"%s\"" "$i" $(get_ipaddress "$i")
           print "\"%s\":\"%s\"" "packet_max_size" $(get_packetmaxsize "$i")
            [....]
           printf "\n"


     fi
done

Basically you create a single function for every information part you want, then put it all together in a main loop which cycles through all the interfaces. The creation of the other function is left to the interested reader as an exercise. This presents you the added opportunity to remove the UUOCs you so generously spilled into your code.
This User Gave Thanks to foad For This Post:
# 3  
Old 11-19-2019
Here is an awk example with ip -ooption (among others).
Not everything is parsed, but you get the idea with this example how to proceed if you want (or not Smilie

Quote:
-o, -oneline
output each record on a single line, replacing line feeds with the '\' character. This is convenient when you want to count records with wc(1) or to grep(1) the output.
Perhaps somebody can shorten this out, assumption is ip with those options will print out stuff equally on linux boxes (for now probably Smilie )
Correction and/or suggestions are welcomed of course!

Save following as intinfo.awk :
Code:
BEGIN {
FS="\\"
OFS=" "
}
{
# split first field on ":" into array a
split($1,a,":")
# hardcode wanted info from array members into variables
interface=a[2]
interface_info=a[3]
# split variable 'interface_info'  on "<space>" into array b
split(interface_info,b," ")
# iterate over array 'b' detecting info
for ( i in b ) {
	# if string state is matched
	if ( b[i] == "state" )
	# define 'status' variable as next member of array
	status=b[i+1]
	# if string mtu is matched
	if ( b[i] == "mtu" )
	# define 'mtu' variable as next member of array
	mtu=b[i+1]
}
# execute ethtool for information
ethtool="ethtool "interface
# while we read ethtool command output line by line
while ( ( ethtool | getline info ) > 0 ) {
# Detect speed and link status for interface
if ( match (info, /Speed: [0-9].*s/) )
	rmstr="Speed: "
     	speed=substr(info,RSTART+length(rmstr),RLENGTH)

( info ~ "Link detected: yes" )  ? lst="UP" : lst="DOWN"
}
close(ethtool)
# remove starting whitespace from interface name
gsub("^ ","",interface)
# print and output and json-ify whatever...
printf ("{\"'name'\":\"'%s'\",\"'status'\":'%s',\"'packet_max_size'\":'%s'}\n", interface, lst, mtu)

}

Ran on my home box :
Code:
root@glitch:~# ip -o -s -a l show  | awk -f intinfo.awk 
{"'name'":"'lo'","'status'":'UP',"'packet_max_size'":'65536'}
{"'name'":"'enp3s0'","'status'":'UP',"'packet_max_size'":'1500'}
{"'name'":"'ovs-system'","'status'":'DOWN',"'packet_max_size'":'1500'}
{"'name'":"'brclust'","'status'":'UP',"'packet_max_size'":'1500'}
{"'name'":"'brintlan'","'status'":'UP',"'packet_max_size'":'1500'}
{"'name'":"'brpub'","'status'":'UP',"'packet_max_size'":'1500'}
{"'name'":"'homelan0'","'status'":'UP',"'packet_max_size'":'1500'}
{"'name'":"'brvxlan'","'status'":'UP',"'packet_max_size'":'1500'}
{"'name'":"'vxlan_sys_4789'","'status'":'UP',"'packet_max_size'":'65000'}
{"'name'":"'vnet0'","'status'":'UP',"'packet_max_size'":'1500'}
{"'name'":"'vnet1'","'status'":'UP',"'packet_max_size'":'1500'}
{"'name'":"'vnet2'","'status'":'DOWN',"'packet_max_size'":'1500'}

Hope that helps
Regards
Peasant.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Network Printing issue

I have network printer registerd in solaris environment. When I issue print, it prints banner page. The issue printer is registerd in Print server which is running on Linux environment. So when I issue print in Print server, it doesn't print banner page as banner settings are made to turn off It... (6 Replies)
Discussion started by: Sunil Koya
6 Replies

2. Shell Programming and Scripting

How to stop Sqlplus command from printing db connection details

Hi, Could someone tell me how to stop SQLPLUS command from printing the connection details in the console. Below is the lines i get in console when executing the sqlplus... SQL*Plus: Release 10.2.0.1.0 - Production on Wed Mar 9 03:31:03 2011 Copyright (c) 1982, 2005, Oracle. All rights... (2 Replies)
Discussion started by: funonnet
2 Replies

3. Solaris

Network Card Name details

Hi All, Can somebody explain that whats the meaning of different interfaces naming scheme, Like ' hme, qfe, e1000g0, ce, eri, bge' ? This is such a basic question which i always overlooked :( Thanks ! (6 Replies)
Discussion started by: Solarister
6 Replies

4. Solaris

Getting Network Interface details in solaris

Hi, /sbin/ifconfig -a when i login as guest user , output like below, lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1 inet 127.0.0.1 netmask ff000000 eri0: flags=1004843<UP,BROADCAST,RUNNING,MULTICAST,DHCP,IPv4> mtu 1500 index 2 inet... (5 Replies)
Discussion started by: forumguest
5 Replies

5. Solaris

Network printing solaris 2.5.1

Hello guys, I'm having a problem at configuring an HP printer for solaris 2.5.1. Unfortunatly solaris did not support network printing until 2.6.x so i had to use jetadmin, wich suprisingly worked well the verry first time i installed the package. Allthough the sparcstation witch i'm using is... (0 Replies)
Discussion started by: Goodfellas
0 Replies

6. UNIX for Dummies Questions & Answers

Get Network and Node details

which unix cmd will give the following result... Node Name Task Name ID Status Network NUTTS CTP_E 1000 In Service X25/OFF TCP/ON MUSKET CTP_E 2011 Unreachable X25/OFF TCP/OFF ... (0 Replies)
Discussion started by: rahulrathod
0 Replies

7. UNIX for Advanced & Expert Users

HP Network Printing

I am installing a HP printer using ./hpnpcfg and I am getting the following: Please enter selection: 5 Enter the network peripheral name or IP address: hpndlife The following types of test files can be sent to the printer: 1) text file ... (1 Reply)
Discussion started by: likando
1 Replies

8. UNIX for Dummies Questions & Answers

Hp Network Printing

I am installing a HP printer using ./hpnpcfg and I am getting the following: Please enter selection: 5 Enter the network peripheral name or IP address: hpndlife The following types of test files can be sent to the printer: 1) text file ... (0 Replies)
Discussion started by: likando
0 Replies

9. Post Here to Contact Site Administrators and Moderators

Sco Network Printing

I am installing a HP printer using ./hpnpcfg and I am getting the following: Please enter selection: 5 Enter the network peripheral name or IP address: hpndlife The following types of test files can be sent to the printer: 1) text file ... (0 Replies)
Discussion started by: Likando Luywa
0 Replies

10. IP Networking

network printing

When I send a request to print to a HP4si networked printer, It prints an extra page with User, Host, Class & job info. Is it possible to eliminate this page? (2 Replies)
Discussion started by: sbuck331
2 Replies
Login or Register to Ask a Question