帮我写一个将文件中带子网掩码的IP段变为所有IP的shell脚本
以下是一个将文件中带子网掩码的IP段变为所有IP的shell脚本的示例:
#!/bin/bash
# 读取文件中的IP段并转换为所有IP
while IFS= read -r line; do
if [[ $line =~ ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\/([0-9]+) ]]; then
ip=${BASH_REMATCH[1]}
subnet=${BASH_REMATCH[2]}
IFS='.' read -r -a ip_parts <<< "$ip"
ip_start=${ip_parts[3]}
ip_end=$((2 ** (32 - subnet) - 1 + ip_start))
for ((i=ip_start; i<=ip_end; i++)); do
echo "${ip_parts[0]}.${ip_parts[1]}.${ip_parts[2]}.$i"
done
else
echo "无法解析的IP段: $line"
fi
done < "$1"
将上述脚本保存为convert_ip.sh文件,并赋予执行权限:
chmod +x convert_ip.sh
然后,可以通过运行以下命令来执行脚本:
./convert_ip.sh 文件名.txt
其中,文件名.txt是包含要转换的IP段的文件名。脚本将会逐行读取文件中的IP段,并输出所有IP地址。如果文件中存在无法解析的IP段,将会显示相应的错误信息
原文地址: https://www.cveoy.top/t/topic/iBlk 著作权归作者所有。请勿转载和采集!