Are you sure you read the full /help on $server()?
$server(N).group does what you want:
//echo -a $server(1).group
Loop through them all to enumerate all the networks. You'll obviously want to ignore duplicate groups, but $addtok will do this for you, and you already seem to know how to do that:
; use with //echo -a $networks
alias networks {
var %i = 1, %max = $server(0), %groups
while (%i < %max) {
%groups = $addtok(%groups,$server(%i).group,32)
inc %i
}
return %groups
}
I'm assuming you have less than 4096bytes worth of network data, otherwise you'll need to write it to a file instead.