-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstatic-ip.sh
236 lines (208 loc) · 8.1 KB
/
static-ip.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
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
#!/bin/bash
# Description: change dynamic to static ip for ubuntu-server setup
# Author: Amancio C.
#HIGH INTENSITY (NORMAL)
GREEN="\e[0;92m"
YELLOW="\e[0;93m"
WHITE="\e[0;97m"
#HIGH INTENSITY (BOLD)
BGREEN="\e[1;92m"
BYELLOW="\e[1;93m"
BWHITE="\e[1;97m"
#HIGH INTENSITY (UNDERLINE)
UGREEN="\e[4;92m"
UYELLOW="\e[4;93m"
UWHITE="\e[4;97m"
#RESET/END
ENDCOLOR="\e[0m"
#SHOW DATE
Now=$(date)
#CURRENT USERNAME
username=$(whoami)
## POST INSTALL WELCOME MESSAGE
echo ""
echo -e "${BYELLOW} ,_,${ENDCOLOR}
${BYELLOW}https://github.com/projects-by-ac${ENDCOLOR} ${YELLOW}(O,O)
________________________________________________( )__
" "
╔═╗╦═╗╔═╗ ╦╔═╗╔═╗╔╦╗╔═╗ ┌┐ ┬ ┬ ╔═╗╔═╗
╠═╝╠╦╝║ ║ ║║╣ ║ ║ ╚═╗ ├┴┐└┬┘ ╠═╣║
╩ ╩╚═╚═╝╚╝╚═╝╚═╝ ╩ ╚═╝ └─┘ ┴ ╩ ╩╚═╝
(@>
__{||__________________________________________________
""||${ENDCOLOR}
${YELLOW} | Author:${ENDCOLOR} Amancio C.
${YELLOW} | Description:${ENDCOLOR} Script {Dynamic to Static IP}
"
echo ""
echo -e "initializing script on ${GREEN}$Now${ENDCOLOR}"
echo ""
sleep 1
echo -e "${BYELLOW}.......................................................................${ENDCOLOR}"
echo ""
echo -e "${BYELLOW}WELCOME! INSTALLING SCRIPT FOR THE FOLLOWING USER:${ENDCOLOR}"
echo ""
echo -e "${BGREEN}$username${ENDCOLOR}"
echo ""
read -p "Do you want to proceed? (y/n)" -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
exit 1
fi
echo ""
echo -e "${BYELLOW}script will start running in${ENDCOLOR}"
sleep 1
echo -e "${BYELLOW}3...${ENDCOLOR}"
sleep 1
echo -e "${BYELLOW}2...${ENDCOLOR}"
sleep 1
echo -e "${BYELLOW}1...${ENDCOLOR}"
sleep 1
echo ""
echo -e "${BYELLOW}*/running script${ENDCOLOR}"
echo ""
sleep 1
echo -e "${BYELLOW}.......................................................................${ENDCOLOR}"
echo ""
# Find the active network interface
active_interface=$(ip -o route show to default | awk '{print $5}')
# Find all active network interfaces
active_interfaces=$(ip link show | awk '/state UP/ {print $2}' | sed 's/://g')
# Get the current IP address, netmask, and gateway
current_ipv4=$(ip addr show $active_interface | awk '/inet / {print $2}' | cut -d/ -f1)
current_ipv6=$(ip addr show $active_interface | grep "inet6" | grep -v "fe80" | grep -v "fda6" | awk '{print $2}' | cut -d/ -f1)
current_netmask=$(ip addr show $active_interface | awk '/inet / {print $2}' | cut -d/ -f2)
current_gateway=$(ip route show default | awk '/default/ {print $3}')
current_dns=$(resolvectl status | grep "DNS Servers" | awk '{for (i=3; i<=NF; i++) if ($i ~ /^[0-9.]+$/) print $i}' | tr ' ' '\n')
current_hostname=$(hostname)
# Check if DHCP is enabled for IPv4 from Netplan configuration
dhcp4=$(grep -q "dhcp4: true" /etc/netplan/*.yaml && echo "yes" || echo "no")
# Check if DHCP is enabled for IPv6 from Netplan configuration
dhcp6=$(grep -q "dhcp6: true" /etc/netplan/*.yaml && echo "yes" || echo "no")
# Show the overview of the current IP address, netmask, and gateway
echo -e "${BYELLOW}CURRENT (DEFAULT) NETWORK SETTINGS:${ENDCOLOR}"
echo ""
echo -e "${WHITE}Current Hostname:${ENDCOLOR}"
echo -e "${BGREEN}$current_hostname${ENDCOLOR}"
echo ""
echo -e "${WHITE}Active Interface:${ENDCOLOR}"
echo -e "${BGREEN}$active_interface${ENDCOLOR}"
echo ""
echo -e "${WHITE}DHCP4 Enabled:${ENDCOLOR} ${BGREEN}$dhcp4${ENDCOLOR}"
echo -e "${WHITE}DHCP6 Enabled:${ENDCOLOR} ${BGREEN}$dhcp6${ENDCOLOR}"
echo ""
echo -e "${WHITE}Current IPv4 address:${ENDCOLOR}"
echo -e "${BGREEN}$current_ipv4${ENDCOLOR}"
echo ""
echo -e "${WHITE}Current IPv6 address:${ENDCOLOR}"
echo -e "${BGREEN}$current_ipv6${ENDCOLOR}"
echo ""
echo -e "${WHITE}Current Netmask:${ENDCOLOR}"
echo -e "${BGREEN}255.255.255.0/$current_netmask${ENDCOLOR}"
echo ""
echo -e "${WHITE}Current Gateway:${ENDCOLOR}"
echo -e "${BGREEN}$current_gateway${ENDCOLOR}"
echo ""
echo -e "${WHITE}Current DNS-Servers:${ENDCOLOR}"
echo -e "${BGREEN}$current_dns${ENDCOLOR}"
echo ""
echo "-----------------------------------------------------------------------"
echo -e "${BYELLOW}if you press Enter with 'no input' it will default to the current${ENDCOLOR}"
echo -e "${BYELLOW}existing network settings and change the dynamic IP to a static IP${ENDCOLOR}"
echo -e "${BYELLOW}with DHCP4/DHCP6 Disabled and Hostname set to: '$current_hostname'${ENDCOLOR}"
echo "-----------------------------------------------------------------------"
echo ""
# Prompt the user for the new static IP address
read -p "Set Static IP address (default: $current_ipv4): " new_ip
new_ip=${new_ip:-$current_ipv4}
echo ""
# Prompt the user for the netmask
read -p "Set Netmask CIDR (default: $current_netmask): " netmask
netmask=${netmask:-$current_netmask}
echo ""
# Prompt the user for the gateway
read -p "Set Gateway (default: $current_gateway): " gateway
gateway=${gateway:-$current_gateway}
echo ""
# Prompt the user for dhcp4
read -p "Enable DHCP4? (y/n): " dhcp4
case $dhcp4 in
[Yy]) dhcp4="true";;
[Nn]) dhcp4="false";;
*) dhcp4="false";;
esac
echo ""
# Prompt the user for dhcp6
read -p "Enable DHCP6? (y/n): " dhcp6
case $dhcp6 in
[Yy]) dhcp6="true";;
[Nn]) dhcp6="false";;
*) dhcp6="false";;
esac
echo ""
# Prompt the user for dns servers
read -p "Set DNS-Servers (default: 1.1.1.1,8.8.8.8): " dns_servers
dns_servers=${dns_servers:-"1.1.1.1,8.8.8.8"}
echo ""
# Prompt the user for hostname
read -p "Set Hostname (default: $current_hostname): " hostname
hostname=${hostname:-$current_hostname}
echo ""
# Find the netplan configuration file
netplan_file=$(ls /etc/netplan/ | grep -E '^[0-9]{2}-.*\.yaml$')
# Backup the original netplan configuration
cp /etc/netplan/$netplan_file /etc/netplan/$netplan_file.bak
echo "-----------------------------------------------------------------------"
echo -e "${BYELLOW}NETPLAN CONFIGURATION FILE:${ENDCOLOR}${BGREEN} /etc/netplan/$netplan_file${ENDCOLOR}"
echo "-----------------------------------------------------------------------"
echo ""
# Create a new netplan configuration file
cat <<EOF | sudo tee /etc/netplan/$netplan_file
network:
version: 2
renderer: networkd
ethernets:
$active_interface:
dhcp4: $dhcp4
dhcp6: $dhcp6
addresses: [$new_ip/$netmask]
routes:
- to: default
via: $gateway
nameservers:
addresses: [$dns_servers]
search: [$hostname.local]
EOF
echo ""
echo "-----------------------------------------------------------------------"
echo ""
echo -e "${BYELLOW}Static IP address configured successfully${ENDCOLOR}"
echo ""
echo "-----------------------------------------------------------------------"
echo -e "${BYELLOW}after applying the netplan configuration file your current connection${ENDCOLOR}"
echo -e "${BYELLOW}will exit and you'll be able to access your new static ip address from:${ENDCOLOR}"
echo "-----------------------------------------------------------------------"
echo ""
echo -e " Hostname: ${BGREEN}$hostname.local${ENDCOLOR}"
echo -e " IP address: ${BGREEN}$new_ip/$netmask${ENDCOLOR}"
echo -e " Gateway: ${BGREEN}$gateway${ENDCOLOR}"
echo ""
echo "-----------------------------------------------------------------------"
echo ""
# Prompt the user to apply the netplan configuration
read -p "Apply the netplan configuration? (y/n): " apply_netplan
if [[ $apply_netplan == [Yy] ]]; then
# Set the hostname
sudo hostnamectl set-hostname $hostname
# Apply the netplan configuration
sudo netplan apply
echo ""
echo -e "${BYELLOW}Netplan configuration applied successfully${ENDCOLOR}"
exit 0
else
# Revert the changes made earlier
sudo mv /etc/netplan/$netplan_file.bak /etc/netplan/$netplan_file
echo ""
echo -e "${BYELLOW}Changes reverted. Netplan configuration Not applied.${ENDCOLOR}"
fi