Introduction

This script automatically rotates IP addresses for a cPanel account at regular intervals (e.g., every 5 or 10 minutes). It reads IPs from a file and updates the account IP sequentially.


Prerequisites

  • Root SSH access
  • cPanel server
  • Multiple IP addresses are configured on the server
  • IP list file created

Implementation

Script:

#!/bin/bash

#List of IP’s in file /root/mydomain/iplist.txt

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

#This is a cPanel user account

USERNAME=mail23

IPLIST=/root/mydomain/iplist_123mail.txt

CUR_IP=/root/mydomain/current_ip_123mail.txt

##REMOVE EMPTY LINES FROM FILE

sed -i “/^$/d” iplist_123mail.txt

#Current IP

CIP=$(cat ${CUR_IP})

#Total line count

TLN=$(wc -l ${IPLIST}|cut -d” ” -f1)

#Current line number

CLN=$(grep -n ${CIP} ${IPLIST}|cut -d: -f1)

echo -e “$CLN\n$TLN\n$CIP”

if [ $CLN -eq $TLN ]; then

CLN=1

echo ${CLN} > ${CUR_IP}

sed -n 1p ${IPLIST} > ${CUR_IP}

CIP=$(cat ${CUR_IP})

$CHANGEIP -u ${USERNAME} ${CIP} > ~/report_emailexp.txt

ret=$?

echo -e “CLN=1\nRET=${ret}”

else

CLN=$((CLN+1))

echo ${CLN} > ${CUR_IP}

sed -n “${CLN}”p ${IPLIST} > ${CUR_IP}

CIP=$(cat ${CUR_IP})

$CHANGEIP -u ${USERNAME} ${CIP} > ~/report_emailexp.txt

ret=$?

echo -e “CLN=${CLN}\nRET=${ret}”

fi

####SEND ALERT####

EMAIL_=rrrrrrrrrrrrttttttttttt@hotmail.com

if [ $ret -eq 0 ];then

exit 0

#msg=”IP CHANGED SUCCESSFULLY NEW IP=${CIP}”

#echo “NOTIFICATION FROM hostname”| mail -s “${msg}” ${EMAIL_}

else

msg=”IP CHANGED FAILURE IP TRIED=${CIP}”

content=$(cat ~/report.txt)

echo “$content” |mail -s “${msg}” ${EMAIL_}

fi

#######

##END

#######


Conclusion

By using this script on a cPanel server, you can automatically rotate IP addresses for a specific account. Ensure all IPs are listed in /root/mydomain/iplist.txt and schedule the script via cron for periodic execution.

Leave a Reply