Sunday, 18 September 2011

IP changing script

Hi,

Consider a scenario, if you have n number of IP address in your server and you want to switch the IP address for every 10 or 5 minutes. You can use the following script to achieve this. We have tested this script in cpanel servers. This

#!/bin/bash

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

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

#This is 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

#######


Note that you have to add all the IP address in /root/mydomain/iplist.txt.

No comments: