Differential D16839 Diff 47080 cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/ip/get.ipv4remote.pl
Changeset View
Changeset View
Standalone View
Standalone View
cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/ip/get.ipv4remote.pl
Show All 21 Lines | |||||
# | # | ||||
# Copyright 2008 Sun Microsystems, Inc. All rights reserved. | # Copyright 2008 Sun Microsystems, Inc. All rights reserved. | ||||
# Use is subject to license terms. | # Use is subject to license terms. | ||||
# | # | ||||
#pragma ident "%Z%%M% %I% %E% SMI" | #pragma ident "%Z%%M% %I% %E% SMI" | ||||
# | # | ||||
# get.ipv4remote.pl [tcpport] | # get.ipv4remote.pl [port] [proto] | ||||
# | # | ||||
# Find an IPv4 reachable remote host using both ifconfig(1M) and ping(1M). | # Find an IPv4 reachable remote host using both ifconfig(1M) and ping(1M). | ||||
# If a tcpport is specified, return a host that is also listening on this | # If a port is specified, return a host that is also listening on this | ||||
# TCP port. Print the local address and the remote address, or an | # port. If the port is specified, the protocol can also be specified and | ||||
# defaults to tcp. Print the local address and the remote address, or an | |||||
# error message if no suitable remote host was found. Exit status is 0 if | # error message if no suitable remote host was found. Exit status is 0 if | ||||
# a host was found. | # a host was found. | ||||
# | # | ||||
use strict; | use strict; | ||||
use IO::Socket; | use IO::Socket; | ||||
my $MAXHOSTS = 32; # max hosts to port scan | my $MAXHOSTS = 32; # max hosts to port scan | ||||
my $TIMEOUT = 3; # connection timeout | my $TIMEOUT = 3; # connection timeout | ||||
my $tcpport = @ARGV == 1 ? $ARGV[0] : 0; | my $port = @ARGV >= 1 ? $ARGV[0] : 0; | ||||
my $proto = @ARGV == 2 ? $ARGV[1] : "tcp"; | |||||
# | # | ||||
# Determine local IP address | # Determine local IP address | ||||
# | # | ||||
my $local = ""; | my $local = ""; | ||||
my $remote = ""; | my $remote = ""; | ||||
my %Broadcast; | my %Broadcast; | ||||
my $up; | my $up; | ||||
Show All 21 Lines | |||||
# which isn't a local address. | # which isn't a local address. | ||||
# | # | ||||
open PING, "/sbin/ping -n -s 56 -c $MAXHOSTS $Broadcast{$local} |" or | open PING, "/sbin/ping -n -s 56 -c $MAXHOSTS $Broadcast{$local} |" or | ||||
die "Couldn't run ping: $!\n"; | die "Couldn't run ping: $!\n"; | ||||
while (<PING>) { | while (<PING>) { | ||||
if (/bytes from (.*): / and not defined $Broadcast{$1}) { | if (/bytes from (.*): / and not defined $Broadcast{$1}) { | ||||
my $addr = $1; | my $addr = $1; | ||||
if ($tcpport != 0) { | if ($port != 0) { | ||||
# | # | ||||
# Test TCP | # Test TCP | ||||
# | # | ||||
my $socket = IO::Socket::INET->new( | my $socket = IO::Socket::INET->new( | ||||
Proto => "tcp", | Type => SOCK_STREAM, | ||||
Proto => $proto, | |||||
PeerAddr => $addr, | PeerAddr => $addr, | ||||
PeerPort => $tcpport, | PeerPort => $port, | ||||
Timeout => $TIMEOUT, | Timeout => $TIMEOUT, | ||||
); | ); | ||||
next unless $socket; | next unless $socket; | ||||
close $socket; | close $socket; | ||||
} | } | ||||
$remote = $addr; | $remote = $addr; | ||||
last; | last; | ||||
} | } | ||||
} | } | ||||
close PING; | close PING; | ||||
die "Can't find a remote host for testing: No suitable response from " . | die "Can't find a remote host for testing: No suitable response from " . | ||||
"$Broadcast{$local}\n" if $remote eq ""; | "$Broadcast{$local}\n" if $remote eq ""; | ||||
print "$local $remote\n"; | print "$local $remote\n"; |