File: clientname

package info (click to toggle)
plum 2.33.1-9
  • links: PTS
  • area: main
  • in suites: sarge, woody
  • size: 908 kB
  • ctags: 128
  • sloc: perl: 4,442; makefile: 51
file content (59 lines) | stat: -rwxr-xr-x 1,345 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/perl -w
# $Id: clientname,v 2.10 1999/01/14 17:26:55 hasegawa Exp $
# copyright (c)1997-1999 Yoshinori Hasegawa <hasegawa@madoka.org>

if ($] < 5) {
  foreach $inc (@INC) {
    if (-r "$inc/sys/socket.ph") {
      eval 'require "sys/socket.ph"';
      $SOCKET = "$inc/sys/socket.ph" unless $@;
      last;
    }
    if (-r "$inc/socket.ph") {
      eval 'require "socket.ph"';
      $SOCKET = "$inc/socket.ph" unless $@;
      last;
    }
  }
} else {
  eval 'use Socket';
  $SOCKET = 'Socket.pm' unless $@;
}

$AF_INET = eval '&AF_INET' || 2;

&main(@ARGV);

sub main {
  local(@args) = @_;
  local($host, $addr, @addr, $name);
  if (@args < 1) {
    &usage;
    exit(1);
  }
  ($host) = @args;
  if ($host =~ /^\d+$/) {
    $addr = pack('N', $host);
  } elsif ($host =~ /^[\d\.]+$/) {
    @addr = split(/\./, $host);
    $addr = pack('C4', @addr, 0, 0, 0);
  } else {
    $addr = (gethostbyname($host))[4] || "\0\0\0\0";
  }
  if ($addr && $addr ne "\0\0\0\0") {
    $name = (gethostbyaddr($addr, $AF_INET))[0];
    if ($name) {
      print $name, "\n";
    } else {
      print "*** unknown ***\n";
    }
    print join('.', unpack('C4', $addr)), "\n";
    print unpack('N', $addr), "\n";
  } else {
    print "hostname lookup failed for $host\n";
  }
}

sub usage {
  print "usage: perl clientname {<hostname>|<ip-address>}\n";
}