This repository has been archived by the owner on Feb 22, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbirdctl.pm
70 lines (53 loc) · 1.38 KB
/
birdctl.pm
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
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/perl -w
package birdctl;
use strict;
use warnings;
use Carp;
use Params::Validate qw(:all);
use IO::Socket::UNIX;
# Constructor.
sub new {
my $class = shift(@_);
local ($!, $_);
my %attr = validate(@_, {
socket => { default => "/var/run/bird.ctl" },
restrict => 0,
});
my $socket = new IO::Socket::UNIX->new(
Type => SOCK_STREAM,
Peer => $attr{socket},
) or croak "Connection failed: $!";
defined($_ = <$socket>) or croak "While reading 'hello': $!";
/^0001 BIRD 1\.[\d\.]+ ready\.$/ or
/^0001 BIRD 2\.[\d\.]+ ready\.$/ or croak "Bad 'hello' received";
my $self = {
_socket => $socket,
};
bless $self, $class;
if ($attr{restrict}) {
$self->cmd("restrict") =~ /^0016/ or croak "Could not enter restricted mode";
}
return $self;
};
# Return an array of lines received in response to the given command.
sub long_cmd {
my $self = shift(@_);
my $socket = $self->{_socket};
local ($!, $_);
$socket->send(shift(@_) . "\n");
my @result;
while (<$socket>) {
# FIXME: Handle continuations (Rare, I believe)
push @result, $_;
/^\d{4} / and return @result;
}
# Fall through: no closing line found.
croak "While reading output: $!";
};
# Return just the finishing line received in response to the given command.
sub cmd {
my $self = shift(@_);
my @result = $self->long_cmd(@_);
return pop(@result);
};
1;