connect-stat.sh

来自「Shall高级编程」· Shell 代码 · 共 55 行

SH
55
字号
#!/bin/bashPROCNAME=pppd        # ppp daemonPROCFILENAME=status  # Where to look.NOTCONNECTED=65INTERVAL=2           # Update every 2 seconds.pidno=$( ps ax | grep -v "ps ax" | grep -v grep | grep $PROCNAME | awk '{ print $1 }' )# Finding the process number of 'pppd', the 'ppp daemon'.# Have to filter out the process lines generated by the search itself.##  However, as Oleg Philon points out,#+ this could have been considerably simplified by using "pidof".#  pidno=$( pidof $PROCNAME )##  Moral of the story:#+ When a command sequence gets too complex, look for a shortcut.if [ -z "$pidno" ]   # If no pid, then process is not running.then  echo "Not connected."  exit $NOTCONNECTEDelse  echo "Connected."; echofiwhile [ true ]       # Endless loop, script can be improved here.do  if [ ! -e "/proc/$pidno/$PROCFILENAME" ]  # While process running, then "status" file exists.  then    echo "Disconnected."    exit $NOTCONNECTED  finetstat -s | grep "packets received"  # Get some connect statistics.netstat -s | grep "packets delivered"  sleep $INTERVAL  echo; echodoneexit 0# As it stands, this script must be terminated with a Control-C.#    Exercises:#    ---------#    Improve the script so it exits on a "q" keystroke.#    Make the script more user-friendly in other ways.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?