Shell exercise: Analyze Nic Information

Source: Internet
Author: User

 


Use shell scripts to analyze Nic status information

Question:

Shell programming
1. Write a script to read the traffic on the NIC, the number of packets received and sent, the number of packet loss, and the packet loss rate;
2. The NIC name must be configurable, not limited to ethx. For example, pagx (X is a number) and the number of NICs can also be configured;
3. Configure relevant parameters to control the display information of each Nic;
4. The output of the program must be concise and intuitive, and the output result should be easily processed twice;
5. The configuration file should be independent of the shell program;
6. Consider the statistical differences of the shell in different operating system versions (consider systems such as As5, as6, turbolinux)


This requirement is an internal test question for a new employee of a certain company. The employee asked me for help and I wrote a version for him. If you can understand this script, congratulations, you are no longer a newbie. Haha .. Except for the sixth point, you must test the script as follows:

Script Name: nic_information_analysis.sh

#! /Bin/bash
# Date: 2014-09-10
# Author: yourname
# Function:
# Used to analysis information for NIC (network interface card)
# VARIABLES description:
# Scriptdir --- the directory path of script
# Nicname --- storage the NIC Name List
# RX --- recive flow, but the unit is byte
# TX --- transfer flow, but the unit is byte
# Reciveflow --- recive flow, but the unit is Mbyte
# Transferflow --- transfer flow, but the unit is Mbyte
# Totalflow ---- the summary of reciveflow and transferflow, the unit is Mbyte
# Rpkgnum --- recive packets number
# Spkgnum --- send (transfer) packets number
# Rdpkgnum --- the drop packet number based on recive packets
# Sdpkgnum --- the drop packet number based on send packets
# Rdratio --- the drop packet ratio based on recive packets
# Sdratio --- the drop packet ratio based on send packets
#
# Arguments description:
# RP --- recive packets
# TP --- transfer packets
# DP --- Drop packets
# DRA --- Drop Ratio
# All --- all information for every netcard
#

# Description:
# Please set variable "scriptdir" first !!!
#

# Notice: This script and config.txt must be in the same directory, or you can set right # directory

#
Scriptdir =/home/workspace
. $ Scriptdir/config.txt

For nic in $ nicname
Do
Ifconfig $ Nic> &/dev/null
[$? -Ne 0] & Echo-e "\ 033 [31 mwrong: Nic $ Nic is not exist! Please check config file: $ scriptdir/config.txt \ 033 [0 m "& Exit 1
Done

Function flow_count (){
RX = 'ifconfig $1 | sed-n'/RX Bytes/S/^ [[: Space:] * // P' | awk-F' [:] ''{ Print $3 }''
Tx = 'ifconfig $1 | sed-n'/RX Bytes/S/^ [[: Space:] * // P' | awk-F' [:] ''{print $(NF-2 )}''
Reciveflow = $ (echo "scale = 2; $ Rx/1024" | BC) m
Transferflow = $ (echo "scale = 2; $ Tx/1024" | BC) m
Totalflow = $ (echo "scale = 2; 'expr $ RX + $ TX '/1024" | BC) m
Echo "RX flow is $ reciveflow"
Echo "TX flow is $ transferflow"
Echo "total flow is $ totalflow"
Echo
}

Function recivepackets_count (){
Rpkgnum = 'ifconfig $1 | sed-n'/RX packets/S/^ [[: Space:] * // P' | awk-F' [:] ''{ Print $3 }''
Echo "recive packet number is: $ rpkgnum"
Echo
}
Function sendpackets_count (){
Spkgnum = 'ifconfig $1 | sed-n'/TX packets/S/^ [[: Space:] * // P' | awk-F' [:] ''{ Print $3 }''
Echo "Send packet number is: $ spkgnum"
Echo
}
Function droppackets_count (){
Rdpkgnum = 'ifconfig $1 | sed-n'/RX packets/S/^ [[: Space:] * // P' | awk-F' [:] ''{ Print $7 }''
Sdpkgnum = 'ifconfig $1 | sed-n'/TX packets/S/^ [[: Space:] * // P' | awk-F' [:] ''{ Print $7 }''
Echo "Drop packet number of Rx is: $ rdpkgnum"
Echo "Drop packet number of TX is: $ sdpkgnum"
Echo
}
Function dropratio_count (){
Rdratio = $ (echo "scale = 2; $ rdpkgnum/($ rdpkgnum + $ rpkgnum) * 100" | BC) %
Sdratio = $ (echo "scale = 2; $ sdpkgnum/($ sdpkgnum + $ spkgnum) * 100" | BC) %
Echo "Drop Ratio of Rx is: $ rdratio"
Echo "Drop Ratio of TX is: $ rdratio"
Echo
}

Function dropratio_count_single (){
Rpkgnum = 'ifconfig $1 | sed-n'/RX packets/S/^ [[: Space:] * // P' | awk-F' [:] ''{ Print $3 }''
Spkgnum = 'ifconfig $1 | sed-n'/TX packets/S/^ [[: Space:] * // P' | awk-F' [:] ''{ Print $3 }''
Rdpkgnum = 'ifconfig $1 | sed-n'/RX packets/S/^ [[: Space:] * // P' | awk-F' [:] ''{ Print $7 }''
Sdpkgnum = 'ifconfig $1 | sed-n'/TX packets/S/^ [[: Space:] * // P' | awk-F' [:] ''{ Print $7 }''
Rdratio = $ (echo "scale = 2; $ rdpkgnum/($ rdpkgnum + $ rpkgnum) * 100" | BC) %
Sdratio = $ (echo "scale = 2; $ sdpkgnum/($ sdpkgnum + $ spkgnum) * 100" | BC) %
Echo "Drop Ratio of Rx is: $ rdratio"
Echo "Drop Ratio of TX is: $ sdratio"
Echo
}

Function all_information_summary (){
Flow_count $1
Recivepackets_count $1
Sendpackets_count $1
Droppackets_count $1
Dropratio_count $1
}

Function print_information (){
Echo "follow information is about $1 of every netcard :"
}

Function analysis_result (){
For I in $ nicname
Do
Echo-e "\ 033 [31 mnetcard is $ I: \ 033 [0 m"
$1 $ I
Done
}

Case $1 in
Flow)
Print_information "the flow information"
Analysis_result flow_count
;;
RP)
Print_information "The recive packet number"
Analysis_result recivepackets_count
;;
TP)
Print_information "The send packet number"
Analysis_result sendpackets_count

;;
DP)
Print_information "the drop packet number"
Analysis_result droppackets_count
;;
DRA)
Print_information "the drop packet ratio"
Analysis_result dropratio_count_single
;;
All)
Print_information "all information"
Analysis_result all_information_summary
;;
*)
Echo "usage $0 {flow | RP | TP | DP | DRA | all }"
Echo
;;
Esac



Nic configuration file name: config.txt, the content is as follows:

# Configure your network card
Nicname = "eth0 Lo"


This configuration file allows you to add or delete NICs at will, so as to meet your needs, you do not need to move any script content .. The configuration file and the script should be placed in the same directory, as described in the script ..


The following shows the execution result:

650) This. width = 650; "style =" float: none; "Title =" 3.jpg" alt = "wKioL1QRRkeRFmgCAAPSdUa-3_A084.jpg" src = "http://s3.51cto.com/wyfs02/M02/49/3D/wKioL1QRRkeRFmgCAAPSdUa-3_A084.jpg"/>

650) This. width = 650; "style =" float: none; "Title =" 4.jpg" alt = "wkiom1qr1_icrrgxaaji9fpgnbg005.jpg" src = "http://s3.51cto.com/wyfs02/M02/49/3B/wKiom1QRRjiCRRgxAAJi9FpgnBg005.jpg"/>

650) This. width = 650; "style =" float: none; "Title =" 5.jpg" alt = "wkiol1qrrkijvf2haaknex6kskw475.jpg" src = "http://s3.51cto.com/wyfs02/M00/49/3D/wKioL1QRRkijVF2HAAKNEx6kSKw475.jpg"/>

 

From the above information, we can see that there is no problem with script execution, and the function is basically achieved... Someone may ask? What if I pass multiple parameters at a time? Okay. You can solve this problem by yourself. Haha !!!

 

End !!!

Stupid technology ------ not afraid of you !!!

 


 



This article is from the "Stupid technology" blog, please be sure to keep this source http://mingyang.blog.51cto.com/2807508/1551153

Shell exercise: Analyze Nic Information

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.