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

I wrote the following code to determine what network connection (NIC or virtual nic) actually has an internet connection (gateway). I would be interested in your comments. Is there an easier way to determine this?
use strict; use Socket; my $hostname = `hostname`; $hostname=strip($hostname); my $ip_address=''; my ($name,$aliases,$addrtype,$length,@addrs)=gethostbyname($hostname); foreach my $ip_addr (@addrs){ my @tmp = unpack('C4',$ip_addr); $ip_address=join('.',@tmp); next if $ip_address=~/^169\.254/s; print "checking $ip_address\n"; my ($sock,$err) = connectToHost("www.google.com",80,$ip_addr); if(!$err){last;} } print "IP Address with gateway to internet: $ip_address\n"; exit; ################## sub connectToHost { # Create a socket that connects to a certain host # connectToHost($MainSock, $remote_hostname, $port) my ($remote_hostname, $port,$ip_addr) = @_; my $Sock; #print "connectToHost ($remote_hostname, $port)\n"; my ($socket_format, $proto, $packed_port, $cur); my ($remote_addr, @remote_ip, $remote_ip); my ($local_port, $remote_port); if ($port !~ /^\d+$/) { $port = (getservbyname($port, "tcp"))[2]; $port = 80 unless ($port); } $proto = (getprotobyname('tcp'))[2]; $remote_addr = (gethostbyname($remote_hostname))[4]; if (!$remote_addr) { return (undef,"Unknown host: $remote_hostname"); } @remote_ip = unpack("C4", $remote_addr); $remote_ip = join(".", @remote_ip); #print STDOUT "Connecting to $remote_ip port $port.\r\n"; $socket_format = 'S n a4 x8'; $local_port = pack($socket_format, &AF_INET, 0, $ip_addr); $remote_port = pack($socket_format, &AF_INET, $port, $remote_addr) +; socket($Sock, &AF_INET, &SOCK_STREAM, $proto) || return (undef,"So +cket Error: $!"); bind($Sock, $local_port) || return (undef,"Socket Bind Error: $!") +; #print "connect($Sock, $remote_port)\n"; connect($Sock, $remote_port) || return (undef,"Socket Connect Erro +r: $!"); $cur = select($Sock); $| = 1; # Disable buffering on socket. select($cur); return $Sock; } ############### sub strip{ #usage: $str=strip($str); #info: strips off beginning and endings returns, newlines, tabs, a +nd spaces my $str=shift; if(length($str)==0){return;} $str=~s/^[\r\n\s\t]+//s; $str=~s/[\r\n\s\t]+$//s; return $str; } exit;

-------------------------------
by me
http://www.basgetti.com
http://www.kidlins.com

Replies are listed 'Best First'.
Re: Determining what NIC has an internet gateway
by Corion (Patriarch) on Feb 18, 2006 at 21:19 UTC

    I use the following code/logic in Net::Pcap::FindDevice to find the interface with the default gateway:

    # `netstat -rn` my $device_ip; my $re_if = $^O eq 'MSWin32' ? qr/^\s*(?:0.0.0.0)\s+(\S+)\s+(\S+)\s+/ : qr/^(?:0.0.0.0|default)\s+(\S+)\s+.*?(\S+)\s*$/; for (qx{netstat -rn}) { if ( /$re_if/ ) { $device_ip = $2; last; }; };

    This gives me the IP address of the device. After that, it's just a matter of finding the corresponding Net::Pcap device, but that's of little oncernt to you :-)

      Unfortunately,

      1. There is no guarantee that the default interface actually connects to the internet (stupid, but not unknown)
      2. Some OSes can have multiple default routes

      A much easier method (and avoiding shelling out) is to create an IO::Socket::INET connection and then ask it which IP it binds to:

      use IO::Socket::INET; my $sock = IO::Socket::INET->new('www.google.com:80') or die "Can't bind : $@\n"; print 'IP Address with gateway to internet: ',$sock->sockhost(),"\n";

      Note that this does not identify which (physical or virtual) NIC has connectivity, but then, neither does the original code, so I am not sure that that was what was actually sought. IO::Interface offers functions to make that connection.


      The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. — Cyrus H. Gordon
Re: Determining what NIC has an internet gateway
by traveler (Parson) on Feb 18, 2006 at 23:10 UTC
    slloyd's andidsfa's code try to find out which interface will reach google, and it's a pretty good guess that that interface will reach the whole 'net. However, I have worked with sites that manage their traffic by dividing the traffic to the Internet through multiple routers.

    Also, slloyd's code does not handle multiple interfaces to reach the 'net: it only prints one of these. It is not unusual in a large net to have paths to the outside on more than one interface.

    So, my point is that you really have to define what you want for this tool to do: find one path to the net, find all paths to the net, find the path to google, or whatever.

    HTH, --traveler