Auto IP Changer for VestaCP

Section with additional software for Vesta
mingu2001
Posts: 2
Joined: Fri Mar 10, 2017 7:19 pm

Auto IP Changer for VestaCP

Postby mingu2001 » Tue Aug 01, 2017 2:37 am

I made this little script that auto updates the server IP of VestaCP.
This script does not need any arguments, so it can be run on cron.
Also, this only works on configurations that have the the server IP as the public IP.

Disclaimer: I am not responsible for any damages done to your server using this program. Please use in your own risks. Also, backup before using this as this script may have bugs present.

Instructions:
First, do

Code: Select all

sudo -i
or login as ROOT on your server.

Second, do

Code: Select all

touch /usr/local/vesta/bin/v-update-ip
to create the file.

Third, do

Code: Select all

nano /usr/loca/vesta/bin/v-update-ip
and paste the code below.

Code: Select all

#!/bin/bash
# info: updates server IP
# options: NONE
#
# The function updates the server IP
# Created by mingu2001

LOG=/var/log/vesta/system.log

getUID=`/usr/bin/id -u`

if [ "$getUID" != 0 ]; then
        log "Attempted without ROOT Access. Halting operation!";
        exit 0
fi

log() {
   echo -e "$1"
   echo -e "$1" >> $LOG
}

replaceIP() {
   sed -i -e "s/${oldIP}/${newIP}/g" $1
}

oldIP=$(ls /etc/nginx/conf.d/ | grep -oE '((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])')

log "Old IP: $oldIP"

newIP=$(curl -sL http://checkip.dyndns.com/ | grep -oE '((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])')

failsafeIP=$(curl -sL http://api.ipaddress.com/myip?format=txt | grep -oE '((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])')

log "New IP: $newIP"

log "Failsafe IP: $failsafeIP"

if [ $failsafeIP != $newIP ]
then
   log "Results from servers differ. Halting operation!"
   exit 0
fi

if [ $oldIP == $newIP ]
then
   log "No need to change IP!"
   exit 0
fi

replaceIP /etc/nginx/conf.d/$oldIP.conf
mv -f /etc/nginx/conf.d/$oldIP.conf /etc/nginx/conf.d/$newIP.conf

replaceIP /etc/apache2/conf.d/$oldIP.conf
mv -f /etc/apache2/conf.d/$oldIP.conf /etc/apache2/conf.d/$newIP.conf

find /home/ -name "*.conf" -not -path "/home/timeshift/*" -print | xargs sed -i "s/${oldIP}/${newIP}/g"

for i in `ls /usr/local/vesta/data/users`
do
{
    replaceIP /usr/local/vesta/data/users/$i/web.conf
    replaceIP /usr/local/vesta/data/users/$i/dns.conf
   replaceIP /home/$i/conf/web/apache2.conf
    replaceIP /home/$i/conf/web/nginx.conf
   
    for j in `ls /usr/local/vesta/data/users/$i/dns/*.conf`
   do
    {
        replaceIP $j
    }
    done

    for j in `ls /home/$i/conf/dns/*.db`
   do
    {
      replaceIP $j
    }
    done

}
done

service nginx restart
service apache2 restart

log "Changed IP from $oldIP to $newIP"


Then press Ctrl+X to end, and press y to confirm.

Lastly,

Code: Select all

chmod +x /usr/local/vesta/bin/v-update-ip
to make the program executable.

Optionally, you can head over to your vesta panel to add a cron job running

Code: Select all

sudo /usr/local/vesta/bin/v-update-ip
.

Feel free to report any bugs, and DO BACKUP BEFORE USING! Thanks!

skurudo
VestaCP Team
Posts: 7798
Joined: Fri Dec 26, 2014 2:23 pm
Location: Moscow
Contact:

Re: Auto IP Changer for VestaCP

Postby skurudo » Tue Aug 08, 2017 8:54 am

Nice!

PS: Moved to 3rd Party Software
-> DigitalOcean competition - please, support us
-> fix for phpmyadmin - nice and sweet now


Return to “3rd Party Software”



Who is online

Users browsing this forum: No registered users and 3 guests