Unicode stuff.
[profile.git] / opt / bin / get_remote_ip
1 #!/usr/bin/perl
2 #
3 # get_remote_ip: Get IP address (or hostname) of the host from which this SSH 
4 #                session is active.
5 # Usage: get_remote_ip [-n]
6 #
7
8 use Socket;
9
10 my $ip = $ENV{SSH_CLIENT};
11 exit 111 unless $ip;
12
13 $ip =~ s/\s.*//; $ip =~ s/^::ffff://;
14 unless ($ARGV[0] eq "-n") {
15   print "$ip\n";
16   exit 0;
17 }
18
19 my $name = gethostbyaddr(inet_aton($ip), AF_INET);
20 if ($name) { $name =~ s/\..*//; print "$name\n"; } else { print "$ip\n" }
21 exit 0;