0

I wrote this script a little while ago :

#!/bin/bash

#Default number of IPs
NUM_IPS=48
if [ "$1" != "" ]; then
    NUM_IPS=$1
fi

#Example of IPv6 supplied 2001:41d0:0303:6e1b::/64

read -p "Starting IPv6 : " IPv6
IPv6_c=$(echo $IPv6 | sed -e 's/\/64//g' | sed -e 's/:*$//g')
IPv6_e=$(sed -e 's#.*:\(\)#\1#' <<< "$IPv6_c")
IPv6_s=$(echo $IPv6_c | sed -e "s/$IPv6_e//g")
IPv6_t1=$(echo $IPv6 | sed -e 's/\/64//g')
IPv6_t2=$(echo $IPv6_t1 | sed -e "s/$IPv6_c//g")
IPv6_t3=$IPv6_t2"/64"

for i in $(seq 0 $((NUM_IPS-1))); do
    printf "$IPv6_s%.4x$IPv6_t3\n" $((i+0x$IPv6_e))
done

But this little script simply prints the results. Now I would like to have this generated list of IPv6 into an array to further process them. How may I do this ?

Thanks in advance

PS. If you have a better way of formatting the IPv6 feel free to share ;)

EDIT :

Example of result of ipv6 variables:

Starting IPv6 : 2001:41d0:0303:6e1b::/64

IPv6_c......: 2001:41d0:0303:6e1b
IPv6_e......: 6e1b
IPv6_s......: 2001:41d0:0303:
IPv6_t1.....: 2001:41d0:0303:6e1b::
IPv6_t2.....: ::
IPv6_t3.....: ::/64
3
  • To add an element to an array, use arr+=(element) Commented Jun 17, 2018 at 23:57
  • Do you happen to know a better way of generating the ipv6 from this input example ? (I add the example variables in my post) Commented Jun 18, 2018 at 0:37
  • @choroba when I echo my array IPv6_ARR+=$(printf "$IPv6_s%.4x$IPv6_t3\n" $((i+0x$IPv6_e))) like this echo $IPv6_ARR[0], it echos the entire generated list without even separating the result. Any leads on that Commented Jun 18, 2018 at 1:44

1 Answer 1

0

Here is the result that works for me :

#!/bin/bash

Generate_IPv6(){
    #Variables
    if [ "$1" != "" ]; then
        NUM_IPS=$1
    else
        NUM_IPS=48
    fi
    if [ "$2" != "" ]; then
        IPv6=$2
    else
        read -p "Starting IPv6 : " IPv6
    fi
    IPv6_c=$(echo $IPv6 | sed -e 's/\/64//g' | sed -e 's/:*$//g')
    IPv6_e=$(sed -e 's#.*:\(\)#\1#' <<< "$IPv6_c")
    IPv6_s=$(echo $IPv6_c | sed -e "s/$IPv6_e//g")
    IPv6_t1=$(echo $IPv6 | sed -e 's/\/64//g')
    IPv6_t2=$(echo $IPv6_t1 | sed -e "s/$IPv6_c//g")
    IPv6_t3=$IPv6_t2"/64"
    for i in $(seq 0 $((NUM_IPS-1))); do
        IPv6_RR+="$(printf "$IPv6_s%.4x$IPv6_t3\n" $((i+0x$IPv6_e))) "
    done
    IFS=' ' read -r -a IPv6_ARR <<< "$IPv6_RR[0]"
}
Generate_IPv6 "$1" "$2"

So basically IPv6_RR+="$(printf "$IPv6_s%.4x$IPv6_t3\n" $((i+0x$IPv6_e))) " creates the list separated by space, then IFS=' ' read -r -a IPv6_ARR <<< "$IPv6_RR[0]" creates the array

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.