1st commit

This commit is contained in:
2024-04-06 08:52:36 +02:00
commit 765c50f2fc
5 changed files with 303 additions and 0 deletions

128
sync_pihole_lan.sh Executable file
View File

@@ -0,0 +1,128 @@
#!/usr/bin/env bash
italic="\033[3m"
underline="\033[4m"
ita_under="\033[3;4m"
bgd="\033[1;4;31m"
red="\033[1;31m"
bold="\033[1m"
bold_ita="\033[1;3m"
box="\033[1;41m"
redbold="\033[1;31m"
redbox="\033[1;41m"
green="\033[0;32m"
reset="\033[0m"
echo -e "${bold}Backup ${italic}lan.list...${reset}"
ssh -p51322 root@192.168.2.116 "gzip -c cat /etc/pihole/lan.list" > lan.list.1.gz
echo "--"
ssh -p51522 root@192.168.2.216 "cat /etc/pihole/lan.list" > lan.list.2.gz
cp lan.list.bak /tmp/lan.list
bzip2 /tmp/lan.list # lan.list.bz2
mv /tmp/lan.list.bz2 .
cp lan.list lan.list.bak
nb_dynhost=$(awk 'END { print NR }' lan.list)
echo "$nb_dynhost dynhost found !"
# nano /etc/pihole/lan.list
#ssh -p51322 root@192.168.2.116 "cat /etc/pihole/lan.list" > /Users/bruno/Documents/Scripts/pihole/lan_1.list
#ssh -p51522 root@192.168.2.216 "cat /etc/pihole/lan.list" > /Users/bruno/Documents/Scripts/pihole/lan_2.list
# Zones DNS -> photos-nas.ovh -> Historique des zones
absent=()
not_required=("drive" "files" "gitea" "home-assistant" "homebridge" "portainer" "wg" "yatch")
not_used=("ds916" "musiiic" "tunes")
dynhost_list=$(cat photos-nas.ovh_dns_data.txt | grep "60 IN A" | sed '1d')
while IFS= read -r line; do
dynhost=$(echo "${line}" | awk '{print $1}')
grep -q $dynhost lan.list
if [ $? != 0 ]; then
if [[ " ${not_required[*]} " != *"$dynhost"* ]] && [[ " ${not_used[*]} " != *"$dynhost"* ]]; then
absent+=(${dynhost})
fi
fi
done <<< "$dynhost_list"
echo
echo -e "${bold}Update ${italic}lan.list...${reset}"
echo -e "${green}Not required: ${not_required[@]}${reset}"
#echo
echo -e "${italic}Not used: ${not_used[@]}${reset}"
#echo
echo -e "${red}Absent: ${absent[@]}${reset}"
#echo
if [ ${#absent[@]} -ge 1 ]; then
[ -f temp_file.list ] && rm temp_file.list
for val in ${absent[@]}
do
echo -e "192.168.2.57 ${val}.photos-nas.ovh ${val}" >> temp_file.list
done
cat temp_file.list >> lan.list
fi
cat lan.list | sort -k2 > lan.list.sorted
mv lan.list.sorted lan.list
echo
echo -e "${red}Display ${italic}lan.list...${reset}"
cat lan.list
nb_dynhost=$(awk 'END { print NR }' lan.list)
echo "$nb_dynhost dynhost !"
[ ${#absent[@]} -eq 0 ] && echo -e "\n${green}No new dynhost !${reset}"
a=$(echo -e "Do you want to export ${italic}lan.list${reset} to ${bold}pihole1${reset} and ${bold}pihole2${reset} ? (y/n)")
read -p "$a" choice
if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then
#curl -Is https://google.com | head -1 | grep 200 1>/dev/null
#curl -Is https://1.1.1.1 | head -1 | grep 302 1>/dev/null
cat < /dev/null > /dev/tcp/1.1.1.1/53
if [[ $? -ne 0 ]]; then
echo -e "\n${red}No Internet connection !${reset}"
echo -e "Exit !"
exit 1
fi
#echo "Export to pihole1..."
#cat lan.list | ssh -p51322 root@192.168.2.116 'cat > /etc/pihole/lan.list && pihole restartdns'
#[ $? != 0 ] && echo -e "${red}Error during transfer to pihole1 (192.168.2.116)${reset}" || echo -e "${green}Ok${reset}"
echo "Export to pihole1..."
cat lan.list | ssh -p51322 root@192.168.2.116 'cat > /etc/pihole/lan.list'
[ $? != 0 ] && echo -e "${red}Error during transfer to pihole1 (192.168.2.116)${reset}" || echo -e "${green}Ok${reset}"
echo "Restarting dns on pihole1..."
ssh -p51322 root@192.168.2.116 'pihole restartdns'
[ $? != 0 ] && echo -e "${red}Error during restarting dns on pihole1 (192.168.2.116)${reset}" || echo -e "${green}Ok${reset}"
echo "Export to pihole2..."
cat lan.list | ssh -p51522 root@192.168.2.216 'cat > /etc/pihole/lan.list'
[ $? != 0 ] && echo -e "${red}Error during transfer to pihole2 (192.168.2.216)${reset}" || echo -e "${green}Ok${reset}"
echo "Restarting dns on pihole2..."
ssh -p51522 root@192.168.2.216 'pihole restartdns'
[ $? != 0 ] && echo -e "${red}Error during restarting dns on pihole2 (192.168.2.216)${reset}" || echo -e "${green}Ok${reset}"
fi
[ -f temp_file.list ] && rm temp_file.list
[ -f lan.list.sorted ] && rm lan.list.sorted
#cat ~/Documents/Scripts/pihole/lan.list
# dockge syno-dashboard yacy