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


In reply to Determining what NIC has an internet gateway by slloyd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.