xref: /onnv-gate/usr/src/cmd/dtrace/test/tst/common/ip/get.ipv4remote.pl (revision 6878:360e73ea6b0c)
1*6878Sbrendan#!/usr/bin/perl -w
2*6878Sbrendan#
3*6878Sbrendan# CDDL HEADER START
4*6878Sbrendan#
5*6878Sbrendan# The contents of this file are subject to the terms of the
6*6878Sbrendan# Common Development and Distribution License (the "License").
7*6878Sbrendan# You may not use this file except in compliance with the License.
8*6878Sbrendan#
9*6878Sbrendan# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*6878Sbrendan# or http://www.opensolaris.org/os/licensing.
11*6878Sbrendan# See the License for the specific language governing permissions
12*6878Sbrendan# and limitations under the License.
13*6878Sbrendan#
14*6878Sbrendan# When distributing Covered Code, include this CDDL HEADER in each
15*6878Sbrendan# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*6878Sbrendan# If applicable, add the following below this CDDL HEADER, with the
17*6878Sbrendan# fields enclosed by brackets "[]" replaced with your own identifying
18*6878Sbrendan# information: Portions Copyright [yyyy] [name of copyright owner]
19*6878Sbrendan#
20*6878Sbrendan# CDDL HEADER END
21*6878Sbrendan#
22*6878Sbrendan
23*6878Sbrendan#
24*6878Sbrendan# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25*6878Sbrendan# Use is subject to license terms.
26*6878Sbrendan#
27*6878Sbrendan#pragma ident	"%Z%%M%	%I%	%E% SMI"
28*6878Sbrendan
29*6878Sbrendan#
30*6878Sbrendan# get.ipv4remote.pl [tcpport]
31*6878Sbrendan#
32*6878Sbrendan# Find an IPv4 reachable remote host using both ifconfig(1M) and ping(1M).
33*6878Sbrendan# If a tcpport is specified, return a host that is also listening on this
34*6878Sbrendan# TCP port.  Print the local address and the remote address, or an
35*6878Sbrendan# error message if no suitable remote host was found.  Exit status is 0 if
36*6878Sbrendan# a host was found.
37*6878Sbrendan#
38*6878Sbrendan
39*6878Sbrendanuse strict;
40*6878Sbrendanuse IO::Socket;
41*6878Sbrendan
42*6878Sbrendanmy $MAXHOSTS = 32;			# max hosts to port scan
43*6878Sbrendanmy $TIMEOUT = 3;			# connection timeout
44*6878Sbrendanmy $tcpport = @ARGV == 1 ? $ARGV[0] : 0;
45*6878Sbrendan
46*6878Sbrendan#
47*6878Sbrendan# Determine local IP address
48*6878Sbrendan#
49*6878Sbrendanmy $local = "";
50*6878Sbrendanmy $remote = "";
51*6878Sbrendanmy %Broadcast;
52*6878Sbrendanmy $up;
53*6878Sbrendanopen IFCONFIG, '/usr/sbin/ifconfig -a |' or die "Couldn't run ifconfig: $!\n";
54*6878Sbrendanwhile (<IFCONFIG>) {
55*6878Sbrendan	next if /^lo/;
56*6878Sbrendan
57*6878Sbrendan	# "UP" is always printed first (see print_flags() in ifconfig.c):
58*6878Sbrendan	$up = 1 if /^[a-z].*<UP,/;
59*6878Sbrendan	$up = 0 if /^[a-z].*<,/;
60*6878Sbrendan
61*6878Sbrendan	# assume output is "inet X ... broadcast Z":
62*6878Sbrendan	if (/inet (\S+) .* broadcast (\S+)/) {
63*6878Sbrendan		my ($addr, $bcast) = ($1, $2);
64*6878Sbrendan		$Broadcast{$addr} = $bcast;
65*6878Sbrendan		$local = $addr if $up and $local eq "";
66*6878Sbrendan		$up = 0;
67*6878Sbrendan	}
68*6878Sbrendan}
69*6878Sbrendanclose IFCONFIG;
70*6878Sbrendandie "Could not determine local IP address" if $local eq "";
71*6878Sbrendan
72*6878Sbrendan#
73*6878Sbrendan# Find the first remote host that responds to an icmp echo,
74*6878Sbrendan# which isn't a local address.
75*6878Sbrendan#
76*6878Sbrendanopen PING, "/usr/sbin/ping -ns $Broadcast{$local} 56 $MAXHOSTS |" or
77*6878Sbrendan    die "Couldn't run ping: $!\n";
78*6878Sbrendanwhile (<PING>) {
79*6878Sbrendan	if (/bytes from (.*): / and not defined $Broadcast{$1}) {
80*6878Sbrendan		my $addr = $1;
81*6878Sbrendan
82*6878Sbrendan		if ($tcpport != 0) {
83*6878Sbrendan			#
84*6878Sbrendan			# Test TCP
85*6878Sbrendan			#
86*6878Sbrendan			my $socket = IO::Socket::INET->new(
87*6878Sbrendan				Proto    => "tcp",
88*6878Sbrendan				PeerAddr => $addr,
89*6878Sbrendan				PeerPort => $tcpport,
90*6878Sbrendan				Timeout  => $TIMEOUT,
91*6878Sbrendan			);
92*6878Sbrendan			next unless $socket;
93*6878Sbrendan			close $socket;
94*6878Sbrendan		}
95*6878Sbrendan
96*6878Sbrendan		$remote = $addr;
97*6878Sbrendan		last;
98*6878Sbrendan	}
99*6878Sbrendan}
100*6878Sbrendanclose PING;
101*6878Sbrendandie "Can't find a remote host for testing: No suitable response from " .
102*6878Sbrendan    "$Broadcast{$local}\n" if $remote eq "";
103*6878Sbrendan
104*6878Sbrendanprint "$local $remote\n";
105