So, main purpose of this is so a QA guy can run this script to update /etc/hosts on one of the qa-test VMs to ensure they're hitting the correct cluster of hosts.. I grabbed most of this from two stackexchange answers:
The /etc/hosts stuff:
https://unix.stackexchange.com/questions/60549/etc-hosts-file-refer-to-another-configuration-file
The part listing out files to use to insert:
https://askubuntu.com/questions/682095/create-bash-menu-based-on-file-list-map-files-to-numbers
Currently, the script looks in /etc/hosts.d/ and creates a list of available files to insert into /etc/hosts and replace the old content between two markers..
example of one of the hosts.d files - this one would be called 'westus-prod'
It works great!
However, I would instead rather have the user pick from a list of options that are contained in the script itself instead of maintaining all the different /etc/hosts.d files..
I probably just need to think harder here, i'm sure something like this would work, but i'm blanking on listing the choices and inserting them into the /etc/hosts file part.. :
Here's the script
The /etc/hosts stuff:
https://unix.stackexchange.com/questions/60549/etc-hosts-file-refer-to-another-configuration-file
The part listing out files to use to insert:
https://askubuntu.com/questions/682095/create-bash-menu-based-on-file-list-map-files-to-numbers
Currently, the script looks in /etc/hosts.d/ and creates a list of available files to insert into /etc/hosts and replace the old content between two markers..
example of one of the hosts.d files - this one would be called 'westus-prod'
Code:
111.222.333.444 app1.domain.com # westus-prod
111.222.333.444 app2.domain.com # westus-prod
It works great!
However, I would instead rather have the user pick from a list of options that are contained in the script itself instead of maintaining all the different /etc/hosts.d files..
I probably just need to think harder here, i'm sure something like this would work, but i'm blanking on listing the choices and inserting them into the /etc/hosts file part.. :
Code:
westus-prod=(
111.222.333.444 app1.domain.com #westus-prod
111.222.333.444 app2.domain.com #westus-prod
);
centralus-prod=(
222.111.222.333 app1.domain.com #centralus-prod
222.111.222.333 app2.domain.com #centralus-prod
);
Here's the script
Code:
#!/bin/bash
files=( /etc/hosts.d/*)
shopt -s extglob
string="@(${files[0]}"
for((i=1;i<${#files[@]};i++))
do
string+="|${files[$i]}"
done
string+=")"
select file in "${files[@]}" "quit"
do
case $file in
$string)
sudo cp /etc/hosts "/etc/hosts.bak.$(date +%Y%m%d%H%M%S)" && \
(
sed -n '1,/^# CUSTOM BEGIN/{/^# CUSTOM BEGIN/!p;}' /etc/hosts; \
echo "# CUSTOM BEGIN"; \
cat "$file"; \
echo "# CUSTOM END"; \
sed -n '/^# CUSTOM END/,${/^# CUSTOM END/!p;}' /etc/hosts; \
) | \
sudo tee /etc/hosts.new | \
sed -n '/^# CUSTOM BEGIN/,/^# CUSTOM END/p' && \
sudo mv /etc/hosts.new /etc/hosts
break;
;;
"quit")
exit;;
*)
file=""
echo "Please choose a number from 1 to $((${#files[@]}+1))";;
esac
done
Last edited: