This is the first thing I did in my company that is directly related to programming and requires shell commands. In fact, I feel that the shell under Linux is the same as the DOS batch processing under windows.
Question:
1. Use the shell language stream control combined with ifconfig, grep, cut, Echo, sed and other commands to obtain from the interface
IP address, subnet mask, and Ethernet hardware address program showif. Sh.
① Call the program in the form of showif. Sh <parameter>.
② Output the result of <parameter>.
③ If the input parameter is incorrect, the help information is printed.
④ The execution and output are as follows:
#./Showif. Sh IPv4
#192.168.0.172
2. Use shell language stream control combined with ifconfig, grep, cut, Echo, sed and other commands to collect interface traffic in real time
Program speed. Sh.
Requirement: ① The Statistical interval is 3 seconds.
② Call the program in the form of speed. Sh <if Name>.
③ If the input parameter is incorrect, the help information is printed.
④ The execution and output are as follows:
#./Speed. Sh eth0
# Eth0: recive-> 128.36 K transmit-> 48.75 K
# Eth0: recive-> 268.20 K transmit-> 78.00 K
I spent a day and a half on the specific implementation:
1. The function is basically implemented here, but it is not very correct in using regular expressions, and the search method is relatively simple.
#! /Bin/bash
# Filename: showif
If test $ # = 0; then
Echo "the number of parameters cannot be 0. Enter the correct parameter [IPv4 | submask | Mac]"
Else if test $1 = "IPv4"; then
Echo "output IPv4 address :"
/Sbin/ifconfig | awk '{if ($1 = "Inet" & $3 ~ /^ Bcast/) Print $2} '| cut-F2-d ":"
Else if test $1 = "submask"; then
Echo "output submask subnet mask :"
Echo '/sbin/ifconfig | awk-F "Mask"'/mask/{print $2} ''| cut-F2-d ":"
Echo '/sbin/ifconfig | awk-F "Mask"'/mask/{print $2} ''| cut-F3-d ":"
Echo '/sbin/ifconfig | awk-F "Mask"'/mask/{print $2} ''| cut-F4-d ":"
Echo '/sbin/ifconfig | awk-F "Mask"'/mask/{print $2} ''| cut-F5-d ":"
Else if test $1 = "Mac"; then
Echo "output Mac physical address :"
Echo '/sbin/ifconfig | awk-F "hwaddr"'/hwaddr/{print $2} ''| cut-F1-d ""
Echo '/sbin/ifconfig | awk-F "hwaddr"'/hwaddr/{print $2} ''| cut-F2-d ""
Echo '/sbin/ifconfig | awk-F "hwaddr"'/hwaddr/{print $2} ''| cut-F3-d ""
Else
Echo "enter the correct parameter [IPv4 | submask | Mac]"
Fi
Fi
Fi
Fi
2. the filtering method (Regular Expression) used is not good, and the input of the invalid parameter is not completed (parameter n has been defined ):
#! /Bin/bash
# Filename: Speed
Delay = 1
# Here, n is used to detect input parameter errors, but it is not implemented in detail.
N = 1
Now_get = '/sbin/ifconfig $1 | awk-F "RX Bytes:"'/Rx/{print $2} '| cut-F1-d ""'
Now_up = '/sbin/ifconfig $1 | awk-F "TX Bytes:"'/Tx/{print $2} '| cut-F1-d ""'
While (test $ n-Ne 0)
Do
Have_get = $ now_get
Have_up = $ now_up
Now_get = '/sbin/ifconfig $1 | awk-F "RX Bytes:"'/Rx/{print $2} '| cut-F1-d ""'
Now_up = '/sbin/ifconfig $1 | awk-F "TX Bytes:"'/Tx/{print $2} '| cut-F1-d ""'
Recv_bytes = 'expr $ now_get-$ have_get'
Recv_bytes = 'echo "scale = 0; $ recv_bytes/$ delay" | bc'
# Recv_bytes = 'echo "scale = 0; $ recv_bytes/delay" | bc'
Recv_kb = 'echo "scale = 3; $ recv_bytes/1024" | bc'
Tran_bytes = 'expr $ now_up-$ have_up'
Tran_bytes = 'echo "scale = 0; $ tran_bytes/$ delay" | bc'
# Tran_bytes = 'echo "scale = 0; $ tran_bytes/delay" | bc'
Tran_kb = 'echo "scale = 3; $ tran_bytes/1024" | bc'
# Echo "Download:" $ recv_bytes "B/S upload:" $ tran_bytes "B/S"
Echo "Download:" $ recv_kb "kb/s upload:" $ tran_kb "kb/s"
Sleep $ Delay
Done