Brought get_remote_ip into version control.
[profile.git] / opt / bin / get_remote_ip
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;