Compare commits

...

3 Commits

Author SHA1 Message Date
09042d0eb2 API OVH
Synchro avec la zone photos-nas.ovh sur OVH via l'API:
-on récupère via l'API la list des CNAME sur la zone photos-nas.ovh => zone.photos-nas.ovh
-on ajoute ou supprime les CNAME sur la zone photos-nas.ovh
2024-08-02 10:29:18 +02:00
ed2cee734e sync_pihole_lan.sh
v 3.0
erreur sur le commit précédent
2024-07-19 11:03:58 +02:00
296e7dd0a7 sync_pihole_lan.sh v2.0
New:
-n’utilise plus la zone DNS de OVH mais subdomains.photos-nas.ovh.txt (juste une liste de NDD)
-possibilité d’éditer le lan.list avec nano avant d’exporter
2024-07-19 10:59:24 +02:00
4 changed files with 309 additions and 13 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
.env .env
.envrc .envrc
.DS_Store

View File

@@ -4,6 +4,7 @@
192.168.2.57 books.photos-nas.ovh books 192.168.2.57 books.photos-nas.ovh books
192.168.2.57 change.photos-nas.ovh changedetection 192.168.2.57 change.photos-nas.ovh changedetection
192.168.2.57 chibisafe.photos-nas.ovh chibisafe 192.168.2.57 chibisafe.photos-nas.ovh chibisafe
192.168.2.57 dcommander.photos-nas.ovh dcommander
192.168.2.57 dockge.photos-nas.ovh dockge 192.168.2.57 dockge.photos-nas.ovh dockge
192.168.2.57 docspell.photos-nas.ovh docspell 192.168.2.57 docspell.photos-nas.ovh docspell
192.168.2.57 dozzle.photos-nas.ovh dozzle 192.168.2.57 dozzle.photos-nas.ovh dozzle
@@ -13,7 +14,9 @@
192.168.2.57 gokapi.photos-nas.ovh gokapi 192.168.2.57 gokapi.photos-nas.ovh gokapi
192.168.2.57 gotify.photos-nas.ovh gotify 192.168.2.57 gotify.photos-nas.ovh gotify
192.168.2.57 igotify.photos-nas.ovh gotify-api 192.168.2.57 igotify.photos-nas.ovh gotify-api
192.168.2.57 immich.photos-nas.ovh immich
192.168.2.57 invidious.photos-nas.ovh invidious 192.168.2.57 invidious.photos-nas.ovh invidious
192.168.2.57 it-tools.photos-nas.ovh it-tools
192.168.2.57 maloja.photos-nas.ovh maloja 192.168.2.57 maloja.photos-nas.ovh maloja
192.168.2.57 maps-server.photos-nas.ovh maps-server 192.168.2.57 maps-server.photos-nas.ovh maps-server
192.168.2.57 maps.photos-nas.ovh maps 192.168.2.57 maps.photos-nas.ovh maps

View File

@@ -0,0 +1,55 @@
airdrop
asus
books
change
chibisafe
dcommander
dockge
docspell
dozzle
drive
ds916
ds923
files
ftp
git
gitea
glances
gokapi
gotify
home-assistant
homebridge
igotify
immich
invidious
it-tools
maloja
maps-server
maps
menu
musiiic
navidrome
nextcloud
notif
paperlessngx
pastebin
photos
pihole1
pihole2
pingvin
portainer
psitransfer
seafile
search
send
snippet
syno-dashboard
tautulli
test
tube
tunes
uptime
vault
wg
yacy
yatch

View File

@@ -1,5 +1,14 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Sync_pihole_lan.sh synchronise the /etc/pihole/lan.list file with your Subdomains
# v4.0 synchro avec la zone photos-nas.ovh sur OVH via l'API
# 1. liste principale des sous-domaines => subdomains.photos-nas.ovh.txt
# 2. sauvegarde des lan.list de chaque pihole => lan.list.1 et lan.list.2
# 3. lan.list à jour que l'on copie sur chaque pihole
# 4. on récupère via l'API la list des CNAME sur la zone photos-nas.ovh => zone.photos-nas.ovh
# 5. on ajoute ou supprime les CNAME sur la zone photos-nas.ovh
italic="\033[3m" italic="\033[3m"
underline="\033[4m" underline="\033[4m"
ita_under="\033[3;4m" ita_under="\033[3;4m"
@@ -18,13 +27,15 @@ reset="\033[0m"
absent=() absent=()
delete=() delete=()
domain=".photos-nas.ovh" domain=".photos-nas.ovh"
not_required=("drive" "files" "gitea" "home-assistant" "homebridge" "portainer" "wg" "yatch") not_required=("drive" "files" "gitea" "home-assistant" "homebridge" "portainer" "tunes" "wg" "www" "yatch")
not_used=("ds916" "musiiic" "tunes" "notif" "pastefy") not_used=("ds916" "musiiic" "notif")
pihole1=192.168.2.116 pihole1=192.168.2.116
pihole2=192.168.2.216 pihole2=192.168.2.216
echo -e "${greenbold}Sync_pihole_lan.sh synchronise the /etc/pihole/lan.list file with your Zones DNS ...${reset}\n" echo -e "${greenbold}Sync_pihole_lan.sh synchronise the /etc/pihole/lan.list file with your Subdomains ...${reset}\n"
: << 'COMMENTS'
cat < /dev/null > /dev/tcp/1.1.1.1/53 cat < /dev/null > /dev/tcp/1.1.1.1/53
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
@@ -33,19 +44,21 @@ if [[ $? -ne 0 ]]; then
exit 1 exit 1
fi fi
# Sauvegarde du lan.list
echo -e "${bold}Backup ${italic}lan.list...${reset}" echo -e "${bold}Backup ${italic}lan.list...${reset}"
# 1. Sauvegarde du /etc/pihole/lan.list de chaque pihole vers lan.list.1 et lan.list.2
ping -q -c1 "$pihole1" &>/dev/null && ssh -p51322 root@"$pihole1" "cat /etc/pihole/lan.list" > lan.list.1 || echo -e "\n${red}Pihole1 is unreachable !'${reset}" ping -q -c1 "$pihole1" &>/dev/null && ssh -p51322 root@"$pihole1" "cat /etc/pihole/lan.list" > lan.list.1 || echo -e "\n${red}Pihole1 is unreachable !'${reset}"
ping -q -c1 "$pihole2" &>/dev/null && ssh -p51522 root@"$pihole2" "cat /etc/pihole/lan.list" > lan.list.2 || echo -e "${red}Pihole2 is unreachable !'${reset}" ping -q -c1 "$pihole2" &>/dev/null && ssh -p51522 root@"$pihole2" "cat /etc/pihole/lan.list" > lan.list.2 || echo -e "${red}Pihole2 is unreachable !'${reset}"
# Si un lan.list.bak existe, on l'archive
if [ -f lan.list.bak ]; then if [ -f lan.list.bak ]; then
cp lan.list.bak /tmp/lan.list cp lan.list.bak /tmp/lan.list
bzip2 /tmp/lan.list # lan.list.bz2 bzip2 /tmp/lan.list # lan.list.bz2
mv /tmp/lan.list.bz2 . mv /tmp/lan.list.bz2 .
fi fi
# On backup le lan.list vers lan.list.bak (sinon lan.list.1 devient lan.list)
if [ -f lan.list ]; then if [ -f lan.list ]; then
cp lan.list lan.list.bak cp lan.list lan.list.bak
elif [ -f lan.list.1 ]; then elif [ -f lan.list.1 ]; then
@@ -57,11 +70,13 @@ else
exit exit
fi fi
COMMENTS
nb_dynhost=$(awk 'END { print NR }' lan.list) nb_dynhost=$(awk 'END { print NR }' lan.list)
echo -e "\n$nb_dynhost dynhost found in lan.list !" echo -e "\n$nb_dynhost dynhost found in lan.list !"
# Suppression des 'not_required' et 'not_used' dans le lan.list # 2. Suppression des 'not_required' et 'not_used' dans le lan.list
echo -e "${bold}\nFind extra dynhost in ${italic}lan.list...${reset}" echo -e "${bold}\nFind extra dynhost in ${italic}lan.list...${reset}"
@@ -69,7 +84,9 @@ while IFS= read -r line; do
ndd=$(echo "${line}" | awk '{print $2}' | awk -F"." '{print $1}') ndd=$(echo "${line}" | awk '{print $2}' | awk -F"." '{print $1}')
if [[ " ${not_required[*]} " =~ " $ndd " ]] || [[ " ${not_used[*]} " =~ " $ndd " ]]; then if [[ " ${not_required[*]} " =~ " $ndd " ]] || [[ " ${not_used[*]} " =~ " $ndd " ]]; then
delete+=(${ndd}) if [[ ! "$line" == *asusrouter* ]]; then
delete+=(${ndd})
fi
fi fi
done < lan.list done < lan.list
@@ -97,12 +114,12 @@ else
fi fi
# Ajout des DynHOST de OVH (Zones DNS) dans les 2 lan.list (pihole1 & pihole2) # 3. Recherche des sous-domaines manquants dans lan.list (d'après subdomains.photos-nas.ovh.txt)
# Zones DNS -> photos-nas.ovh -> Historique des zones
echo -e "${bold}\nFind missing dynhost in ${italic}lan.list...${reset}" echo -e "${bold}\nFind missing dynhost in ${italic}lan.list...${reset}"
dynhost_list=$(cat photos-nas.ovh_dns_data.txt | grep "60 IN A" | sed '1d') #dynhost_list=$(cat photos-nas.ovh_dns_data.txt | grep "60 IN A" | sed '1d')
dynhost_list=$(cat subdomains.photos-nas.ovh.txt)
while IFS= read -r line; do while IFS= read -r line; do
@@ -120,19 +137,25 @@ done <<< "$dynhost_list"
if [ ${#absent[@]} -ge 1 ]; then if [ ${#absent[@]} -ge 1 ]; then
echo -e "${bold}Update ${italic}lan.list...${reset}"
echo -e "${green}Not required: ${not_required[@]}${reset}" echo -e "${green}Not required: ${not_required[@]}${reset}"
echo -e "${italic}Not used: ${not_used[@]}${reset}" echo -e "${italic}Not used: ${not_used[@]}${reset}"
echo -e "${red}Missing: ${absent[@]}${reset}" echo -e "${red}Missing: ${absent[@]}${reset}"
# On supprime /tmp/temp_file.list
[ -f /tmp/temp_file.list ] && rm /tmp/temp_file.list [ -f /tmp/temp_file.list ] && rm /tmp/temp_file.list
# On crée un /tmp/temp_file.list avec les sous-domaines manquants
for val in ${absent[@]} for val in ${absent[@]}
do do
echo -e "192.168.2.57 ${val}.photos-nas.ovh ${val}" >> /tmp/temp_file.list echo -e "192.168.2.57 ${val}.photos-nas.ovh ${val}" >> /tmp/temp_file.list
done done
echo -e "\n${bold}Update ${italic}lan.list...${reset}"
cat /tmp/temp_file.list
# On ajoute le /tmp/temp_file.list au lan.list
cat /tmp/temp_file.list >> lan.list cat /tmp/temp_file.list >> lan.list
# On trie le lan.list
cat lan.list | sort -k2 > lan.list.sorted cat lan.list | sort -k2 > lan.list.sorted
mv lan.list.sorted lan.list mv lan.list.sorted lan.list
@@ -142,15 +165,29 @@ fi
echo echo
echo -e "${bold}Display ${italic}lan.list...${reset}" echo -e "${bold}Display ${italic}lan.list. Please verify IP and subdomains.${reset}"
cat lan.list cat lan.list
nb_dynhost=$(awk 'END { print NR }' lan.list) nb_dynhost=$(awk 'END { print NR }' lan.list)
echo -e "$nb_dynhost dynhost !\n" echo -e "$nb_dynhost dynhost !\n"
# Do you want to modify lan.list ?
a=$(echo -e "Do you want to export ${italic}lan.list${reset} to ${bold}pihole1${reset} ($pihole1) and ${bold}pihole2${reset} ($pihole2) ? (y/n)") a=$(echo -e "Do you want to edit ${italic}lan.list${reset} ? (y/n)")
read -p "$a" choice read -p "$a" choice
if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then
nano -l lan.list
fi
# 4. On exporte lan.list vers chaque /etc/pihole/lan.list (pihole1 et pihole2)
: << 'COMMENTS2'
echo "toto"
b=$(echo -e "Do you want to export ${italic}lan.list${reset} to ${bold}pihole1${reset} ($pihole1) and ${bold}pihole2${reset} ($pihole2) ? (y/n)")
read -p "$b" choice
if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then
if ping -q -c1 "$pihole1" &>/dev/null; then if ping -q -c1 "$pihole1" &>/dev/null; then
@@ -181,5 +218,205 @@ else
echo -e "OK, let's continue..." echo -e "OK, let's continue..."
fi fi
COMMENTS2
# 5. On compare la liste principale des sous-domaines subdomains.photos-nas.ovh.txt avec la zone photos-nas.ovh sur ovh (via l'API)
# On récupère la zone photos-nas.ovh depuis ovh
# a) Zones DNS -> photos-nas.ovh -> Historique des zones => photos-nas.ovh_dns_data.txt
# b) API OVH -> liste des CNAME de la zone photos-nas.ovh => zone.photos-nas.ovh
HTTP_QUERY="https://api.ovh.com/1.0/domain"
TIME=$(curl -s https://api.ovh.com/1.0/auth/time)
ZONE_FILE="zone.photos-nas.ovh"
ZONE="photos-nas.ovh"
source ovh_secrets.txt
export_zone() {
HTTP_METHOD="GET"
HTTP_BODY=""
HTTP_QUERY="https://eu.api.ovh.com/v1/domain/zone/$ZONE/export"
CLEAR_SIGN="$OVH_APP_SECRET+$OVH_CONSUMER_KEY+$HTTP_METHOD+$HTTP_QUERY+$HTTP_BODY+$TIME"
SIG='$1$'$(echo -n $CLEAR_SIGN | openssl dgst -sha1 | sed -e 's/^.* //')
curl --silent -X $HTTP_METHOD -H "Content-Type:application/json;charset=utf-8" -H "X-Ovh-Application:$OVH_APP_KEY" -H "X-Ovh-Timestamp:$TIME" -H "X-Ovh-Signature:$SIG" -H "X-Ovh-Consumer:$OVH_CONSUMER_KEY" --data "$HTTP_BODY" $HTTP_QUERY | sed 's/\\n/\n/g' | sed 's/\"//g' | grep 'CNAME'> $ZONE_FILE
#curl --silent -X $HTTP_METHOD -H "Content-Type:application/json;charset=utf-8" -H "X-Ovh-Application:$OVH_APP_KEY" -H "X-Ovh-Timestamp:$TIME" -H "X-Ovh-Signature:$SIG" -H "X-Ovh-Consumer:$OVH_CONSUMER_KEY" --data "$HTTP_BODY" $HTTP_QUERY | sed 's/\\n/\n/g' | grep 'CNAME'
# |sed 's/,//g' |sed 's/\\n/\'$'\n''/g'
# | sed 's/\\n/\n/g'
#echo $?
if [ $? == 0 ]; then
echo -e "File ${italic}$ZONE_FILE${reset} have been created from OVH $ZONE zone !"
fi
}
remove_cname() {
record=""
HTTP_METHOD="GET"
HTTP_BODY=""
HTTP_QUERY="https://eu.api.ovh.com/v1/domain/zone/$ZONE/record?fieldType=CNAME&subDomain=$1"
CLEAR_SIGN="$OVH_APP_SECRET+$OVH_CONSUMER_KEY+$HTTP_METHOD+$HTTP_QUERY+$HTTP_BODY+$TIME"
SIG='$1$'$(echo -n $CLEAR_SIGN | openssl dgst -sha1 | sed -e 's/^.* //')
record=$(curl --silent -X $HTTP_METHOD -H "Content-Type:application/json;charset=utf-8" -H "X-Ovh-Application:$OVH_APP_KEY" -H "X-Ovh-Timestamp:$TIME" -H "X-Ovh-Signature:$SIG" -H "X-Ovh-Consumer:$OVH_CONSUMER_KEY" --data "$HTTP_BODY" $HTTP_QUERY | sed 's/.//;s/.$//')
#echo $? # 0
if [ $? == 0 ] && [ -n "$record" ]; then
echo "Record number for CNAME $1 is $record !"
HTTP_METHOD="DELETE"
HTTP_BODY=""
HTTP_QUERY="https://eu.api.ovh.com/v1/domain/zone/$ZONE/record/$record"
CLEAR_SIGN="$OVH_APP_SECRET+$OVH_CONSUMER_KEY+$HTTP_METHOD+$HTTP_QUERY+$HTTP_BODY+$TIME"
SIG='$1$'$(echo -n $CLEAR_SIGN | openssl dgst -sha1 | sed -e 's/^.* //')
curl --silent -X $HTTP_METHOD -H "Content-Type:application/json;charset=utf-8" -H "X-Ovh-Application:$OVH_APP_KEY" -H "X-Ovh-Timestamp:$TIME" -H "X-Ovh-Signature:$SIG" -H "X-Ovh-Consumer:$OVH_CONSUMER_KEY" --data "$HTTP_BODY" $HTTP_QUERY
#echo $? # 0
if [ $? == 0 ]; then
echo "CNAME $1 have been successfully deleted !"
else
echo "Error while deleting CNAME $1 !"
fi
else
echo "No record number found for CNAME $1 !"
fi
}
add_cname() {
HTTP_METHOD="POST"
HTTP_BODY="{\"fieldType\": \"CNAME\",\"subDomain\": \"$1\",\"target\": \"photos-nas.ovh.\",\"ttl\": 0}"
HTTP_QUERY="https://eu.api.ovh.com/v1/domain/zone/$ZONE/record"
CLEAR_SIGN="$OVH_APP_SECRET+$OVH_CONSUMER_KEY+$HTTP_METHOD+$HTTP_QUERY+$HTTP_BODY+$TIME"
SIG='$1$'$(echo -n $CLEAR_SIGN | openssl dgst -sha1 | sed -e 's/^.* //')
curl --silent -X $HTTP_METHOD -H "Content-Type:application/json;charset=utf-8" -H "X-Ovh-Application:$OVH_APP_KEY" -H "X-Ovh-Timestamp:$TIME" -H "X-Ovh-Signature:$SIG" -H "X-Ovh-Consumer:$OVH_CONSUMER_KEY" --data "$HTTP_BODY" $HTTP_QUERY
#echo $? # null
if [ $? == 0 ]; then
echo "CNAME $1 have been successfully added !"
else
echo "Error adding CNAME $1 !"
fi
HTTP_BODY=""
HTTP_QUERY="https://eu.api.ovh.com/v1/domain/zone/$ZONE/refresh"
CLEAR_SIGN="$OVH_APP_SECRET+$OVH_CONSUMER_KEY+$HTTP_METHOD+$HTTP_QUERY+$HTTP_BODY+$TIME"
SIG='$1$'$(echo -n $CLEAR_SIGN | openssl dgst -sha1 | sed -e 's/^.* //')
curl --silent -X $HTTP_METHOD -H "Content-Type:application/json;charset=utf-8" -H "X-Ovh-Application:$OVH_APP_KEY" -H "X-Ovh-Timestamp:$TIME" -H "X-Ovh-Signature:$SIG" -H "X-Ovh-Consumer:$OVH_CONSUMER_KEY" --data "$HTTP_BODY" $HTTP_QUERY
#echo $? # 0
if [ $? == 0 ]; then
echo "Zone $ZONE have been successfully refresh !"
else
echo "Error while refreshing zone $ZONE !"
fi
}
echo -e "${bold}\nGet extra CNAME records list from zone ${italic}$ZONE${reset} (API OVH)..."
export_zone
#ovh_cname_list=$(cat photos-nas.ovh_dns_data.txt | grep "CNAME" | sed '1d')
ovh_cname_list=$(cat $ZONE_FILE)
subdomains_list=$(cat subdomains.photos-nas.ovh.txt)
extra_ovh=()
absent_ovh=()
echo -e "${bold}\nFind extra CNAME records in zone ${italic}$ZONE${reset}..."
while IFS= read -r line; do
dynhost=$(echo "${line}" | awk '{print $1}')
grep -q $dynhost subdomains.photos-nas.ovh.txt
if [ $? != 0 ]; then
if [[ ! " ${not_required[*]} " =~ " $dynhost " ]] && [[ ! " ${not_used[*]} " =~ " $dynhost " ]]; then
#echo "$dynhost présent sur ovh"
extra_ovh+=(${dynhost})
fi
fi
# 55 sur ovh
# 54 sur subdomains.photos-nas.ovh.txt
# 43 sur lan.list
done <<< "$ovh_cname_list"
if [ ${#extra_ovh[@]} -ge 1 ]; then
echo -e "${red}To delete: ${extra_ovh[@]}${reset}"
for val in ${!extra_ovh[@]}
do
value="${extra_ovh[$val]}"
b=$(echo -e "Do you want to delete ${red}$value${reset} CNAME in OVH zone ${italic}$ZONE${reset} ? (y/n)")
read -p "$b" choice
if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then
remove_cname "$value"
fi
done
#nb_dynhost=$(awk 'END { print NR }' lan.list)
#echo "$nb_dynhost dynhost found in lan.list !"
#fi
else
echo "None"
fi
echo -e "${bold}\nFind missing CNAME records in zone ${italic}$ZONE${reset}..."
while IFS= read -r line; do
dynhost=$(echo "${line}" | awk '{print $1}')
grep -q $dynhost zone.photos-nas.ovh
if [ $? != 0 ]; then
if [[ ! " ${not_required[*]} " =~ " $dynhost " ]] && [[ ! " ${not_used[*]} " =~ " $dynhost " ]]; then
#echo "$dynhost absent sur ovh"
absent_ovh+=(${dynhost})
fi
fi
done <<< "$subdomains_list"
if [ ${#absent_ovh[@]} -ge 1 ]; then
echo -e "${red}To add: ${absent_ovh[@]}${reset}"
for val in ${!absent_ovh[@]}
do
value="${absent_ovh[$val]}"
b=$(echo -e "Do you want to add ${red}$value${reset} CNAME in OVH zone ${italic}$ZONE${reset} ? (y/n)")
read -p "$b" choice
if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then
echo "$value"
add_cname "$value"
fi
done
#nb_dynhost=$(awk 'END { print NR }' lan.list)
#echo "$nb_dynhost dynhost found in lan.list !"
else
echo "None"
fi
# 6. On supprime les fichiers temporaires
[ -f /tmp/temp_file.list ] && rm /tmp/temp_file.list [ -f /tmp/temp_file.list ] && rm /tmp/temp_file.list
[ -f lan.list.sorted ] && rm lan.list.sorted [ -f lan.list.sorted ] && rm lan.list.sorted
# https://www.nas-forum.com/forum/topic/80160-multiples-dynhost-ovh/#comment-1319505612