szabgab has asked for the wisdom of the Perl Monks concerning the following question:

I have to write a script that running on a Solaris box and given a distant IP address will tell me the local IP that will be used for sending packages to that IP.

There might be several network cards on the machine. For version 2 of the tool I also need support cases when there can be several IP addresses on one card.

Is there any module that would help me achive this?
(I could not find any.)
If no such module then how should I approach the problem?

Replies are listed 'Best First'.
Re: routing information on Solaris
by puploki (Hermit) on Sep 05, 2005 at 12:42 UTC

    I'm not a Solaris person, but I assume there's a route print command available? This should list network destinations, the bound NIC IP address and the gateway for the relevant destinations.

    For example, under Windows, you get the following output:

    Active Routes: Network Destination Netmask Gateway Interface M +etric 0.0.0.0 0.0.0.0 155.198.52.1 155.198.52.89 + 20 127.0.0.0 255.0.0.0 127.0.0.1 127.0.0.1 + 1 155.198.52.0 255.255.255.0 155.198.52.89 155.198.52.89 + 20 155.198.52.89 255.255.255.255 127.0.0.1 127.0.0.1 + 20 155.198.255.255 255.255.255.255 155.198.52.89 155.198.52.89 + 20 224.0.0.0 240.0.0.0 155.198.52.89 155.198.52.89 + 20 255.255.255.255 255.255.255.255 155.198.52.89 155.198.52.89 + 1 255.255.255.255 255.255.255.255 155.198.52.89 10004 + 1 Default Gateway: 155.198.52.1

    You can then do some matching based upon the destination IP address and the routes available to you.

      I assume there's a route print command available

      No, that information is supplied by netstat:

      # netstat -nr Routing Table: IPv4 Destination Gateway Flags Ref Use Interface -------------------- -------------------- ----- ----- ------ --------- 172.17.0.0 172.17.0.101 U 1 562395 hme0 224.0.0.0 172.17.0.101 U 1 0 hme0 default 172.17.0.6 UG 1 113291 127.0.0.1 127.0.0.1 UH 20 13053 lo0

      You will want to to see whether the target address is matched by any of the addresses (hosts or networks) in the first column. cpan::Net::CIDR features Net::CIDR::cidrlookup will help in this regard. If none of them match then the default address is used.

      Aliases on Solaris cards follow the standard convention. E.g. if the first interface is hme0, the first alias will be named hme0:1 and so forth. You can enumerate the interfaces on the machine by parsing the output of ifconfig -a. AFAIK there are no modules that offer this functionality... Now there's an itch to be scratched...

      update: it's been a while... I was a bit wrong with the netstat switches. You need netstat -nrv which looks like:

      # netstat -nrv IRE Table: Destination Mask Gateway Device Mxfrg + Rtt Ref Flg Out In/Fwd -------------------- --------------- -------------------- ------ ----- + ----- --- --- ----- ------ 10.0.1.0 255.255.255.0 172.17.0.21 1500 +* 0 0 UG 0 0 172.17.0.0 255.255.224.0 172.17.0.1 hme0 1500 +* 0 3 U 201577 0 224.0.0.0 240.0.0.0 172.17.0.1 hme0 1500 +* 0 3 U 0 0 default 0.0.0.0 172.17.0.6 1500 +* 0 0 UG 728283 0 127.0.0.1 255.255.255.255 127.0.0.1 lo0 8232 +* 0 0 UH 221 0

      You need to look at the Destination and Mask columns to determine if you are looking at a host or a network

      - another intruder with the mooring in the heart of the Perl