I have a shell function that returns a list of hostnames and IP addresses:
$ list_hosts
hostA 10.1.1.1
hostB 10.1.1.2
hostC 10.1.1.3
I want to get a different address for each host:
$ 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
}
This only prints out the IP6 for the first host returned by list_hosts
:
$ list_ip6
Host hostA has IP6 fd57:9e3a:c90e:753f:d625:ccff:feb0:7984/128
The problem appears to be the command substitution on the second line; if I comment that out, it loops over all the hosts:
$ list_ip6
Host hostA has IP6
Host hostB has IP6
Host hostC has IP6
Does the process substitution somehow interfere with reading from the the output of list_hosts
?
Is there a good way of doing this in the busybox ash
shell? In bash
I would just read the lines into an array and loop over the array but of course that's not an option in ash.
Edit to add:
The problem seems to be specifically the ssh
connection. If I just print out the local IP6 for each host then it works correctly:
$ 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
The process substitution is okay, it's just that SSH somehow breaks out of the loop.