Home > Scripts > Script to rotate IP of a cpanel domain

Script to rotate IP of a cpanel domain

 

 

SITUATION: One of my customers wants to change ip address of his cpanel account every 10mins. The TTL of the domain was reduced and the following script was created for this purpose. The script uses sqlite database for holding details.

 

 

SOLUTION

 

1) Create a sqlite database to hold the ipaddress and set active flag for only one ip as described below.

 

In this example I have used ip address range 192.142.12.12 to 192.142.12.18 and have set the ip 192.142.12.12 as the active one,

 


# /usr/bin/sqlite3 /root/sqlitetesting/iprounding
SQLite version 3.3.6
Enter ".help" for instructions
sqlite> create table iproundrobin(num INTEGER PRIMARY KEY AUTOINCREMENT, ipv4address VARCHAR(16) NOT NULL UNIQUE,activeflag BOOLEAN);
sqlite> insert into iproundrobin(ipv4address,activeflag) values('192.142.12.12',1);
sqlite> insert into iproundrobin(ipv4address,activeflag) values('192.142.12.13',0);
sqlite> insert into iproundrobin(ipv4address,activeflag) values('192.142.12.14',0);
sqlite> insert into iproundrobin(ipv4address,activeflag) values('192.142.12.15',0);
sqlite> insert into iproundrobin(ipv4address,activeflag) values('192.142.12.16',0);
sqlite> insert into iproundrobin(ipv4address,activeflag) values('192.142.12.17',0);
sqlite> insert into iproundrobin(ipv4address,activeflag) values('192.142.12.18',0);
sqlite> select * from iproundrobin;
1|192.142.12.12|1
2|192.142.12.13|0
3|192.142.12.14|0
4|192.142.12.15|0
5|192.142.12.16|0
6|192.142.12.17|0
7|192.142.12.18|0
sqlite> .exit
#

 

2) Schedule the following script using cron to run at a definite interval,

 

#!/bin/bash

CHANGEIP=/usr/local/cpanel/bin/setsiteip
USERNAME=username

#get the currently active ip and its position
curIP=$(/usr/bin/sqlite3 /root/sqlitetesting/iprounding "select ipv4address from iproundrobin where activeflag=1")
curPos=$(/usr/bin/sqlite3 /root/sqlitetesting/iprounding "select num from iproundrobin where activeflag=1")

#Calculate the next position
nextPos=$(/usr/bin/sqlite3 /root/sqlitetesting/iprounding "select num from iproundrobin where num > `echo -n ${curPos}` ORDER BY num limit 1")

if [ "$nextPos" != "" ];then
  # get the ip of nextPos and set it as the new ip
  newIP=$(/usr/bin/sqlite3 /root/sqlitetesting/iprounding "select ipv4address from iproundrobin where num=`echo -n ${nextPos}`")
else
  # get the ip of first pos and set it as active
  newIP=$(/usr/bin/sqlite3 /root/sqlitetesting/iprounding "select ipv4address from iproundrobin where num=(select min(num) from iproundrobin)")
fi

# set activeflag to 0 of current ip and also set activeflag to 1 of newly selected ip
# At a time value of activeFlag will be 1 for only one ipaddress
/usr/bin/sqlite3 /root/sqlitetesting/iprounding "update iproundrobin set activeflag=0 where ipv4address=\"`echo -n ${curIP}`\""
/usr/bin/sqlite3 /root/sqlitetesting/iprounding "update iproundrobin set activeflag=1 where ipv4address=\"`echo -n ${newIP}`\""

#Set site ip
$CHANGEIP -u ${USERNAME} ${newIP} 2>&1 >/dev/null

#Display the contents of table
/usr/bin/sqlite3 /root/sqlitetesting/iprounding "select * from iproundrobin;"

 

OUTPUT ( Sample run )

# ./script.sh 
1|192.142.12.12|0
2|192.142.12.13|1
3|192.142.12.14|0
4|192.142.12.15|0
5|192.142.12.16|0
6|192.142.12.17|0
7|192.142.12.18|0
# ./script.sh 
1|192.142.12.12|0
2|192.142.12.13|0
3|192.142.12.14|1
4|192.142.12.15|0
5|192.142.12.16|0
6|192.142.12.17|0
7|192.142.12.18|0
# ./script.sh 
1|192.142.12.12|0
2|192.142.12.13|0
3|192.142.12.14|0
4|192.142.12.15|1
5|192.142.12.16|0
6|192.142.12.17|0
7|192.142.12.18|0
# ./script.sh 
1|192.142.12.12|0
2|192.142.12.13|0
3|192.142.12.14|0
4|192.142.12.15|0
5|192.142.12.16|1
6|192.142.12.17|0
7|192.142.12.18|0
# ./script.sh 
1|192.142.12.12|0
2|192.142.12.13|0
3|192.142.12.14|0
4|192.142.12.15|0
5|192.142.12.16|0
6|192.142.12.17|1
7|192.142.12.18|0
# ./script.sh 
1|192.142.12.12|0
2|192.142.12.13|0
3|192.142.12.14|0
4|192.142.12.15|0
5|192.142.12.16|0
6|192.142.12.17|0
7|192.142.12.18|1
# ./script.sh 
1|192.142.12.12|1
2|192.142.12.13|0
3|192.142.12.14|0
4|192.142.12.15|0
5|192.142.12.16|0
6|192.142.12.17|0
7|192.142.12.18|0
#
Categories: Scripts
  1. December 14, 2012 at 5:10 am

    I truly found to this unique and original site recently. I was seriously grabbed with the part of assets you’ve got here. Big thumbs up for creating such fantastic website!

  2. Dianne
    March 16, 2013 at 11:37 am

    hi I am a newbie in server field, can you detailed more the guide on how to do this. Kinda nose bleeding when reading on how to do this. If possible kindly email me the step by step guide ecashwealths at yahoo dot com. I can pay your for the guide if its not free. hope to hear from you.

    • March 17, 2013 at 9:39 am

      Hi Dianne,

      I’ve send you the detailed docs. Please check it.

  3. June 18, 2013 at 1:37 pm

    Hi

    i want to rotate ip for sending mail in centos using sendmail and dovecot for good delivery ratio.

    can you send me the script and some information

    • June 19, 2013 at 5:31 am

      Hi Santosh,

      You need to use some script like this. If you need my help to write this script, please contact me on my email address rnldpj[at]gmail.com

  4. February 22, 2014 at 9:52 pm

    Hello!
    I am a new in server, can you detailed more the guide on how to do this. If possible please email me the step by step guide vinph.info at gmail dot com. I can pay your for the guide if its not free. hope to hear from you.

  5. May 7, 2014 at 9:52 pm

    looks good enough
    But i don’t have enough experience to do this . could someone explain me step by step ?

  6. August 6, 2014 at 10:44 am

    I must thank you for the efforts you have put in writing this blog.
    I’m hoping to view the same high-grade content by you
    in the future as well. In fact, your creative writing abilities has motivated me to get my own, personal blog now
    😉

  7. somnath
    August 22, 2014 at 1:30 pm

    hi,
    i am also newbie in server field
    can you send me detailed in docs
    please please

  8. Jual Tas Rajut Jogja
    December 4, 2014 at 3:26 am

    What’s Happening i’m new to this, I stumbled upon this I
    have discovered It absolutely helpful and it has aided me out loads.
    I am hoping to contribute & help other users like its aided me.
    Good job.

  1. No trackbacks yet.

Leave a comment