summaryrefslogtreecommitdiff
path: root/svdrpsend.pl
blob: 5efd504be495bd5ba883da0822977d7932331f25 (plain)
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
#!/usr/bin/perl

use Socket;
use Getopt::Std;

$Usage = qq{
Usage: $0 options command...

Options: -d hostname        destination hostname (default: localhost)
         -p port            SVDRP port number (default: 2001)
};

die $Usage if (!$ARGV[0] || !getopts("d:p:"));

$Dest = $opt_d  || "localhost";
$Port = $opt_p  || 2001;
$Cmd  = "@ARGV" || Error("missing command");

$Timeout = 10; # max. seconds to wait for response

$SIG{ALRM} = sub { Error("timeout"); };
alarm($Timeout);

$iaddr = inet_aton($Dest)                   || Error("no host: $Dest");
$paddr = sockaddr_in($Port, $iaddr);

$proto = getprotobyname('tcp');
socket(SOCK, PF_INET, SOCK_STREAM, $proto)  || Error("socket: $!");
connect(SOCK, $paddr)                       || Error("connect: $!");
select(SOCK); $| = 1;
Receive();
Send($Cmd);
Send("quit");
close(SOCK)                                 || Error("close: $!");

sub Send
{
  my $cmd = shift || Error("no command to send");
  print SOCK "$cmd\r\n";
  Receive();
}

sub Receive
{
  while (<SOCK>) {
        print STDOUT $_;
        last if substr($_, 3, 1) ne "-";
        }
}

sub Error
{
  print STDERR "@_\n";
  close(SOCK);
  exit 0;
}