-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_ip.sh
More file actions
66 lines (49 loc) · 1.64 KB
/
config_ip.sh
File metadata and controls
66 lines (49 loc) · 1.64 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
#!/bin/bash
# 备份原有的interfaces文件
cp /etc/network/interfaces /etc/network/interfaces.backup
# 清理原eth0
sed -i '/auto eth0/,+4d' /etc/network/interfaces
read -p "请输入IP地址:" IP
if [[ $IP =~ ^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ ]]; then
echo "输入的IP格式合格"
else
echo "输入的IP格式不合格"
exit 1
fi
# 预设的子网掩码前两个八位组
PRESET_SUBNET="255.255."
# 提示用户输入剩余的部分
read -p "请输入子网掩码:$PRESET_SUBNET" INPUT
# 完整的子网掩码
FULL_SUBNET_MASK="$PRESET_SUBNET$INPUT"
# 正则表达式验证子网掩码格式
if [[ $FULL_SUBNET_MASK =~ ^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ ]]; then
echo "输入的子网掩码格式合格"
else
echo "输入的子网掩码格式不合格"
exit 1
fi
# 提取IP地址的前三个八位组作为默认网关的前缀
GATEWAY_PREFIX=$(echo $IP | cut -d '.' -f 1,2,3)
# 提示用户输入网关地址的最后一部分
read -p "请输入网关地址:$GATEWAY_PREFIX." GATEWAY_LAST_OCTET
# 完整的网关地址
FULL_GATEWAY="$GATEWAY_PREFIX.$GATEWAY_LAST_OCTET"
# 验证网关地址格式
if [[ $FULL_GATEWAY =~ ^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ ]]; then
echo "输入的网关格式合格"
else
echo "输入的网关格式不合格"
exit 1
fi
cat << EOF >> /etc/network/interfaces
auto eth0
iface eth0 inet static
address $IP
netmask $FULL_SUBNET_MASK
gateway $FULL_GATEWAY
EOF
echo "配置网卡信息为"
cat /etc/network/interfaces
echo
service networking restart