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 :
#!/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 :
{"'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
{"'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
{"'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.