1 | #!/bin/sh |
---|
2 | |
---|
3 | . /sbin/start-function |
---|
4 | ethfile="/mnt/network/interfaces" |
---|
5 | ID= |
---|
6 | board=`cat /etc/.board` |
---|
7 | |
---|
8 | set_info() |
---|
9 | { |
---|
10 | dns=`awk '/nameserver/ { print $2 }' /mnt/network/resolv.conf` |
---|
11 | if [ $board = "inihdp" ];then |
---|
12 | case $showip in ip) writeoled "$ID$IP" ;; esac |
---|
13 | else |
---|
14 | case $showip in ip) echo "$ID$IP" > /dev/oled0;; esac |
---|
15 | fi |
---|
16 | } |
---|
17 | |
---|
18 | set_hosts() |
---|
19 | { |
---|
20 | case $IFACE in |
---|
21 | eth0) |
---|
22 | if ! grep -q "$hostname" /mnt/network/hosts; then |
---|
23 | echo "$IP $hostname" >> /mnt/network/hosts |
---|
24 | else |
---|
25 | hostsip=`grep -m1 "$hostname" /mnt/network/hosts`; fastcut "$hostsip" " " 1; hostsip="$fcr" |
---|
26 | if [ "$hostsip" != "$IP" ]; then |
---|
27 | sed s/"$hostsip $hostname"/"$IP $hostname"/ -i /mnt/network/hosts |
---|
28 | fi |
---|
29 | fi;; |
---|
30 | *) |
---|
31 | ID="W" |
---|
32 | ifmetric eth0 10;; |
---|
33 | esac |
---|
34 | } |
---|
35 | |
---|
36 | set_interfaces() { |
---|
37 | MEM=$IFS;IFS= |
---|
38 | if grep -q "$IFACE inet static" "$ethfile"; then |
---|
39 | echo "[$PROG] $IFACE using static ip" |
---|
40 | ifconfig $IFACE up |
---|
41 | IP=`grep -A3 "iface $IFACE inet" "$ethfile"`; fastgrep "$IP" "address"; IP=`echo "$fgr" | sed s"/^\s*//; s/ \+/ /g"`; fastcut "$IP" " " 2; IP="$fcr" |
---|
42 | GW=`grep -A3 "iface $IFACE inet" "$ethfile"`; fastgrep "$GW" "gateway"; GW=`echo "$fgr" | sed s"/^\s*//; s/ \+/ /g"`; fastcut "$GW" " " 2; GW="$fcr" |
---|
43 | MASK=`grep -A3 "iface $IFACE inet" "$ethfile"`; fastgrep "$MASK" "netmask"; MASK=`echo "$fgr" | sed s"/^\s*//; s/ \+/ /g"`; fastcut "$MASK" " " 2; MASK="$fcr" |
---|
44 | case $MASK in "") MASK="255.255.255.0";; esac |
---|
45 | case $IP in |
---|
46 | "") ;; |
---|
47 | *) |
---|
48 | ifconfig $IFACE $IP netmask $MASK |
---|
49 | hostname=`hostname` |
---|
50 | set_hosts;; |
---|
51 | esac |
---|
52 | case $GW in |
---|
53 | "") ;; |
---|
54 | *) |
---|
55 | route del default 2>/dev/null |
---|
56 | route add default gw $GW |
---|
57 | echo "$GW" > /mnt/network/default_gw;; |
---|
58 | esac |
---|
59 | set_info |
---|
60 | elif grep -q "$IFACE inet dhcp" "$ethfile"; then |
---|
61 | echo "[$PROG] $IFACE using dhcp" |
---|
62 | ifconfig $IFACE up |
---|
63 | route del default 2>/dev/null |
---|
64 | hostname=`hostname` |
---|
65 | udhcpc -t 10 -i $IFACE -n -q -x hostname:"$hostname" |
---|
66 | ifconfig=`ifconfig $IFACE`; fastgrep "$ifconfig" "inet addr:"; ifconfig="$fgr" |
---|
67 | IP="${ifconfig/*inet addr:/}"; IP="${IP/ */}" |
---|
68 | MASK="${ifconfig/*Mask:/}"; MASK="${MASK/ */}" |
---|
69 | GW=`route -n | grep -m1 "^0.0.0.0" | sed s/" \+"/" "/g`; fastcut "$GW" " " 2; GW="$fcr" |
---|
70 | echo "$GW" > /mnt/network/default_gw |
---|
71 | case $IP in |
---|
72 | "") ;; |
---|
73 | *) set_hosts;; |
---|
74 | esac |
---|
75 | set_info |
---|
76 | elif grep -q "$IFACE inet off" "$ethfile"; then |
---|
77 | ifconfig $IFACE down |
---|
78 | fi |
---|
79 | if grep -q "iface lo inet loopback" "$ethfile"; then |
---|
80 | ifconfig lo 127.0.0.1 |
---|
81 | fi |
---|
82 | IFS=$MEM |
---|
83 | } |
---|
84 | |
---|
85 | stop_interfaces() |
---|
86 | { |
---|
87 | ip addr flush $IFACE |
---|
88 | } |
---|
89 | |
---|
90 | usage() { |
---|
91 | echo "Usage: $PROG [-i <INTERFACE>] [-s <showip>] <start|stop|restart>" |
---|
92 | exit |
---|
93 | } |
---|
94 | |
---|
95 | PROG="networking" |
---|
96 | IFACE="eth0" |
---|
97 | while [ -n "$*" ]; do |
---|
98 | case $1 in |
---|
99 | -i) shift |
---|
100 | IFACE=$1 |
---|
101 | shift;; |
---|
102 | -s) shift |
---|
103 | showip=$1 |
---|
104 | shift;; |
---|
105 | start|stop|restart) |
---|
106 | MODE=$1 |
---|
107 | shift;; |
---|
108 | *) |
---|
109 | usage;; |
---|
110 | esac |
---|
111 | done |
---|
112 | |
---|
113 | case $MODE in |
---|
114 | start) |
---|
115 | echo "[$PROG] start $IFACE..." |
---|
116 | set_interfaces;; |
---|
117 | stop) |
---|
118 | echo "[$PROG] stop $IFACE..." |
---|
119 | stop_interfaces;; |
---|
120 | restart) |
---|
121 | echo "[$PROG] restart $IFACE..." |
---|
122 | stop_interfaces |
---|
123 | set_interfaces;; |
---|
124 | *) |
---|
125 | usage;; |
---|
126 | esac |
---|