blob: bfc2532d4981943f6bd855355801b996f208f81b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
#!/bin/bash
. /etc/rc.conf
. /etc/rc.d/functions
[ -f /etc/conf.d/dhcpcd ] && . /etc/conf.d/dhcpcd
NETCFG_VER=0.1
PATH="/bin:/usr/bin:/sbin:/usr/sbin:$PATH"
PROFILE_DIR="/etc/network-profiles"
STATE_DIR="/var/run/net"
version()
{
echo "netcfg v$NETCFG_VER"
}
usage()
{
version
echo
echo "usage: netcfg [options] <profile_name>"
echo " netcfg --stop <interface>"
echo " netcfg --menu [--timeout <secs>]"
echo " netcfg --stopall"
echo
echo "options:"
echo " -c Don't reconfigure an interface if it's already up"
echo
echo "Network profiles are stored in $PROFILE_DIR"
echo
}
stop_profile()
{
if [ "$1" = "" ]; then
echo "error: missing interface name (eg, eth0)"
exit 1
fi
INTERFACE=$1
[ -f $STATE_DIR/$INTERFACE ] || return
unset GATEWAY
. $STATE_DIR/$INTERFACE
stat_busy "Shutting down interface: $INTERFACE"
# bring down the default route (gateway)
[ "$GATEWAY" ] && route del default gw $GATEWAY
# bring down the interface
ifconfig $INTERFACE down
rm -f $STATE_DIR/$INTERFACE
stat_done
}
stop_all()
{
[ -d $STATE_DIR ] || return
for prof in `ls $STATE_DIR`; do
unset INTERFACE
. $STATE_DIR/$prof
stop_profile $INTERFACE
done
}
start_profile()
{
if [ "$1" = "" ]; then
echo "error: missing profile name"
exit 1
fi
if [ ! -f $PROFILE_DIR/$1 ]; then
echo "error: $PROFILE_DIR/$1 is missing" >&2
exit 1
fi
# Read the profile
. $PROFILE_DIR/$1
[ "$CHECK" = "1" -a -f $STATE_DIR/$INTERFACE ] && return
# Shut down any profiles tied to this interface
stop_profile $INTERFACE
stat_busy "Starting network profile: $1"
# Re-read the profile (stop_profile might have overwritten our settings)
unset DESCRIPTION INTERFACE IFOPTS IWOPTS WIFI_INTERFACE WIFI_WAIT
unset GATEWAY HOSTNAME DOMAIN DNS1 DNS2
. $PROFILE_DIR/$1
# Configure wireless settings, if necessary
[ "$WIFI_INTERFACE" ] || WIFI_INTERFACE=$INTERFACE
if [ "$IWOPTS" ]; then
iwconfig $WIFI_INTERFACE $IWOPTS
[ $? -ne 0 ] && stat_fail && return
[ "$WIFI_WAIT" ] && sleep $WIFI_WAIT
fi
if [ "$IFOPTS" = "dhcp" -o "$IFOPTS" = "DHCP" ]; then
# remove the .pid file if it exists
rm -f /etc/dhcpc/dhcpcd-${INTERFACE}.{pid,cache} >/dev/null 2>&1
dhcpcd $DHCPCD_ARGS $INTERFACE
[ $? -ne 0 ] && stat_fail && return
else
# bring up the interface
ifconfig $INTERFACE $IFOPTS up
[ $? -ne 0 ] && stat_fail && return
# bring up the default route (gateway)
if [ "$GATEWAY" ]; then
route add default gw $GATEWAY
[ $? -ne 0 ] && stat_fail && return
fi
fi
# set the hostname
if [ "$HOSTNAME" ]; then
hostname $HOSTNAME
[ $? -ne 0 ] && stat_fail && return
fi
# Generate a new resolv.conf
if [ "$DNS1" ]; then
: >/etc/resolv.conf
[ $? -ne 0 ] && stat_fail && return
[ "$DOMAIN" ] && echo "domain $DOMAIN" >>/etc/resolv.conf
[ "$DNS1" ] && echo "nameserver $DNS1" >>/etc/resolv.conf
[ "$DNS2" ] && echo "nameserver $DNS2" >>/etc/resolv.conf
fi
# Save the info in /var/run so we can shut it down later
mkdir -p $STATE_DIR
cp $PROFILE_DIR/$1 $STATE_DIR/$INTERFACE
stat_done
}
menu()
{
if [ "`ls $PROFILE_DIR 2>/dev/null | grep -v ^template$`" = "" -o ! -d $PROFILE_DIR ]; then
echo "No profiles found. Add profiles in $PROFILE_DIR"
return
fi
# scan all profiles
unset profiles
DEFAULT=
i=0
for prof in `ls $PROFILE_DIR`; do
# ignore the template
[ "$prof" = "template" ] && continue
NAME=$prof
# if there's a profile called "main", use that as default
[ "$NAME" = "main" ] && DEFAULT=$NAME
unset DESCRIPTION
. $PROFILE_DIR/$NAME
if [ "$DESCRIPTION" ]; then
profiles[$i]=$NAME
i=$((i+1))
profiles[$i]=$DESCRIPTION
i=$((i+1))
fi
done
if [ ${#profiles} -eq 0 ]; then
echo "No profiles were found in $PROFILE_DIR"
return
fi
# if no default yet, use the first entry
[ "$DEFAULT" = "" ] && DEFAULT=${profiles[0]}
ANSWER=`mktemp`
if [ "$TIMEOUT" != "" ]; then
dialog \
--timeout $TIMEOUT \
--default-item $DEFAULT \
--menu "Select the network profile you wish to use\n\n (timeout in $TIMEOUT seconds)" \
13 50 6 \
"${profiles[@]}" 2>$ANSWER
ret=$?
else
dialog \
--default-item $DEFAULT \
--menu "Select the network profile you wish to use" \
13 50 6 \
"${profiles[@]}" 2>$ANSWER
ret=$?
fi
case $ret in
1) ;; # cancel - do nothing
255) start_profile $DEFAULT ;; # timeout - use default
0) start_profile `cat $ANSWER` ;; # user selection
# abnormal
*) echo "abnormal ret code from dialog: $ret" ;;
esac
rm $ANSWER
}
#
# Begin
#
if [ "`id -u`" != "0" ]; then
echo "This script should be run as root."
exit 1
fi
# Parse command line
MODE="profile"
CHECK=0
PROFILE=
IFACE=
TIMEOUT=
while [ $# -ne 0 ]; do
case $1 in
--version) MODE="ver" ;;
--help) MODE="usage" ;;
--menu) MODE="menu" ;;
--stopall) MODE="stopall" ;;
--stop) MODE="stop"
shift
IFACE=$1 ;;
--timeout) shift
TIMEOUT=$1 ;;
--*) MODE="usage" ;;
-c) CHECK=1 ;;
-*) MODE="usage" ;;
*) PROFILE=$1 ;;
esac
shift
done
if [ "$MODE" = "profile" -a "$PROFILE" = "" ]; then
MODE="usage"
fi
# Figure out what we're doing...
[ "$MODE" = "ver" ] && version
[ "$MODE" = "usage" ] && usage
[ "$MODE" = "profile" ] && start_profile $PROFILE
[ "$MODE" = "stop" ] && stop_profile $IFACE
[ "$MODE" = "stopall" ] && stop_all
[ "$MODE" = "menu" ] && menu
exit 0
# vim: set ts=2 noet:
|