Multi ip: Add a shared ipv6 to all users at once

Djunity

Verified User
Joined
Mar 9, 2008
Messages
243
Location
Holland
NOTE !!!
As of directadmin version 1.43 this script isnt needed any more becouse the abillity is build in to directadmin and covers all users all resellers and ssl accounts
http://www.directadmin.com/features.php?id=1377

I leave the post online for learning and idea's for other users



As we are currently adding ipv6 to or live systems and servers contain at least 1000 domains a server it was not really handy to add every thing by hand and da it self does not really has a function cq script for it.

After searching the forum i fond 1 post that came in handy but not yet fully what i needed so i used this script as basic and changed it to my needs.

So credits for the base script go to Icehosting forum post http://www.directadmin.com/forum/showthread.php?t=40405&highlight=mass+add+ip+to+account

Ok i created the script and added it to:
cd /usr/local/directadmin/script/custom/
and called it "add_ipv6_to_user.sh"

after creating the script chmod the file to 755
chmod 755 add_ipv6_to_user.sh

The script: (off course change YOUR IPV6 ADDRESS THAT YOU WANT TO ADD to the ip address you want to add!)
Code:
#Set the IPV6 IP here
IP="[COLOR="Red"]YOUR IPV6 ADDRESS THAT YOU WANT TO ADD[/COLOR]"
COUNTER="0"
#Set the server shared ipv4 ip address here
IP4="[COLOR="Red"]YOUR SERVER SHARED IPV4 HERE[/COLOR]"

#Does the IPV6 address exist?
COUNT_IP="`grep -c ${IP} /usr/local/directadmin/data/admin/ip.list`"

if [ ${COUNT_IP} -eq 0 ]; then
   echo "IP ${IP} does not exist. Exiting..."
   exit 1;
fi

#Does the IPV4 address exist?
COUNT_IP4="`grep -c ${IP4} /usr/local/directadmin/data/admin/ip.list`"

if [ ${COUNT_IP4} -eq 0 ]; then
   echo "IP ${IP4} does not exist. Exiting..."
   exit 1;
fi

for user in `ls /usr/local/directadmin/data/users`; do
{

   COUNT_IP4_USER="`grep -c ${IP4} /usr/local/directadmin/data/users/$user/user_ip.list`"
   
   if [ ${COUNT_IP4_USER} -eq 1 ]; then
   
	   COUNT_IP_USER="`grep -c ${IP} /usr/local/directadmin/data/users/$user/user_ip.list`"
	     
	   if [ ${COUNT_IP_USER} -eq 0 ]; then
	   		  COUNTER="1"
	   
		  	  echo "Adding ip to user_ip.list user $user"
		 	  echo "${IP}" >> /usr/local/directadmin/data/users/$user/user_ip.list
	
			# added to update user amount using this ip in the ip config file
			  x="`grep value /usr/local/directadmin/data/admin/ips/${IP}`"
			  xx=$(echo $x|sed 's/value=//g')
			  let "val = $xx + 1"
			  replacement=s/${x}/value=${val}/
			  sed -i $replacement /usr/local/directadmin/data/admin/ips/${IP}
			# end update ip config file
	   fi
	   if [ ${COUNT_IP_USER} -eq 0 ]; then
		 for domain in `cat /usr/local/directadmin/data/users/$user/domains.list`; do
		   {
			if [ -e /usr/local/directadmin/data/users/$user/domains/$domain.ip_list ]; then
			
				if [ ${COUNTER} -eq 0 ]; then
					COUNTER="1"
				fi
				COUNT_IP_DOMAIN="`grep -c ${IP} /usr/local/directadmin/data/users/$user/domains/$domain.ip_list`"
				if [ ${COUNT_IP_USER} -eq 0 ]; then
			
				   echo "Adding ip to domain.ip_list"
		     	  	   echo "${IP}" >> /usr/local/directadmin/data/users/$user/domains/$domain.ip_list
		       	fi
			
			COUNT_IP_DNS="`grep -c ${IP} /var/named/$domain.db`"
			if [ ${COUNT_IP_DNS} -eq 0 ]; then
				
				if [ ${COUNTER} -eq 0 ]; then
					COUNTER="1"
				fi
				
				echo "Adding ip to dns zone"
			    echo "$domain. IN AAAA ${IP}" >> /var/named/$domain.db
                echo "www IN AAAA ${IP}" >> /var/named/$domain.db
                echo "ftp IN AAAA ${IP}" >> /var/named/$domain.db

                # Check if domain uses local mail server
                 GREP_DOMAIN="`grep -c $domain /etc/virtual/domains`"
                 if [ ${GREP_DOMAIN} -eq 1 ]; then
                   echo "mail IN AAAA ${IP}" >> /var/named/$domain.db
                   echo "pop IN AAAA ${IP}" >> /var/named/$domain.db
                   echo "smtp IN AAAA ${IP}" >> /var/named/$domain.db
                 fi
                 echo "action=rewrite&value=named&domain=$domain" >> /usr/local/directadmin/data/task.queue
			fi
		     fi
		   }
		   done;
	    fi
	fi
}
done;

if [ ${COUNTER} -eq 1 ]; then
 echo "action=rewrite&value=httpd" >> /usr/local/directadmin/data/task.queue
fi
exit 0;

After the script is created you can run it with a cronjob for example:
15 * * * * /usr/local/directadmin/script/custom/add_ipv6_to_user.sh >/dev/null 2>&1

Im not that great in bash scripts so iff you feel that i made any mistake or know a better way or if i forgot something feel free to correct me.

Update 10-12-12

I changed the code so it only works for all users of the main admin account.
Changed the code for adding the changes to the dns when using multi cluster to export the dns changed to the other dns servers it only updates the dns of the domain that is changed instead of all zone of the server.
 
Last edited:
Good working script, I've made a few changes in the script..

If you have an client with a owned IPv4 (lets say for SSL certificate) and add an ipv6 address.
the ipv6 address will resolve as shared ip. and not the the clients website

http://[your_ipv6_address] gives an 'This IP is being shared among many domains"
instead off the clients website.

solution:
after running the script
edit /usr/local/directadmin/data/admin/ips/{your ipv6 added ipaddress}
and make changes like below, and offcourse use the right values for reseller en username

Code:
reseller=admin 
status=owned
value=username

after that run:
echo "action=rewrite&value=ips" >> /usr/local/directadmin/data/task.queue

this would rewrite the /etc/httpd/conf/ips.conf
and removes the virtualhosts section, so that the domain isn't shared among other domains.
but now succesfully resolves to the clients website on both ipv4 and ipv6 addresses.

also another little problem fix, script is using this command.

Code:
# Check if domain uses local mail server
                 GREP_DOMAIN="`grep -c $domain /etc/virtual/domains`"

which if the script finds the domain inside /etc/virtual/domains
would result with value 1 and would at the mail records to dns

but what if there are more then one domainnames values inside /etc/virtual/domains let say:

example.com
myexample.com

the script would result in a value of 2 possible domains for a grep like "example.com"
change the line to this would (I believe solve the problem, notice the ^ infront of ^$domain)

Code:
# Check if domain uses local mail server
                 GREP_DOMAIN="`grep -c [B]^[/B]$domain /etc/virtual/domains`"

this makes the $domain uniq. and the returning value would be 1 instead of 2.

Thanks to icehosting ;-) and Djunity for the scripts..
 
Back
Top