| 1 | #!/bin/bash |
| 2 | # connect-stat.sh |
| 3 | # Note that this script may need modification |
| 4 | #+ to work with a wireless connection. |
| 5 | |
| 6 | PROCNAME=pppd # ppp daemon |
| 7 | PROCFILENAME=status # Where to look. |
| 8 | NOTCONNECTED=85 |
| 9 | INTERVAL=2 # Update every 2 seconds. |
| 10 | |
| 11 | pidno=$( ps ax | grep -v "ps ax" | grep -v grep | grep $PROCNAME | |
| 12 | awk '{ print $1 }' ) |
| 13 | |
| 14 | # Finding the process number of 'pppd', the 'ppp daemon'. |
| 15 | # Have to filter out the process lines generated by the search itself. |
| 16 | # |
| 17 | # However, as Oleg Philon points out, |
| 18 | #+ this could have been considerably simplified by using "pidof". |
| 19 | # pidno=$( pidof $PROCNAME ) |
| 20 | # |
| 21 | # Moral of the story: |
| 22 | #+ When a command sequence gets too complex, look for a shortcut. |
| 23 | |
| 24 | |
| 25 | if [ -z "$pidno" ] # If no pid, then process is not running. |
| 26 | then |
| 27 | echo "Not connected." |
| 28 | # exit $NOTCONNECTED |
| 29 | else |
| 30 | echo "Connected."; echo |
| 31 | fi |
| 32 | |
| 33 | while [ true ] # Endless loop, script can be improved here. |
| 34 | do |
| 35 | |
| 36 | if [ ! -e "/proc/$pidno/$PROCFILENAME" ] |
| 37 | # While process running, then "status" file exists. |
| 38 | then |
| 39 | echo "Disconnected." |
| 40 | # exit $NOTCONNECTED |
| 41 | fi |
| 42 | |
| 43 | netstat -s | grep "packets received" # Get some connect statistics. |
| 44 | netstat -s | grep "packets delivered" |
| 45 | |
| 46 | |
| 47 | sleep $INTERVAL |
| 48 | echo; echo |
| 49 | |
| 50 | done |
| 51 | |
| 52 | exit 0 |
| 53 | |
| 54 | # As it stands, this script must be terminated with a Control-C. |
| 55 | |
| 56 | # Exercises: |
| 57 | # --------- |
| 58 | # Improve the script so it exits on a "q" keystroke. |
| 59 | # Make the script more user-friendly in other ways. |
| 60 | # Fix the script to work with wireless/DSL connections. |