-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathconfigure_usb_ethernet.sh
executable file
·172 lines (158 loc) · 5.69 KB
/
configure_usb_ethernet.sh
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
#!/bin/bash
#
# =======================================================================
#
# Copyright (C) 2018, Hisilicon Technologies Co., Ltd. All Rights Reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1 Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2 Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3 Neither the names of the copyright holders nor the names of the
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# =======================================================================
usb_ethernet=""
usb_ip=""
usb_ethernets=`dmesg | grep "renamed from usb" | sort -r | cut -d ":" -f 2| cut -c 5- | uniq`
usb_number=`dmesg | grep "renamed from usb" | sort -r | cut -d ":" -f 2| cut -c 5- | uniq | wc -l`
function check_ip_addr()
{
ip_addr=$1
echo ${ip_addr} | grep "^[0-9]\{1,3\}\.\([0-9]\{1,3\}\.\)\{2\}[0-9]\{1,3\}$" > /dev/null
if [ $? -ne 0 ]
then
return 1
fi
for num in `echo ${ip_addr} | sed "s/./ /g"`
do
if [ $num -gt 255 ] || [ $num -lt 0 ]
then
return 1
fi
done
return 0
}
function configure_usb_ethernet()
{
name=$1
address=$2
echo "
auto ${name}
iface ${name} inet static
address ${address}
netmask 255.255.255.0" >> /etc/network/interfaces
}
usage()
{
echo "*********************************************************************************************************"
echo "* Usage :"
echo "* ./configure_usb_ethernet.sh ............. ..: configure usb by default ip: 192.168.1.166"
echo "* ./configure_usb_ethernet.sh -s ip : set usb with given ip"
echo "* ./configure_usb_ethernet.sh -s usb_ethernet ip : if more than one usb name, set with usb name and ip"
echo "* ./configure_usb_ethernet.sh -h|-help : print help message"
echo "*********************************************************************************************************"
exit 1
}
function main()
{
while true
do
case "$1" in
-h | --h | -help | --help)
usage
exit 0
;;
-s)
if [ $# -eq 2 ]; then
usb_ip=$2
check_ip_addr ${usb_ip}
if [ $? -ne 0 ];then
echo "Invalid ip, please check your command."
usage
exit 1
fi
elif [ $# -ge 3 ];then
usb_ethernet=$2
usb_ip=$3
check_ip_addr ${usb_ip}
if [ $? -ne 0 ];then
echo "Invalid ip, please check your command."
usage
exit 1
fi
fi
break
;;
"")
break
;;
*)
echo "Invalid command."
usage
break
;;
esac
done
if [ ${usb_number} -eq 0 ];then
echo "No any unconfigured usb ethernet found, please check your environment."
exit 1
elif [ ${usb_number} -eq 1 ];then
if [[ ${usb_ethernet}"X" == "X" ]];then
usb_ethernet=${usb_ethernets}
fi
else
if [[ ${usb_ethernet}"X" == "X" ]];then
echo -e "Too many usb ethernets found: \n${usb_ethernets}\nPlease input usb ethernet which to configure."
usage
exit 1
fi
fi
if [[ ${usb_ip}"X" == "X" ]];then
echo "No input usb ip, use 192.168.1.166"
usb_ip="192.168.1.166"
fi
echo ${usb_ethernets} | grep ${usb_ethernet} 1>/dev/null 2>&1
if [ $? -ne 0 ];then
echo "Input usb_ethernet: ${usb_ethernet} is not found, please check your command."
exit 1
fi
grep "auto ${usb_ethernet}" /etc/network/interfaces 1>/dev/null 2>&1
if [ $? -eq 0 ];then
echo "${usb_ethernet}'s is already configured in /etc/network/interfaces, please check file for configuration."
exit 0
fi
/sbin/ifconfig | grep "${usb_ip}" 1>/dev/null 2>&1
if [ $? -eq 0 ];then
echo "Ip ${usb_ip} is already used, please input a unused usb ip."
usage
exit 1
fi
echo "Configure ${usb_ethernet} by ${usb_ip}"
configure_usb_ethernet ${usb_ethernet} ${usb_ip}
sed -i "s/managed=false/managed=true/g" /etc/NetworkManager/NetworkManager.conf
ifconfig ${usb_ethernet} down 1>/dev/null 2>&1
ifconfig ${usb_ethernet} up 1>/dev/null 2>&1
echo "Restart NetworkManager service..."
service NetworkManager restart
echo "Configure usb ip successfully."
}
main $*