Brought get_remote_ip into version control.
authorIain Patterson <me@iain.cx>
Thu, 23 Apr 2009 21:09:53 +0000 (22:09 +0100)
committerIain Patterson <me@iain.cx>
Thu, 23 Apr 2009 21:09:53 +0000 (22:09 +0100)
Use get_remote_ip to find the address of an ssh client.
Use get_remote_ip -n to report the remote hostname if
available.

opt/bin/get_remote_ip [new file with mode: 0755]

diff --git a/opt/bin/get_remote_ip b/opt/bin/get_remote_ip
new file mode 100755 (executable)
index 0000000..736a8c5
--- /dev/null
@@ -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;