我不久前写了这个脚本:
#!/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
但这个小脚本只是打印结果。现在我想将生成的 IPv6 列表放入一个数组中以进一步处理它们。我该怎么做?
提前致谢
附言。如果您有更好的格式化 IPv6 的方法,请随时分享;)
编辑 :
ipv6 变量结果示例:
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
答案1
这是对我有用的结果:
#!/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"
所以基本上IPv6_RR+="$(printf "$IPv6_s%.4x$IPv6_t3\n" $((i+0x$IPv6_e))) "
创建以空格分隔的列表,然后IFS=' ' read -r -a IPv6_ARR <<< "$IPv6_RR[0]"
创建数组