通过命令netstat获取链路状态,保存到/tmp下一个文件中。通过添加计划任务周期性获取链路信息。可以手动添加链路信息,可以删除保存的链路状态。还可以任意查看最近三次保存的信息。
获取所有保存的信息:$ vlinkstatus -a
获取最近一条、两条、三条保存的信息$ vlinkstatus -r
$ vlinkstatus -r 2
$ vlinkstatus -r 3
获取最近脚本帮助信息、版本信息$ vlinkstatus -h
$ vlinkstatus -v
vlinkstatus-1.2.sh
手动写入信息、清除保存的信息$ vlinkstatus -w
$ vlinkstatus --clear
脚本“vlinkstatus-1.2.sh”代码如下:#!/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 <
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