xref: /onnv-gate/usr/src/cmd/dtrace/test/tst/common/ip/get.ipv6remote.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.ipv6remote.pl
31*6878Sbrendan#
32*6878Sbrendan# Find an IPv6 reachable remote host using both ifconfig(1M) and ping(1M).
33*6878Sbrendan# Print the local address and the remote address, or print nothing if either
34*6878Sbrendan# no IPv6 interfaces or remote hosts were found.  (Remote IPv6 testing is
35*6878Sbrendan# considered optional, and so not finding another IPv6 host is not an error
36*6878Sbrendan# state we need to log.)  Exit status is 0 if a host was found.
37*6878Sbrendan#
38*6878Sbrendan
39*6878Sbrendanuse strict;
40*6878Sbrendanuse IO::Socket;
41*6878Sbrendan
42*6878Sbrendanmy $MAXHOSTS = 32;			# max hosts to scan
43*6878Sbrendanmy $TIMEOUT = 3;			# connection timeout
44*6878Sbrendanmy $MULTICAST = "FF02::1";		# IPv6 multicast address
45*6878Sbrendan
46*6878Sbrendan#
47*6878Sbrendan# Determine local IP address
48*6878Sbrendan#
49*6878Sbrendanmy $local = "";
50*6878Sbrendanmy $remote = "";
51*6878Sbrendanmy %Local;
52*6878Sbrendanmy $up;
53*6878Sbrendanopen IFCONFIG, '/usr/sbin/ifconfig -a inet6 |'
54*6878Sbrendan    or die "Couldn't run ifconfig: $!\n";
55*6878Sbrendanwhile (<IFCONFIG>) {
56*6878Sbrendan	next if /^lo/;
57*6878Sbrendan
58*6878Sbrendan	# "UP" is always printed first (see print_flags() in ifconfig.c):
59*6878Sbrendan	$up = 1 if /^[a-z].*<UP,/;
60*6878Sbrendan	$up = 0 if /^[a-z].*<,/;
61*6878Sbrendan
62*6878Sbrendan	# assume output is "inet6 ...":
63*6878Sbrendan	if (m:inet6 (\S+)/:) {
64*6878Sbrendan		my $addr = $1;
65*6878Sbrendan                $Local{$addr} = 1;
66*6878Sbrendan                $local = $addr if $up and $local eq "";
67*6878Sbrendan		$up = 0;
68*6878Sbrendan	}
69*6878Sbrendan}
70*6878Sbrendanclose IFCONFIG;
71*6878Sbrendanexit 1 if $local eq "";
72*6878Sbrendan
73*6878Sbrendan#
74*6878Sbrendan# Find the first remote host that responds to an icmp echo,
75*6878Sbrendan# which isn't a local address.
76*6878Sbrendan#
77*6878Sbrendanopen PING, "/usr/sbin/ping -ns -A inet6 $MULTICAST 56 $MAXHOSTS |" or
78*6878Sbrendan    die "Couldn't run ping: $!\n";
79*6878Sbrendanwhile (<PING>) {
80*6878Sbrendan	if (/bytes from (.*): / and not defined $Local{$1}) {
81*6878Sbrendan		$remote = $1;
82*6878Sbrendan		last;
83*6878Sbrendan	}
84*6878Sbrendan}
85*6878Sbrendanclose PING;
86*6878Sbrendanexit 2 if $remote eq "";
87*6878Sbrendan
88*6878Sbrendanprint "$local $remote\n";
89