Sponsored Content
Full Discussion: Printing network details
Top Forums UNIX for Beginners Questions & Answers Printing network details Post 303041212 by balu1234 on Monday 18th of November 2019 11:15:28 AM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 04:40 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy