-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy patharmbian-ipset.sh
More file actions
218 lines (188 loc) · 6.15 KB
/
armbian-ipset.sh
File metadata and controls
218 lines (188 loc) · 6.15 KB
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
#!/bin/bash
# 检查是否为root用户
if [ "$(id -u)" -ne 0 ]; then
echo "请使用root权限运行此脚本"
exit 1
fi
# 获取当前的netplan配置文件
config_file=$(find /etc/netplan -name "*.yaml" | head -1)
# 如果没有找到配置文件,则创建一个新的
if [ -z "$config_file" ]; then
config_file="/etc/netplan/01-netcfg.yaml"
printf "network:\n version: 2\n renderer: networkd\n" > "$config_file"
fi
# 设置配置文件权限,确保安全
chmod 600 "$config_file"
# 验证IP地址格式的函数
is_valid_ip() {
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
# 验证DNS地址列表格式的函数
is_valid_dns_list() {
local dns_list=$1
local stat=0
for ip in $dns_list; do
if ! is_valid_ip "$ip"; then
stat=1
break
fi
done
return $stat
}
# 自动检测可用网卡并让用户选择
interfaces=($(ip -o link show | awk -F': ' '$2 != "lo" && $2 !~ /^(veth|docker|br|bond)/ {print $2}'))
if [ ${#interfaces[@]} -eq 0 ]; then
echo "未找到除lo外的有效物理网卡,脚本退出。"
exit 1
fi
echo "检测到以下可用网卡:"
for i in "${!interfaces[@]}"; do
printf "%d) %s\n" "$((i+1))" "${interfaces[$i]}"
done
default_choice=""
for i in "${!interfaces[@]}"; do
if [ "${interfaces[$i]}" == "eth0" ]; then
default_choice=$((i+1))
break
fi
done
while true; do
read -p "请输入要配置的网卡编号(默认: $default_choice): " choice
if [ -z "$choice" ] && [ -n "$default_choice" ]; then
choice=$default_choice
fi
if [[ "$choice" =~ ^[0-9]+$ ]] && [ "$choice" -ge 1 ] && [ "$choice" -le ${#interfaces[@]} ]; then
interface=${interfaces[$((choice-1))]}
break
else
echo "无效的选项,请重新输入。"
fi
done
echo "您已选择网卡: $interface"
# 获取当前的DHCP配置
current_ip=$(ip -4 addr show dev "$interface" | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | head -1)
current_gateway=$(ip route show default | grep -oP 'via\s\K\d+(\.\d+){3}')
# 获取当前的DNS配置作为默认值
current_dns=$(grep nameserver /etc/resolv.conf | awk '{print $2}' | tr '\n' ' ')
default_dns=${current_dns:-"223.6.6.6 114.114.114.114"}
# 选择网络配置类型
echo "请选择网络配置类型:"
echo "1. 使用DHCP自动获取IP地址"
echo "2. 设置固定IP地址"
read -p "请输入选项 (1-2): " config_type
case $config_type in
1)
# 创建备份
cp "$config_file" "${config_file}.bak"
chmod 600 "${config_file}.bak"
# 配置DHCP
cat > "$config_file" << EOF
network:
version: 2
renderer: networkd
ethernets:
$interface:
dhcp4: true
EOF
echo "已配置$interface使用DHCP获取IP地址"
;;
2)
# 提示用户输入IP地址并验证
while true; do
read -p "请输入IP地址(默认使用当前DHCP获取的地址: $current_ip): " ip_address
ip_address=${ip_address:-$current_ip}
if is_valid_ip "$ip_address"; then
break
else
echo "无效的IP地址格式,请重新输入。"
fi
done
# 提示用户输入子网掩码
read -p "请输入子网掩码(默认: 255.255.255.0): " netmask
netmask=${netmask:-255.255.255.0}
# 计算CIDR表示法
calculate_cidr() {
local mask=$1
local a=$(echo "$mask" | cut -d. -f1)
local b=$(echo "$mask" | cut -d. -f2)
local c=$(echo "$mask" | cut -d. -f3)
local d=$(echo "$mask" | cut -d. -f4)
local bits=$(printf "%08d" $(bc <<< "obase=2;$a"))$(printf "%08d" $(bc <<< "obase=2;$b"))$(printf "%08d" $(bc <<< "obase=2;$c"))$(printf "%08d" $(bc <<< "obase=2;$d"))
echo $(echo "$bits" | grep -o 1 | wc -l)
}
cidr=$(calculate_cidr "$netmask")
# 提示用户输入网关地址并验证
while true; do
read -p "请输入网关地址(默认使用当前DHCP获取的网关: $current_gateway): " gateway
gateway=${gateway:-$current_gateway}
if is_valid_ip "$gateway"; then
break
else
echo "无效的网关地址格式,请重新输入。"
fi
done
# 提示用户输入DNS服务器并验证
while true; do
read -p "请输入DNS服务器(多个服务器请用空格分隔,默认: $default_dns): " dns
dns=${dns:-$default_dns}
if is_valid_dns_list "$dns"; then
break
else
echo "无效的DNS地址格式,请重新输入。"
fi
done
# 创建备份
cp "$config_file" "${config_file}.bak"
chmod 600 "${config_file}.bak"
# 更新配置文件,使用 routes 语法替代 gateway4
cat > "$config_file" << EOF
network:
version: 2
renderer: networkd
ethernets:
$interface:
dhcp4: false
addresses: [$ip_address/$cidr]
routes:
- to: default
via: $gateway
nameservers:
addresses: [$(echo $dns | tr ' ' ',')]
EOF
echo "已配置$interface使用固定IP地址"
;;
*)
echo "无效的选项,脚本退出"
exit 1
;;
esac
# 确保新生成的配置文件权限正确
chmod 600 "$config_file"
echo "配置文件已更新:$config_file"
echo "新配置内容:"
cat "$config_file"
# 应用配置
read -p "是否应用新的网络配置?(y/n): " apply
if [ "$apply" = "y" ] || [ "$apply" = "Y" ]; then
echo "正在应用新的网络配置..."
netplan try
if [ $? -ne 0 ]; then
echo "配置应用失败,恢复到之前的配置"
cp "${config_file}.bak" "$config_file"
netplan apply
else
echo "配置已成功应用"
fi
else
echo "配置未应用,原始配置已保留"
fi