Sponsored Content
Full Discussion: Printing network details
Top Forums UNIX for Beginners Questions & Answers Printing network details Post 303041244 by foad on Tuesday 19th of November 2019 09:45:43 AM
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:
 

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 07:44 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy