blob: 892d9986a89cbe17315d2e023d520777371c6913 [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
4
kelvin-onlab66bccb72015-01-16 14:52:12 -08005author:: Anil Kumar ( anilkumar.s@paxterrasolutions.com )
adminbae64d82013-08-01 10:50:15 -07006
7
8 TestON is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
kelvin-onlab66bccb72015-01-16 14:52:12 -080011 ( at your option ) any later version.
adminbae64d82013-08-01 10:50:15 -070012
13 TestON is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
kelvin-onlab66bccb72015-01-16 14:52:12 -080019 along with TestON. If not, see <http://www.gnu.org/licenses/>.
adminbae64d82013-08-01 10:50:15 -070020
21
22RemoteVMDriver is the basic driver which will handle the Mininet functions
kelvin-onlab66bccb72015-01-16 14:52:12 -080023"""
adminbae64d82013-08-01 10:50:15 -070024import pexpect
adminbae64d82013-08-01 10:50:15 -070025
26from drivers.common.cli.remotetestbeddriver import RemoteTestBedDriver
27
adminbae64d82013-08-01 10:50:15 -070028
kelvin-onlab66bccb72015-01-16 14:52:12 -080029class RemoteVMDriver( RemoteTestBedDriver ):
30
31 """
32 RemoteVMDriver is the basic driver which will handle the Mininet functions
33 """
34 def __init__( self ):
35 super( RemoteTestBedDriver, self ).__init__()
36
37 def connect( self, **connectargs ):
38 for key in connectargs:
39 vars( self )[ key ] = connectargs[ key ]
40
41 self.name = self.options[ 'name' ]
42
43 self.handle = super(
44 RemoteVMDriver,
45 self ).connect(
46 user_name=self.user_name,
47 ip_address=self.ip_address,
48 port=self.port,
49 pwd=self.pwd )
50 if self.handle:
51 main.log.info( self.name + " connected successfully " )
adminbae64d82013-08-01 10:50:15 -070052 return self.handle
53 return main.TRUE
kelvin-onlab66bccb72015-01-16 14:52:12 -080054
55 def SSH( self, **connectargs ):
adminbae64d82013-08-01 10:50:15 -070056 for key in connectargs:
kelvin-onlab66bccb72015-01-16 14:52:12 -080057 vars( self )[ key ] = connectargs[ key ]
58
59 """
adminbae64d82013-08-01 10:50:15 -070060 Connection will establish to the remote host using ssh.
61 It will take user_name ,ip_address and password as arguments<br>
kelvin-onlab66bccb72015-01-16 14:52:12 -080062 and will return the handle.
63 """
adminbae64d82013-08-01 10:50:15 -070064 for key in connectargs:
kelvin-onlab66bccb72015-01-16 14:52:12 -080065 vars( self )[ key ] = connectargs[ key ]
66
adminbae64d82013-08-01 10:50:15 -070067 ssh_newkey = 'Are you sure you want to continue connecting'
kelvin-onlab66bccb72015-01-16 14:52:12 -080068 refused = "ssh: connect to host " + \
69 self.ip_address + " port 22: Connection refused"
adminbae64d82013-08-01 10:50:15 -070070 if self.port:
kelvin-onlab66bccb72015-01-16 14:52:12 -080071 self.handle.sendline(
72 'ssh -p ' +
73 self.port +
74 ' ' +
75 self.user_name +
76 '@' +
77 self.ip_address )
78 else:
79 self.handle.sendline(
80 'ssh ' +
81 self.user_name +
82 '@' +
83 self.ip_address )
84 self.handle.sendline( "\r" )
85
86 i = self.handle.expect( [ ssh_newkey,
87 'password:',
88 pexpect.EOF,
89 pexpect.TIMEOUT,
90 refused ],
91 120 )
92
93 if i == 0:
94 main.log.info( "ssh key confirmation received, send yes" )
95 self.handle.sendline( 'yes' )
96 i = self.handle.expect( [ ssh_newkey, 'password:', pexpect.EOF ] )
97 if i == 1:
98 main.log.info( "ssh connection asked for password, gave password" )
99 self.handle.sendline( self.pwd )
100 self.handle.expect( '>|#|$' )
101
102 elif i == 2:
103 main.log.error( "Connection timeout" )
adminbae64d82013-08-01 10:50:15 -0700104 return main.FALSE
kelvin-onlab66bccb72015-01-16 14:52:12 -0800105 elif i == 3: # timeout
106 main.log.error(
107 "No route to the Host " +
108 self.user_name +
109 "@" +
110 self.ip_address )
adminbae64d82013-08-01 10:50:15 -0700111 return main.FALSE
kelvin-onlab66bccb72015-01-16 14:52:12 -0800112 elif i == 4:
113 main.log.error(
114 "ssh: connect to host " +
115 self.ip_address +
116 " port 22: Connection refused" )
adminbae64d82013-08-01 10:50:15 -0700117 return main.FALSE
118
kelvin-onlab66bccb72015-01-16 14:52:12 -0800119 self.handle.sendline( "\r" )
adminbae64d82013-08-01 10:50:15 -0700120 return main.TRUE