-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregenerate.sh
executable file
·129 lines (118 loc) · 2.37 KB
/
regenerate.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
#!/bin/bash
MODE="REGENERATE_ALL"
HOSTNAME=""
REBOOT="false"
while [[ $# -gt 0 ]]; do
case $1 in
--regenerate)
MODE="REGENERATE_ALL"
shift
;;
--regenerate-id)
MODE="REGENERATE_ID"
shift
;;
--regenerate-hostname)
MODE="REGENERATE_HOSTNAME"
shift
;;
--upgrade)
MODE="UPGRADE"
shift
;;
--get-os)
MODE="GET_OS"
;;
--hostname)
HOSTNAME=$2
shift
shift
;;
-reboot)
REBOOT="true"
shift
;;
*)
echo "Invalid option $1" >&2
exit 1
;;
esac
done
function get_os {
case "$OSTYPE" in
linux*) echo "linux" ;;
darwin*) echo "mac" ;;
win*) echo "windows" ;;
msys*) echo "windows" ;;
cygwin*) echo "windows" ;;
bsd*) echo "bsd" ;;
solaris*) echo "solaris" ;;
*) echo "unknown" ;;
esac
}
function upgrade_system {
echo "Upgrading the system"
apt-get update
apt-get upgrade -y
echo "System has been upgraded"
}
function generate_random_hostname {
echo "ubuntu-$(
head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13
echo ''
)"
}
function renew_id {
echo "Renewing the Machine ID"
if [ -f /etc/machine-id ]; then
rm /etc/machine-id
systemd-machine-id-setup
echo "Machine ID has been renewed to $(cat /etc/machine-id)"
fi
}
function renew_hostname {
echo "Renewing the hostname"
if [ -f /etc/hostname ]; then
echo "$1" >/etc/hostname
hostnamectl set-hostname "$1"
echo "Hostname has been renewed to $(hostname)"
fi
}
# Check if the script is running as root (sudo), if not, exit
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
OS=$(get_os)
if [ "$OS" != "linux" ]; then
echo "This script is only compatible with Linux, exiting"
exit 1
fi
if [ "$MODE" == "REGENERATE_ALL" ]; then
renew_id
if [ -z "$HOSTNAME" ]; then
renew_hostname "$(generate_random_hostname)"
else
renew_hostname "$HOSTNAME"
fi
if [ "$REBOOT" == "true" ]; then
echo "Rebooting the system"
reboot
fi
elif [ "$MODE" == "REGENERATE_ID" ]; then
renew_id
if [ "$REBOOT" == "true" ]; then
echo "Rebooting the system"
reboot
fi
elif [ "$MODE" == "REGENERATE_HOSTNAME" ]; then
if [ -z "$HOSTNAME" ]; then
renew_hostname "$(generate_random_hostname)"
else
renew_hostname "$HOSTNAME"
fi
elif [ "$MODE" == "UPGRADE" ]; then
upgrade_system
elif [ "$MODE" == "GET_OS" ]; then
get_os
fi