經過命令netstat獲取鏈路狀態,保存到/tmp下一個文件中。經過添加計劃任務週期性獲取鏈路信息。能夠手動添加鏈路信息,能夠刪除保存的鏈路狀態。還能夠任意查看最近三次保存的信息。
git
獲取全部保存的信息:vim
$ vlinkstatus -a
獲取最近一條、兩條、三條保存的信息bash
$ vlinkstatus -r $ vlinkstatus -r 2 $ vlinkstatus -r 3
獲取最近腳本幫助信息、版本信息ide
$ vlinkstatus -h $ vlinkstatus -v vlinkstatus-1.2.sh
手動寫入信息、清除保存的信息this
$ vlinkstatus -w $ vlinkstatus --clear
腳本「vlinkstatus-1.2.sh」代碼以下:
spa
#!/bin/bash # vlinkstatus-1.2.sh TMPFILE="/tmp/.tmp_vlinkstatus.txt" write_it () { echo "" >> $TMPFILE date +%F_%T >> $TMPFILE && echo "Write date information." netstat -4 -natup >> $TMPFILE && echo "Write state information." } clear_it () { rm -f $TMPFILE } # The first position parameter of the function is the second position # parameter of the script. # position parameter - show a few records, values allow 1,2,3. # default is 1. read_itTail () { declare tmp_datestamp="" declare -i k=1 case $1 in 2) k=2 ;; 3) k=3 ;; *) k=1 ;; esac tmp_datestamp="`vlinkstatus -a | grep "^[[:digit:]]\{4\}" | tail -$k | head -1`" sed -n '/'$tmp_datestamp'/,/$$/p' $TMPFILE } read_itAll () { cat $TMPFILE 2> /dev/null } edit_tmpFile () { vim $TMPFILE } view_version () { sed -n '/vlinkstatus/p' $0 | sed -n '1p' | cut -d# -f2 } view_help () { cat << EOF Usage: vlinkstatus [OPTION] [PARAMETER of read|r] View the link state information. Mandatory arguments to long option are mandatory for short options too. -r, --read read reslut from tmp file tailed. read the last set of data by default. Allows up to 3 sets of data to be read. -a, --all read reslut from tmp file. --edit edit temporary file. -v, --version display version. -w, --write write reslut to tmp file. --clear delete the temporary file. -h, --help display this help and exit E-mail bug reports to: <773805731@qq.com> EOF } case $1 in -w|--write) write_it ;; -r|--read) read_itTail $2 ;; -a|--all) read_itAll ;; --edit) edit_tmpFile ;; --clear) clear_it ;; -v|--version) view_version ;; *) view_help ;; esac