How to add a range of IPv6 addresses

Panormitis

Verified User
Joined
Sep 13, 2014
Messages
31
I wanted to add a range of IPv6 addresses (approximately 2000 of them) and I noticed that I couldn't do it using the IP Manager.
I know I can add IPv4 ranges as 1.2.3.4-10 but that didn't work for IPv6 ranges, I could only add IPv6 addresses one by one.
So with a little help from http://help.directadmin.com/item.php?id=417 I figured out how to create a shell script for adding them.
I thought to post it here, in case anyone else finds it useful:
Code:
#!/bin/sh

IPPART=2345:f1d2:1234:0190:0000:0000:0000:
IPSTART=10
IPEND=2000
SHORT=false

IPDIR=/usr/local/directadmin/data/admin/ips/
IPLIST=/usr/local/directadmin/data/admin/ip.list

COUNTER=$IPSTART

while [ "$COUNTER" -le $IPEND ]
  do
    if [ "$SHORT" == true ]; then
      if [ "$COUNTER" -ge 1 -a "$COUNTER" -le 9999 ]; then IPADDRESS="$IPPART$COUNTER"; fi
    elif [ "$SHORT" == false ]; then
      if [ "$COUNTER" -ge 1 -a "$COUNTER" -le 9 ]; then IPADDRESS="${IPPART}000${COUNTER}";
      elif [ "$COUNTER" -ge 10 -a "$COUNTER" -le 99 ]; then IPADDRESS="${IPPART}00${COUNTER}";
      elif [ "$COUNTER" -ge 100 -a "$COUNTER" -le 999 ]; then IPADDRESS="${IPPART}0${COUNTER}";
      elif [ "$COUNTER" -ge 1000 -a "$COUNTER" -le 9999 ]; then IPADDRESS="$IPPART$COUNTER";
      fi
    fi

    if [ -n "$IPADDRESS" ]; then 
    echo "$IPADDRESS" >> "$IPLIST"
    
    touch "$IPDIR$IPADDRESS"
    chown diradmin:diradmin "$IPDIR$IPADDRESS"
    chmod 600 "$IPDIR$IPADDRESS"

    echo "gateway="    >> "$IPDIR$IPADDRESS"
    echo "netmask=/64" >> "$IPDIR$IPADDRESS"
    echo "ns="         >> "$IPDIR$IPADDRESS"
    echo "reseller="   >> "$IPDIR$IPADDRESS"
    echo "status=free" >> "$IPDIR$IPADDRESS"
    echo "value="      >> "$IPDIR$IPADDRESS"
        
        IPADDRESS=""
    fi
    let COUNTER+=1
  done

echo "action=rewrite&value=ips" >> /usr/local/directadmin/data/task.queue
exit 0

How to use:
1. Create an empty file with executable permissions and add the contents of the code box to it.
2. Don't execute it just yet. Let's suppose you want to add the addresses from 2345:f1d2:1234:0190:0000:0000:0000:10 to 2345:f1d2:1234:0190:0000:0000:0000:100 to your server.
Then edit the following (pay attention to the semicolons):
Code:
IPPART=2345:f1d2:1234:0190:0000:0000:0000:
IPSTART=10
IPEND=100

If you prefer to add the IPv6 addresses in abbreviated format (ie: 2345:f1d2:1234:190::10 - 2345:f1d2:1234:190::100), then edit the following:
Code:
IPPART=2345:f1d2:1234:190::
IPSTART=10
IPEND=100
SHORT=true

After you have edited the appropriate parts, you can execute the script (you probably need to be root or use sudo).
The script is not perfect, it doesn't check if an IP already exists and can only add decimal ranges up to 9999, but it's a much faster way to add addresses, then adding them one by one, through the IP Manager.

Disclaimer: Use it at your own risk. No guaranties provided. I'm not responsible for any errors or damage the script might cause.
 
By the way, after running the script, run also:
Code:
 /etc/init.d/startips restart
This line could also be included in the script (above exit 0), but I can't edit my previous post to include it.
 
Back
Top