#!/usr/bin/perl -w use strict; use IO::Socket qw (getaddrinfo getnameinfo SOCK_STREAM NI_NUMERICHOST NI_NUMERICSERV ) ; my ($remote,$port, $iaddr, $paddr, $proto, $line); my $user = getpwuid($<); $remote = shift || 'localhost'; $port = shift || 2345; # random port if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') } die "No port" unless $port; my $sock = &connect($remote,$port); syswrite $sock, "$user\n"; # or die "syswrite $!\n"; while (<$sock>) { print ; } $sock->close or die "close: $!"; sub connect { my ($host0,$port0) = @_; my ($err,@res) = getaddrinfo($host0, $port0, {socktype=>SOCK_STREAM}); for my $res (@res) { my ($err1, $host, $port) = getnameinfo($res->{addr},NI_NUMERICHOST | NI_NUMERICSERV); print STDERR "Trying to connect to $host : $port...\n"; my $sock = new IO::Socket; $sock->socket($res->{family}, $res->{socktype}, $res->{protocol}) || next; if ($sock->connect($res->{addr})) { print STDERR "connected to $host port $port\n"; return $sock; } $sock->close; } die "connect attempt failed\n"; }