forked from steamsv/dns-change
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns-change.sh
137 lines (135 loc) · 4.77 KB
/
dns-change.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
#!/bin/bash
if [ $1 != 'restore' ];then
DNS1=$1
DNS2=$2
fi
function Get_OSName(){
if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
DISTRO='CentOS'
elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
DISTRO='Debian'
elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
DISTRO='Ubuntu'
else
DISTRO='unknow'
fi
if [ $DISTRO != 'unknow' ]; then
echo -e '检测到您的系统为: '$DISTRO''
else
echo -e '不支持的操作系统,请更换为 CentOS / Debian / Ubuntu 后重试。'
exit 1;
fi
}
function get_char(){
SAVEDSTTY=`stty -g`
stty -echo
stty cbreak
dd if=/dev/tty bs=1 count=1 2> /dev/null
stty -raw
stty echo
stty $SAVEDSTTY
}
function Welcome(){
echo -e '正在检测您的操作系统...'
Get_OSName
echo -e '您确定要使用下面的DNS地址吗?'
echo -e '主DNS: '$DNS1''
if [ "$DNS2" != '' ]; then
echo -e '备DNS: '$DNS2''
fi
echo
echo -e '请按任意键继续,如有配置错误请使用 Ctrl+C 退出。'
char=`get_char`
}
function ChangeDNS(){
if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
echo
echo -e '正在备份当前DNS配置文件...'
cp /etc/resolv.conf /etc/resolv.conf.backup
echo
echo -e '备份完成,正在修改DNS配置文件...'
if [ `cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'` == 7 ]; then
sed -i '/\[main\]/a dns=none' /etc/NetworkManager/NetworkManager.conf
systemctl restart NetworkManager.service
fi
echo -e 'nameserver '$DNS1'' > /etc/resolv.conf
if [ "$DNS2" != '' ]; then
echo -e 'nameserver '$DNS2'' >> /etc/resolv.conf
fi
echo
echo -e 'DNS配置文件修改完成。'
elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
echo
echo -e '正在备份当前DNS配置文件...'
cp /etc/resolv.conf /etc/resolv.conf.backup
echo
echo -e '备份完成,正在修改DNS配置文件...'
echo -e 'nameserver '$DNS1'' > /etc/resolv.conf
if [ "$DNS2" != '' ]; then
echo -e 'nameserver '$DNS2'' >> /etc/resolv.conf
fi
echo
echo -e 'DNS配置文件修改完成。'
elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
echo
echo -e '正在修改DNS配置文件...'
if [ `cat /etc/issue|awk '{print $2}'|awk -F'.' '{print $1}'` -le 17 ]; then
echo -e 'nameserver '$DNS1'' > /etc/resolvconf/resolv.conf.d/base
if [ "$DNS2" != '' ]; then
echo -e 'nameserver '$DNS2'' >> /etc/resolvconf/resolv.conf.d/base
fi
resolvconf -u
else
echo -e 'nameserver '$DNS1'' >> /etc/systemd/resolved.conf
if [ "$DNS2" != '' ]; then
echo -e 'nameserver '$DNS2'' >> /etc/systemd/resolved.conf
fi
systemctl restart systemd-resolved.service
fi
echo
echo -e 'DNS配置文件修改完成。'
fi
echo
echo -e '感谢您的使用, 如果您想恢复备份,请在执行脚本文件时使用参数 restore 。'
}
function RestoreDNS(){
if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
echo -e '正在恢复默认DNS配置文件...'
rm -rf /etc/resolv.conf
mv /etc/resolv.conf.backup /etc/resolv.conf
if [ `cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'` == 7 ]; then
sed -i 's/dns=none//g' /etc/NetworkManager/NetworkManager.conf
systemctl restart NetworkManager.service
fi
echo
echo -e 'DNS配置文件恢复完成。'
elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
echo -e '正在恢复默认DNS配置文件...'
rm -rf /etc/resolv.conf
mv /etc/resolv.conf.backup /etc/resolv.conf
echo
echo -e 'DNS配置文件恢复完成。'
elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
echo -e '正在恢复默认DNS配置文件...'
if [ `cat /etc/issue|awk '{print $2}'|awk -F'.' '{print $1}'` -le 17 ]; then
echo -e '' > /etc/resolvconf/resolv.conf.d/base
resolvconf -u
else
sed -i '/nameserver/d' /etc/systemd/resolved.conf
systemctl restart systemd-resolved.service
fi
echo
echo -e 'DNS配置文件恢复完成。'
fi
}
function addDNS(){
Welcome
ChangeDNS
}
if [ $1 != 'restore' ];then
addDNS
elif [ $1 == 'restore' ];then
RestoreDNS
else
echo '用法错误!'
fi