blob: e8f4c00c4b58d35613ad820fc171f04d27433aa1 [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#!/usr/bin/env python
kelvin-onlab66bccb72015-01-16 14:52:12 -08002"""
adminbae64d82013-08-01 10:50:15 -07003Created on 12-Feb-2013
Jeremy Songsterae01bba2016-07-11 15:39:17 -07004Modified 2015 by ON.Lab
5
6Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
7the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
8or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
adminbae64d82013-08-01 10:50:15 -07009
kelvin-onlab66bccb72015-01-16 14:52:12 -080010author:: Anil Kumar ( anilkumar.s@paxterrasolutions.com )
adminbae64d82013-08-01 10:50:15 -070011
12
13 TestON is free software: you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 2 of the License, or
kelvin-onlab66bccb72015-01-16 14:52:12 -080016 ( at your option ) any later version.
adminbae64d82013-08-01 10:50:15 -070017
18 TestON is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
kelvin-onlab66bccb72015-01-16 14:52:12 -080024 along with TestON. If not, see <http://www.gnu.org/licenses/>.
adminbae64d82013-08-01 10:50:15 -070025
26
27RemoteVMDriver is the basic driver which will handle the Mininet functions
kelvin-onlab66bccb72015-01-16 14:52:12 -080028"""
adminbae64d82013-08-01 10:50:15 -070029import pexpect
adminbae64d82013-08-01 10:50:15 -070030
31from drivers.common.cli.remotetestbeddriver import RemoteTestBedDriver
32
adminbae64d82013-08-01 10:50:15 -070033
kelvin-onlab66bccb72015-01-16 14:52:12 -080034class RemoteVMDriver( RemoteTestBedDriver ):
35
36 """
37 RemoteVMDriver is the basic driver which will handle the Mininet functions
38 """
39 def __init__( self ):
Devin Limdc78e202017-06-09 18:30:07 -070040 super( RemoteVMDriver, self ).__init__()
kelvin-onlab66bccb72015-01-16 14:52:12 -080041
42 def connect( self, **connectargs ):
43 for key in connectargs:
44 vars( self )[ key ] = connectargs[ key ]
45
46 self.name = self.options[ 'name' ]
47
48 self.handle = super(
49 RemoteVMDriver,
50 self ).connect(
51 user_name=self.user_name,
52 ip_address=self.ip_address,
53 port=self.port,
54 pwd=self.pwd )
55 if self.handle:
56 main.log.info( self.name + " connected successfully " )
adminbae64d82013-08-01 10:50:15 -070057 return self.handle
58 return main.TRUE
kelvin-onlab66bccb72015-01-16 14:52:12 -080059
60 def SSH( self, **connectargs ):
adminbae64d82013-08-01 10:50:15 -070061 for key in connectargs:
kelvin-onlab66bccb72015-01-16 14:52:12 -080062 vars( self )[ key ] = connectargs[ key ]
63
64 """
adminbae64d82013-08-01 10:50:15 -070065 Connection will establish to the remote host using ssh.
66 It will take user_name ,ip_address and password as arguments<br>
kelvin-onlab66bccb72015-01-16 14:52:12 -080067 and will return the handle.
68 """
adminbae64d82013-08-01 10:50:15 -070069 for key in connectargs:
kelvin-onlab66bccb72015-01-16 14:52:12 -080070 vars( self )[ key ] = connectargs[ key ]
71
adminbae64d82013-08-01 10:50:15 -070072 ssh_newkey = 'Are you sure you want to continue connecting'
kelvin-onlab66bccb72015-01-16 14:52:12 -080073 refused = "ssh: connect to host " + \
74 self.ip_address + " port 22: Connection refused"
adminbae64d82013-08-01 10:50:15 -070075 if self.port:
kelvin-onlab66bccb72015-01-16 14:52:12 -080076 self.handle.sendline(
77 'ssh -p ' +
78 self.port +
79 ' ' +
80 self.user_name +
81 '@' +
82 self.ip_address )
83 else:
84 self.handle.sendline(
85 'ssh ' +
86 self.user_name +
87 '@' +
88 self.ip_address )
89 self.handle.sendline( "\r" )
90
91 i = self.handle.expect( [ ssh_newkey,
92 'password:',
93 pexpect.EOF,
94 pexpect.TIMEOUT,
95 refused ],
96 120 )
97
98 if i == 0:
99 main.log.info( "ssh key confirmation received, send yes" )
100 self.handle.sendline( 'yes' )
101 i = self.handle.expect( [ ssh_newkey, 'password:', pexpect.EOF ] )
102 if i == 1:
103 main.log.info( "ssh connection asked for password, gave password" )
104 self.handle.sendline( self.pwd )
Devin Limdc78e202017-06-09 18:30:07 -0700105 self.handle.expect( self.prompt )
kelvin-onlab66bccb72015-01-16 14:52:12 -0800106
107 elif i == 2:
108 main.log.error( "Connection timeout" )
adminbae64d82013-08-01 10:50:15 -0700109 return main.FALSE
kelvin-onlab66bccb72015-01-16 14:52:12 -0800110 elif i == 3: # timeout
111 main.log.error(
112 "No route to the Host " +
113 self.user_name +
114 "@" +
115 self.ip_address )
adminbae64d82013-08-01 10:50:15 -0700116 return main.FALSE
kelvin-onlab66bccb72015-01-16 14:52:12 -0800117 elif i == 4:
118 main.log.error(
119 "ssh: connect to host " +
120 self.ip_address +
121 " port 22: Connection refused" )
adminbae64d82013-08-01 10:50:15 -0700122 return main.FALSE
123
kelvin-onlab66bccb72015-01-16 14:52:12 -0800124 self.handle.sendline( "\r" )
adminbae64d82013-08-01 10:50:15 -0700125 return main.TRUE