tnetwork - scripts - various script and utils
(HTM) git clone git://z3bra.org/scripts
(DIR) Log
(DIR) Files
(DIR) Refs
---
tnetwork (1103B)
---
1 #!/bin/sh
2 #
3 # z3bra - (c) wtfpl 2014
4
5 usage () {
6 cat <<EOF
7 usage: $(basename $0) [-hicwdu]
8 -h : print help
9 -i : print out current interface
10 -c : return 0 if connected to a network (default)
11 -w : return 0 if connected over wifi
12 -d : print out size of downloaded packets
13 -u : print out size of uploaded packets
14 EOF
15 }
16
17 # get current interface
18 netint() {
19 for int in $(ls /sys/class/net); do
20 if grep -q ${int} /proc/net/route; then
21 echo ${int}
22 return 0
23 fi
24 done
25
26 # no interface up, return loopback
27 echo "lo"
28 }
29
30 netstate () {
31 grep -q $(netint) /proc/net/route && return 0 || return 1
32 }
33
34 wireless () {
35 grep -q $(netint) /proc/net/wireless && return 0 || return 1
36 }
37
38 # get upload/download traffic
39 nettraffic() {
40 case $1 in
41 up) col=10 ;;
42 *) col=2 ;;
43 esac
44
45 traffic=$(awk "/$(netint)/ {print \$$col}" /proc/net/dev)
46
47 human ${traffic}
48 }
49
50 case $1 in
51 -h) usage ;;
52 -i) netint ;;
53 -w) wireless ;;
54 -d) nettraffic down ;;
55 -u) nettraffic up ;;
56 *) netstate ;;
57 esac