我有一个 shell 函数,它返回主机名和 IP 地址列表:
$ list_hosts
hostA 10.1.1.1
hostB 10.1.1.2
hostC 10.1.1.3
我想为每个主机获取不同的地址:
$ function list_ip6 {
list_hosts | while read -r hostname ip ; do
ip6=$(ssh ${ip} ip -o address list eth0 | awk '/scope global/ {print $4;}')
echo "Host ${name} has IP6 ${ip}"
done
}
这仅打印出返回的第一个主机的 IP6 list_hosts
:
$ list_ip6
Host hostA has IP6 fd57:9e3a:c90e:753f:d625:ccff:feb0:7984/128
问题似乎出在第二行的命令替换上;如果我注释掉它,它会循环所有主机:
$ list_ip6
Host hostA has IP6
Host hostB has IP6
Host hostC has IP6
进程替换是否会以某种方式干扰从 的输出读取list_hosts
?
在 busybox shell 中有没有好的方法可以做到这一点ash
?在我中,bash
我只是将行读入数组并循环遍历数组,但当然这不是 ash 中的选项。
编辑添加:
问题似乎特别出在ssh
连接上。如果我只是打印出每个主机的本地 IP6,那么它就可以正常工作:
$ function list_ip6 {
list_hosts | while read -r hostname ip ; do
ip6=$(ip -o address list eth0 | awk '/scope global/ {print $4;}')
echo "Host ${name} has IP6 ${ip}"
done
}
$ list_ip6
Host hostA has IP6 fd57:9e3a:c90e:753f:d625:ccff:feb0:7931/128
Host hostB has IP6 fd57:9e3a:c90e:753f:d625:ccff:feb0:7931/128
Host hostC has IP6 fd57:9e3a:c90e:753f:d625:ccff:feb0:7931/128
进程替换没问题,只是 SSH 以某种方式跳出了循环。