linux - 如何将广播IP地址切入另一个变量

标签 linux bash ip

如何获取 ip 地址的字符串。 例如我输入 192.168.2.1,是否有一些方法可以将 2 和 1 剪切成其他字符串。如果不是,我的 for 循环就会有问题。

echo -n "Please enter bmc start ip to ping 192.168."
read bmcipStartaddress

echo -n "Please enter bmc end ip to ping 192.168."
read bmcipEndaddress

for((i=$bmcipStartaddress; i<=$bmcipEndaddress; i=i+2))
do
ip="192.168.$i"
echo -n "BMC IP : $ip:"

最佳答案

在我看来你需要两个循环,不是吗?

read -p "Please enter bmc start ip to ping 192.168." bmcipStartaddress
read -p "Please enter bmc end ip to ping 192.168." bmcipEndaddress
outerstart=${bmcipStartaddress%.*}
outerend=${bmcipEndaddress%.*}
for ((i=$outerstart; i<=$outerend; i++)); do
    if [ $i == $outerstart ]; then
        start=${bmcipStartaddress#*.}
    else
        start=0  # or 1 or 2, depending on what you want
    fi
    if [ $i == $outerend ]; then
        end=${bmcipEndaddress#*.}
    else
        end=254  # or 253 or 255, depending on what you want
    fi
    for ((j=$start; j<=$end; j=j+2)); do
        printf 'BMC IP: 192.168.%s.%s' "$i" "$j"
    done
done

如您所见,${var#*.}$var${var%.*} 中的第一个点向上删除 从最后一个点到结尾删除。

关于linux - 如何将广播IP地址切入另一个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28492918/

相关文章:

linux - 如何创建用于测试的虚拟流量?

linux - 无法安装 Vision Workbench

bash - 如何使用 sed 在文件中的每一行之前插入一行,原始行的内容被一个字符串包围?

linux - 显示目录存在的位置

将十六进制值转换为 C 中字符串类型的 ip 地址。

linux - netstat -t 不显示连接,但 netstat -nt 会显示

c - Linux 中断与轮询

c++ - 编译前将终端输出插入源文件

python - 我的 python 程序在 Windows 7 中运行良好并打印到控制台,但在 Linux Mint 中出现错误?

macos - 如何根据文件扩展名类型在终端中为 `open` 命令设置默认应用程序?