publicip2nginx/public_ip.sh

46 lines
986 B
Bash
Raw Permalink Normal View History

2019-02-03 21:01:10 +00:00
#!/bin/bash
#Aktuelle öffentliche IP ermitteln.
public=`wget -q -O- checkip.dyndns.org | grep -Eo '\<[[:digit:]]{1,3}(\.[[:digit:]]{1,3}){3}\>'`;
#Bisher bekannte öffentliche IP auslesen.
old=`cat /opt/publicip2nginx/current.txt`;
nginx="/etc/nginx/conf.d/hw12.conf";
temp="/opt/publicip2nginx/hw12.conf";
log_time=$(/bin/date +%Y%m%d-%H%M%S);
#Prüfe nach bisheriger IP.
if [ -z "$old" ]; then
echo $log_time": Keine alte IP gefunden." >> /var/log/public2ip.log;
exit;
fi
#Prüfe nach aktueller IP.
if [ -z "$public" ]; then
echo $log_time": Keine aktuelle IP gefunden." >> /var/log/public2ip.log;
exit;
fi
#Vergleich
if [ $old = $public ]; then
exit;
else
sed /allow/s/$old/$public/g $nginx > $temp;
#Dateien verschieben
mv $nginx $nginx.".bak";
mv $temp $nginx;
service nginx reload;
#Log bei Änderung und aktuelle IP in Datei schreiben.
echo $log_time":" $old $public >> /var/log/public2ip.log;
echo $public > /opt/publicip2nginx/current.txt;
fi
exit;