From: Iain Patterson Date: Thu, 23 Apr 2009 21:09:53 +0000 (+0100) Subject: Brought get_remote_ip into version control. X-Git-Url: http://git.iain.cx/?a=commitdiff_plain;h=0311d5d91bb476b3a60866579aaf2c8244797eb9;p=profile.git Brought get_remote_ip into version control. Use get_remote_ip to find the address of an ssh client. Use get_remote_ip -n to report the remote hostname if available. --- diff --git a/opt/bin/get_remote_ip b/opt/bin/get_remote_ip new file mode 100755 index 0000000..736a8c5 --- /dev/null +++ b/opt/bin/get_remote_ip @@ -0,0 +1,21 @@ +#!/usr/bin/perl +# +# get_remote_ip: Get IP address (or hostname) of the host from which this SSH +# session is active. +# Usage: get_remote_ip [-n] +# + +use Socket; + +my $ip = $ENV{SSH_CLIENT}; +exit 111 unless $ip; + +$ip =~ s/\s.*//; $ip =~ s/^::ffff://; +unless ($ARGV[0] eq "-n") { + print "$ip\n"; + exit 0; +} + +my $name = gethostbyaddr(inet_aton($ip), AF_INET); +if ($name) { $name =~ s/\..*//; print "$name\n"; } else { print "$ip\n" } +exit 0;