#!/bin/sh
#	list-iface-gw 0.03
#
#	Copyright (C) 2000:	Manel Marin <manel3@apdo.com>
#	Licence:		GNU GPL version >= 2
#
#
#	Devuelve las IP de gw de los interfaces que se le dan como parmetro
#
#	Uso:	list-iface-gw eth1 eth2, list-iface-gw eth, list-iface-gw
#
#
#	------
#	Returns gw IP of interfaces given as parameter
#
#	Use:	list-iface-gw eth1 eth2, list-iface-gw eth, list-iface-gw
#

# SALIDA DE route -n
#Kernel IP routing table
#Destination     Gateway        Genmask         Flags Metric Ref    Use Iface
#255.255.255.255 0.0.0.0        255.255.255.255 UH    0      0        0 eth0
#192.168.0.0     0.0.0.0        255.255.255.0   U     0      0        0 eth0

# Luego: Gateway = $2
#	 Iface = $8


IFACES=$*		# Interfaces solicitados, "" == TODOS

/sbin/route -n | awk -v IFACES="$IFACES" '

    BEGIN {
	ORS = " "	# Separar valores de salida por espacios y no por \n
	if ( IFACES == "all" ) IFACES = ".*"	# Todos los interfaces 
    }

# PARA CADA LINEA
    {

# SI NO SE HA PEDIDO ESTE IFACE SIGUIENTE
	choosen = "no"
	split( IFACES, parts, "[ \t]+" )    # Crear tabla "parts" con ifaces
	for ( n in parts ) {
		##	print "n=" parts[n] " "			# debug
	    if ( $8 ~ "^"parts[n] ) choosen = "yes"
	}
	if ( choosen == "no" ) next

# ENVIAR A STDOUT
	print $2
    }
'
