blob: 4f9785485e17b3f13436fa73be3e7787d1dc1383 [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
25import struct
26import fcntl
27import os
28import signal
29import re
30import sys
31import time
32
kelvin-onlab66bccb72015-01-16 14:52:12 -080033sys.path.append( "../" )
adminbae64d82013-08-01 10:50:15 -070034
35from drivers.common.cli.remotetestbeddriver import RemoteTestBedDriver
36
adminbae64d82013-08-01 10:50:15 -070037
kelvin-onlab66bccb72015-01-16 14:52:12 -080038class RemoteVMDriver( RemoteTestBedDriver ):
39
40 """
41 RemoteVMDriver is the basic driver which will handle the Mininet functions
42 """
43 def __init__( self ):
44 super( RemoteTestBedDriver, self ).__init__()
45
46 def connect( self, **connectargs ):
47 for key in connectargs:
48 vars( self )[ key ] = connectargs[ key ]
49
50 self.name = self.options[ 'name' ]
51
52 self.handle = super(
53 RemoteVMDriver,
54 self ).connect(
55 user_name=self.user_name,
56 ip_address=self.ip_address,
57 port=self.port,
58 pwd=self.pwd )
59 if self.handle:
60 main.log.info( self.name + " connected successfully " )
adminbae64d82013-08-01 10:50:15 -070061 return self.handle
62 return main.TRUE
kelvin-onlab66bccb72015-01-16 14:52:12 -080063
64 def SSH( self, **connectargs ):
adminbae64d82013-08-01 10:50:15 -070065 for key in connectargs:
kelvin-onlab66bccb72015-01-16 14:52:12 -080066 vars( self )[ key ] = connectargs[ key ]
67
68 """
adminbae64d82013-08-01 10:50:15 -070069 Connection will establish to the remote host using ssh.
70 It will take user_name ,ip_address and password as arguments<br>
kelvin-onlab66bccb72015-01-16 14:52:12 -080071 and will return the handle.
72 """
adminbae64d82013-08-01 10:50:15 -070073 for key in connectargs:
kelvin-onlab66bccb72015-01-16 14:52:12 -080074 vars( self )[ key ] = connectargs[ key ]
75
adminbae64d82013-08-01 10:50:15 -070076 ssh_newkey = 'Are you sure you want to continue connecting'
kelvin-onlab66bccb72015-01-16 14:52:12 -080077 refused = "ssh: connect to host " + \
78 self.ip_address + " port 22: Connection refused"
adminbae64d82013-08-01 10:50:15 -070079 if self.port:
kelvin-onlab66bccb72015-01-16 14:52:12 -080080 self.handle.sendline(
81 'ssh -p ' +
82 self.port +
83 ' ' +
84 self.user_name +
85 '@' +
86 self.ip_address )
87 else:
88 self.handle.sendline(
89 'ssh ' +
90 self.user_name +
91 '@' +
92 self.ip_address )
93 self.handle.sendline( "\r" )
94
95 i = self.handle.expect( [ ssh_newkey,
96 'password:',
97 pexpect.EOF,
98 pexpect.TIMEOUT,
99 refused ],
100 120 )
101
102 if i == 0:
103 main.log.info( "ssh key confirmation received, send yes" )
104 self.handle.sendline( 'yes' )
105 i = self.handle.expect( [ ssh_newkey, 'password:', pexpect.EOF ] )
106 if i == 1:
107 main.log.info( "ssh connection asked for password, gave password" )
108 self.handle.sendline( self.pwd )
109 self.handle.expect( '>|#|$' )
110
111 elif i == 2:
112 main.log.error( "Connection timeout" )
adminbae64d82013-08-01 10:50:15 -0700113 return main.FALSE
kelvin-onlab66bccb72015-01-16 14:52:12 -0800114 elif i == 3: # timeout
115 main.log.error(
116 "No route to the Host " +
117 self.user_name +
118 "@" +
119 self.ip_address )
adminbae64d82013-08-01 10:50:15 -0700120 return main.FALSE
kelvin-onlab66bccb72015-01-16 14:52:12 -0800121 elif i == 4:
122 main.log.error(
123 "ssh: connect to host " +
124 self.ip_address +
125 " port 22: Connection refused" )
adminbae64d82013-08-01 10:50:15 -0700126 return main.FALSE
127
kelvin-onlab66bccb72015-01-16 14:52:12 -0800128 self.handle.sendline( "\r" )
adminbae64d82013-08-01 10:50:15 -0700129 return main.TRUE