Netconf/Access/ssh.pm untested

filter: Statementstmt 0.0% 0 / 83 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 20 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 132 CC 7 SCAR 31.9
CC
7
Cov
0%
start
56.0
trace
2.0
disconnect
2.0
CRAP 24.2
1 package Net::Netconf::Access::ssh;
2  
3 use Net::SSH2;
4 use Net::Netconf::Trace;
5 use Net::Netconf::Constants;
6 use Carp;
7  
8 use parent 'Net::Netconf::Access';
9  
10 our $VERSION = '1.02';
11  
12 # Just for convenience
13 sub trace {
14 my $self = shift;
15 $self->{'trace_obj'}->trace(Net::Netconf::Trace::TRACE_LEVEL,
16 sprintf("[%s] %s", __PACKAGE__, @_));
17 }
18  
19 # Initialises an Net::SSH2 object, connects and authenticates to the Netconf
20 # server. Net::SSH2 object is stored in $self->{'ssh2'}, channel in
21 # $self->{'chan'}.
22 sub start {
23 my $self = shift;
24  
25 $self->{'server'} ||= 'netconf';
26  
27 my $port = ($self->{'server'} eq 'netconf') ?
28 $self->{'port'} || Net::Netconf::Constants::NC_DEFAULT_PORT :
29 $self->{'port'} || (getservbyname('ssh', 'tcp'))[2];
30  
31  
32 my $ssh2 = Net::SSH2->new();
33 croak "Failed to create a new Net::SSH2 object" unless(ref $ssh2);
34  
35 $self->trace("Making SSH connection to '$self->{'hostname'}:$port'...");
36 $ssh2->connect($self->{'hostname'}, $port);
37 croak "SSH connection failed: " . $ssh2->error() if($ssh2->error());
38 $self->trace("SSH connection succeeded!");
39  
40 $self->trace("Performing SSH authentication");
41 $ssh2->auth(username => $self->{'login'},
42 password => $self->{'password'});
43 croak "SSH authentication failed" if(!$ssh2->auth_ok() or $ssh2->error());
44 $self->trace("Authentication succeeded!");
45  
46 $self->trace("Requesting SSH channel...");
47 my $chan = $ssh2->channel();
48 croak "Failed to create SSH channel" if(!ref $chan or $ssh2->error());
49 $self->trace("Successfully created SSH channel!");
50  
51 $self->trace("Starting subsystem '$self->{'server'}'...");
52 my $subsystem = $chan->subsystem($self->{'server'});
53 if(!$subsystem) {
54 $self->trace("Failed to start '$self->{'server'}' subsystem, trying to exec");
55 $chan->exec($self->{'server'})
56 or croak "Failed to exec ". $self->{'server'};
57 $chan->flush();
58 $self->trace("Started server '$self->{'server'}' in exec");
59 } else {
60 $self->trace("Successfully started subsystem!");
61 }
62  
63 $self->{'ssh2'} = $ssh2;
64 $self->{'chan'} = $chan;
65 return $self;
66 }
67  
68 # Gracefully disconnect from Netconf server
69 sub disconnect {
70 my $self = shift;
71  
72 my $ssh2 = $self->{'ssh2'};
73 $ssh2->disconnect("Bye bye from $0 [" . __PACKAGE__ . " v$VERSION]");
74 $self->trace("Disconnected from SSH server");
75  
76 undef $self;
77 }
78  
79 # Writes an XML request to the Netconf server.
80 sub send {
81 my ($self, $xml) = @_;
82  
83 $xml .= ']]>]]>';
84  
85 my $len = length($xml);
86  
87 $self->trace("Will write $len bytes to the SSH channel:");
88 $self->trace("$xml");
89  
90 # Make the channel blocking, so the write() call below waits until there
91 # is available buffer space. Otherwise we'll end up busy-looping.
92 $self->{'chan'}->blocking(1);
93  
94 my $written = 0;
95 while($written != $len) {
96 my $nbytes = $self->{'chan'}->write($xml)
97 or croak "Failed to write XML data to SSH channel!";
98 $written += $nbytes;
99 $self->trace("Wrote $nbytes bytes (total written: $written).");
100 substr($xml, 0, $nbytes) = '';
101 }
102 $self->trace("Successfully wrote $written bytes to SSH channel!");
103 return 1;
104 }
105  
106 # Reads an XML reply from the Netconf server.
107 sub recv {
108 my $self = shift;
109 my $ssh2 = $self->{'ssh2'};
110 my $chan = $self->{'chan'};
111  
112 # Make the channel non-blocking, so that read() below allows for partial
113 # reads (as we can't possibly know if the data we're about to receive is an
114 # exact multiple of the buffer size argument, and doing it one character at
115 # a time instead would be terribly inefficient).
116 $chan->blocking(0);
117  
118 $self->trace("Reading XML response from Netconf server...");
119 my ($resp, $buf);
120 do {
121 # Wait up to 10 seconds for data to become available before attempting
122 # to read anything (in order to avoid busy-looping on $chan->read())
123 my @poll = ({ handle => $chan, events => 'in' });
124 $ssh2->poll(10000, \@poll);
125  
126 $nbytes = $chan->read($buf, 65536) || 0;
127 $self->trace("Read $nbytes bytes from SSH channel: '$buf'");
128 $resp .= $buf;
129 } until($resp =~ s/]]>]]>$//);
130 $self->trace("Received XML response '$resp'");
131  
132 return $resp;
133 }
134  
135 # Checks if the server sent us an EOF
136 sub eof {
137 my $self = shift;
138 return $self->{'chan'}->eof();
139 }
140  
141 1;
142  
143 __END__