parent
c60f767615
commit
dbb86ecace
@ -0,0 +1,97 @@
|
||||
#!/usr/bin/env sh
|
||||
#Author: Aaron Johnson
|
||||
#Comment: This script is not intended to be portable or fail-safe, and is used in my personal provisioning process.
|
||||
|
||||
name_old=$(cat /etc/hostname)
|
||||
#Below command preserved in case it is needed to add older OS support
|
||||
#ipadd_old=$(ifconfig | awk '/inet6/{next;} /127.0.0.1/{next;} /inet/{print $2;}' | grep 172.23)
|
||||
cidr_old=$(ip a | awk '/inet6/{next;} /127.0.0.1/{next;} /inet/{print $2;}' | grep 172.23)
|
||||
ipadd_old=$(echo $cidr_old | awk -F '/' '{print $1}')
|
||||
|
||||
# Get user input
|
||||
echo "Enter new host information."
|
||||
read -p "Hostname [$name_old]: " name
|
||||
read -p "IP address (CIDR format) [$cidr_old]: " cidr
|
||||
# Input sanity check
|
||||
if [ -z "$name" ]; then
|
||||
name=$name_old
|
||||
fi
|
||||
if [ -z "$cidr" ]; then
|
||||
cidr=$cidr_old
|
||||
fi
|
||||
## Validate CIDR
|
||||
n='([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
|
||||
m='([0-9]|[12][0-9]|3[012])'
|
||||
if ! echo $cidr | grep -E "^$n(\.$n){3}/$m$" &>/dev/null; then
|
||||
printf "\nCIDR input is not valid!\n"
|
||||
exit 12
|
||||
fi
|
||||
# End input sanity check
|
||||
ipadd=$(echo $cidr | awk -F '/' '{print $1}')
|
||||
echo
|
||||
|
||||
printf "Review these changes carefully!\n\n"
|
||||
printf "+,CURRENT,PENDING\nHostname:,$name_old,$name\nAddress:,$ipadd_old,$ipadd\nCIDR:,$cidr_old,$cidr" | column -s, -t
|
||||
echo
|
||||
read -p "Are you sure you want to apply the changes above? (y/N)" -n 1 -r
|
||||
if echo $REPLY | grep -Eq '^[Yy]$'; then
|
||||
printf "\n Continuing...\n\n"
|
||||
else
|
||||
printf "\n ABORT\n"
|
||||
exit 13
|
||||
fi
|
||||
|
||||
# Update /etc/hostname
|
||||
printf "Update hostname... "
|
||||
echo $name > /etc/hostname && printf "DONE\n" || exit 1
|
||||
|
||||
# Update /etc/hosts
|
||||
printf "Update /etc/hosts... "
|
||||
sed -i "/^127.0.0.1/s/.*/127.0.0.1\tlocalhost localhost.localdomain/" /etc/hosts && \
|
||||
sed -i "/^127.0.1.1/s/.*/127.0.1.1\t$(echo $name | awk -F . '{print $1}') $name/" /etc/hosts &&\
|
||||
printf "DONE\n" || exit 1
|
||||
|
||||
# Update Zabbix Agent
|
||||
printf "Update Zabbix Agent... "
|
||||
sed -i "s/${name_old}/${name}/g" /etc/zabbix/zabbix_agentd.conf && printf "DONE\n" || exit 1
|
||||
|
||||
# Update IP address
|
||||
printf "Update IP address... "
|
||||
if $(uname -r | grep -E 'el8' &>/dev/null); then
|
||||
printf "CentOS 8... "
|
||||
nmcli con mod ens18 ipv4.addresses $cidr && printf "DONE\n" || exit 1
|
||||
elif $(uname -r | grep 'arch' &>/dev/null); then
|
||||
printf "Arch Linux... "
|
||||
sed -i "s_${cidr_old}_${cidr}_g" /etc/netctl/ens18 && printf "DONE\n" || exit 1
|
||||
else
|
||||
printf "FAIL\n\nOS distribution not supported! Update IP address manually before rebooting!\n\n"
|
||||
fi
|
||||
|
||||
# Update SSH host keys
|
||||
if [ -f /etc/ssh/ssh_host_rsa_key ]; then
|
||||
printf "Found old keys. Removing... "
|
||||
rm /etc/ssh/ssh*key* && printf "DONE\n" || exit 1
|
||||
fi
|
||||
printf "Regenerating host keys... "
|
||||
ssh-keygen -A >/dev/null && printf "DONE\n" || exit 1
|
||||
|
||||
# Remove script after success
|
||||
if [ -f /root/provision_new_server.sh ]; then
|
||||
printf "Removing this script from /root... "
|
||||
unlink /root/provision_new_server.sh || exit 1
|
||||
printf "DONE\n"
|
||||
printf " (This script can still be found in /opt/prov/bin)\n"
|
||||
fi
|
||||
|
||||
printf "\nReboot to apply all changes.\n\n"
|
||||
|
||||
read -p "Would you like to reboot now? (y/N)" -n 1 -r
|
||||
if echo $REPLY | grep -Eq '^[Yy]$'; then
|
||||
printf "\n Rebooting...\n\n"
|
||||
shutdown -r now
|
||||
exit 42
|
||||
else
|
||||
printf "\nReboot has been deferred.\n"
|
||||
exit 0
|
||||
fi
|
||||
|
Loading…
Reference in new issue