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