Netconf/Access/ssh.pm untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 12 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 134 CC 13 SCAR 41.3
CC
13
Cov
0%
start
132.0
recv
6.0
out
6.0
CRAP 62.2
1 # child of Access
2  
3 package Net::Netconf::Access::ssh;
4  
5 use Expect;
6 use Net::Netconf::Trace;
7 use Net::Netconf::Access;
8 use Net::Netconf::Constants;
9 use Carp;
10 use File::Which;
11 our $VERSION ='0.01';
12  
13 use vars qw(@ISA);
14 @ISA = qw(Net::Netconf::Access);
15  
16 sub disconnect
17 {
18 my ($self) = shift;
19 $self->{'ssh_obj'}->hard_close();
20 }
21  
22 sub start
23 {
24 my($self) = @_;
25 my $sshprog;
26  
27 # Get ssh port number if it exists
28 my $rport = (getservbyname('ssh', 'tcp'))[2];
29 $rport = Net::Netconf::Constants::NC_DEFAULT_PORT unless ( defined $self->{'server'} && $self->{'server'} eq 'junoscript');
30  
31 $self->{'server'} = 'netconf' unless $self->{'server'};
32  
33 my $echostate = 'stty -echo;';
34 if (exists($self->{'sshprog'})) {
35 $sshprog = $self->{'sshprog'};
36 } else {
37 $sshprog = which('ssh');
38 if (defined($sshprog) && ($sshprog ne '')) {
39 chomp($sshprog);
40 } else {
41 croak "Could not find sshclient on the system";
42 }
43 }
44  
45 # This implementation assumes OpenSSH.
46 my $command = $echostate . $sshprog . ' -l ' .
47 $self->{'login'} . ' -p ' . $rport .
48 ' -s ' . $self->{'hostname'} .
49 ' ' . $self->{'server'};
50  
51 # Create the Expect object
52 my $ssh = Expect->spawn($command);
53 # Turn off logging to stdout
54 $ssh->log_stdout(0);
55 $ssh->log_file($self->out);
56  
57 # Send our password or passphrase
58 if ($ssh->expect(10, 'password:', 'Password:', '(yes/no)?', '-re', 'passphrase.*:')) {
59 my $m_num = $ssh->match_number();
60
61 SWITCH: {
62 if (($m_num == 1) || ($m_num == 2) || ($m_num == 4)) {
63 print $ssh "$self->{'password'}\r";
64 last SWITCH;
65 }
66 if ($m_num == 3) {
67 # Host-key authenticity.
68 print $ssh "yes\r";
69 if ($ssh->expect(10, 'password:', 'Password:', '-re', 'passphrase.*:')) {
70 print $ssh "$self->{'password'}\r";
71 }
72 # After the yes/no option, expect the line: 'Warning:
73 # Permanently added <....> to the list of known hosts.'
74 $ssh->expect(10, 'known hosts.');
75 last SWITCH;
76 }
77 } # SWITCH
78 # If password prompted second time, it means user has give invalid password
79 if ($ssh->expect(10, 'password:', 'Password:', '-re', 'passphrase.*:'))
80 {
81 print "Failed to login to $self->{'hostname'}\n";
82 $self->{'seen_eof'} = 1;
83 }
84 }
85 else {
86 if ($ssh->expect(10, '-re', '<!(.*?)>')) {
87 # Things are good. Do nothing.
88 } else {
89 $self->{'seen_eof'} = 1;
90 }
91 }
92  
93 $self->{'ssh_obj'} = $ssh;
94 $self;
95 }
96  
97 sub send
98 {
99 my ($self, $xml) = @_;
100 my $ssh = $self->{'ssh_obj'};
101 $xml .= ']]>]]>';
102 print $ssh "$xml\r";
103 1;
104 }
105  
106 sub recv
107 {
108 my $self = shift;
109 my $xml;
110 my $ssh = $self->{'ssh_obj'};
111 if ($ssh->expect(600, ']]>]]>')) {
112 $xml = $ssh->before() . $ssh->match();
113 } else {
114 print "Failed to login to $self->{'hostname'}\n";
115 $self->{'seen_eof'} = 1;
116 }
117 $xml =~ s/]]>]]>//g;
118 $xml;
119 }
120  
121 sub out
122 {
123 my $self = @_;
124 foreach $line (@_) {
125 if ($line =~ /Permission\ denied/) {
126 print "Login failed: Permission Denied\n";
127 $self->{'ssh_obj'}->hard_close();
128 $self->{'seen_eof'} = 1;
129 }
130 }
131 }
132  
133 1;
134  
135 __END__