14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#!/usr/bin/perl -w
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# CDDL HEADER START
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# The contents of this file are subject to the terms of the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# Common Development and Distribution License (the "License").
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# You may not use this file except in compliance with the License.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# or http://www.opensolaris.org/os/licensing.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# See the License for the specific language governing permissions
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# and limitations under the License.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# When distributing Covered Code, include this CDDL HEADER in each
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# If applicable, add the following below this CDDL HEADER, with the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# fields enclosed by brackets "[]" replaced with your own identifying
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# information: Portions Copyright [yyyy] [name of copyright owner]
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# CDDL HEADER END
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# Use is subject to license terms.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#pragma ident "%Z%%M% %I% %E% SMI"
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# get.ipv4remote.pl [tcpport]
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# Find an IPv4 reachable remote host using both ifconfig(1M) and ping(1M).
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# If a tcpport is specified, return a host that is also listening on this
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# TCP port. Print the local address and the remote address, or an
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# error message if no suitable remote host was found. Exit status is 0 if
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# a host was found.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncuse strict;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncuse IO::Socket;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncmy $MAXHOSTS = 32; # max hosts to port scan
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncmy $TIMEOUT = 3; # connection timeout
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncmy $tcpport = @ARGV == 1 ? $ARGV[0] : 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# Determine local IP address
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncmy $local = "";
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncmy $remote = "";
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncmy %Broadcast;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncmy $up;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncopen IFCONFIG, '/usr/sbin/ifconfig -a |' or die "Couldn't run ifconfig: $!\n";
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncwhile (<IFCONFIG>) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync next if /^lo/;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync # "UP" is always printed first (see print_flags() in ifconfig.c):
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync $up = 1 if /^[a-z].*<UP,/;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync $up = 0 if /^[a-z].*<,/;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync # assume output is "inet X ... broadcast Z":
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (/inet (\S+) .* broadcast (\S+)/) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync my ($addr, $bcast) = ($1, $2);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync $Broadcast{$addr} = $bcast;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync $local = $addr if $up and $local eq "";
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync $up = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncclose IFCONFIG;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncdie "Could not determine local IP address" if $local eq "";
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# Find the first remote host that responds to an icmp echo,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync# which isn't a local address.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncopen PING, "/usr/sbin/ping -ns $Broadcast{$local} 56 $MAXHOSTS |" or
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync die "Couldn't run ping: $!\n";
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncwhile (<PING>) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (/bytes from (.*): / and not defined $Broadcast{$1}) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync my $addr = $1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ($tcpport != 0) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync #
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync # Test TCP
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync #
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync my $socket = IO::Socket::INET->new(
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Proto => "tcp",
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync PeerAddr => $addr,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync PeerPort => $tcpport,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Timeout => $TIMEOUT,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync );
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync next unless $socket;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync close $socket;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync $remote = $addr;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync last;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncclose PING;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncdie "Can't find a remote host for testing: No suitable response from " .
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "$Broadcast{$local}\n" if $remote eq "";
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncprint "$local $remote\n";