1210753Srpaulo#!/usr/bin/perl -w 2210753Srpaulo# 3210753Srpaulo# CDDL HEADER START 4210753Srpaulo# 5210753Srpaulo# The contents of this file are subject to the terms of the 6210753Srpaulo# Common Development and Distribution License (the "License"). 7210753Srpaulo# You may not use this file except in compliance with the License. 8210753Srpaulo# 9210753Srpaulo# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10210753Srpaulo# or http://www.opensolaris.org/os/licensing. 11210753Srpaulo# See the License for the specific language governing permissions 12210753Srpaulo# and limitations under the License. 13210753Srpaulo# 14210753Srpaulo# When distributing Covered Code, include this CDDL HEADER in each 15210753Srpaulo# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16210753Srpaulo# If applicable, add the following below this CDDL HEADER, with the 17210753Srpaulo# fields enclosed by brackets "[]" replaced with your own identifying 18210753Srpaulo# information: Portions Copyright [yyyy] [name of copyright owner] 19210753Srpaulo# 20210753Srpaulo# CDDL HEADER END 21210753Srpaulo# 22210753Srpaulo 23210753Srpaulo# 24210753Srpaulo# Copyright 2008 Sun Microsystems, Inc. All rights reserved. 25210753Srpaulo# Use is subject to license terms. 26210753Srpaulo# 27210753Srpaulo#pragma ident "%Z%%M% %I% %E% SMI" 28210753Srpaulo 29210753Srpaulo# 30210753Srpaulo# get.ipv4remote.pl [tcpport] 31210753Srpaulo# 32210753Srpaulo# Find an IPv4 reachable remote host using both ifconfig(1M) and ping(1M). 33210753Srpaulo# If a tcpport is specified, return a host that is also listening on this 34210753Srpaulo# TCP port. Print the local address and the remote address, or an 35210753Srpaulo# error message if no suitable remote host was found. Exit status is 0 if 36210753Srpaulo# a host was found. 37210753Srpaulo# 38210753Srpaulo 39210753Srpaulouse strict; 40210753Srpaulouse IO::Socket; 41210753Srpaulo 42210753Srpaulomy $MAXHOSTS = 32; # max hosts to port scan 43210753Srpaulomy $TIMEOUT = 3; # connection timeout 44210753Srpaulomy $tcpport = @ARGV == 1 ? $ARGV[0] : 0; 45210753Srpaulo 46210753Srpaulo# 47210753Srpaulo# Determine local IP address 48210753Srpaulo# 49210753Srpaulomy $local = ""; 50210753Srpaulomy $remote = ""; 51210753Srpaulomy %Broadcast; 52210753Srpaulomy $up; 53211545Srpauloopen IFCONFIG, '/sbin/ifconfig -a |' or die "Couldn't run ifconfig: $!\n"; 54210753Srpaulowhile (<IFCONFIG>) { 55210753Srpaulo next if /^lo/; 56210753Srpaulo 57210753Srpaulo # "UP" is always printed first (see print_flags() in ifconfig.c): 58210753Srpaulo $up = 1 if /^[a-z].*<UP,/; 59210753Srpaulo $up = 0 if /^[a-z].*<,/; 60210753Srpaulo 61210753Srpaulo # assume output is "inet X ... broadcast Z": 62210753Srpaulo if (/inet (\S+) .* broadcast (\S+)/) { 63210753Srpaulo my ($addr, $bcast) = ($1, $2); 64210753Srpaulo $Broadcast{$addr} = $bcast; 65210753Srpaulo $local = $addr if $up and $local eq ""; 66210753Srpaulo $up = 0; 67210753Srpaulo } 68210753Srpaulo} 69210753Srpauloclose IFCONFIG; 70210753Srpaulodie "Could not determine local IP address" if $local eq ""; 71210753Srpaulo 72210753Srpaulo# 73210753Srpaulo# Find the first remote host that responds to an icmp echo, 74210753Srpaulo# which isn't a local address. 75210753Srpaulo# 76211545Srpauloopen PING, "/sbin/ping -ns $Broadcast{$local} 56 $MAXHOSTS |" or 77210753Srpaulo die "Couldn't run ping: $!\n"; 78210753Srpaulowhile (<PING>) { 79210753Srpaulo if (/bytes from (.*): / and not defined $Broadcast{$1}) { 80210753Srpaulo my $addr = $1; 81210753Srpaulo 82210753Srpaulo if ($tcpport != 0) { 83210753Srpaulo # 84210753Srpaulo # Test TCP 85210753Srpaulo # 86210753Srpaulo my $socket = IO::Socket::INET->new( 87210753Srpaulo Proto => "tcp", 88210753Srpaulo PeerAddr => $addr, 89210753Srpaulo PeerPort => $tcpport, 90210753Srpaulo Timeout => $TIMEOUT, 91210753Srpaulo ); 92210753Srpaulo next unless $socket; 93210753Srpaulo close $socket; 94210753Srpaulo } 95210753Srpaulo 96210753Srpaulo $remote = $addr; 97210753Srpaulo last; 98210753Srpaulo } 99210753Srpaulo} 100210753Srpauloclose PING; 101210753Srpaulodie "Can't find a remote host for testing: No suitable response from " . 102210753Srpaulo "$Broadcast{$local}\n" if $remote eq ""; 103210753Srpaulo 104210753Srpauloprint "$local $remote\n"; 105