admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 2 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 3 | Created on 12-Feb-2013 |
| 4 | |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 5 | author:: Anil Kumar ( anilkumar.s@paxterrasolutions.com ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 6 | |
| 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-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 11 | ( at your option ) any later version. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 12 | |
| 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-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 19 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 20 | |
| 21 | |
| 22 | RemoteVMDriver is the basic driver which will handle the Mininet functions |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 23 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 24 | import pexpect |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 25 | |
| 26 | from drivers.common.cli.remotetestbeddriver import RemoteTestBedDriver |
| 27 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 28 | |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 29 | class 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 " ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 52 | return self.handle |
| 53 | return main.TRUE |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 54 | |
| 55 | def SSH( self, **connectargs ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 56 | for key in connectargs: |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 57 | vars( self )[ key ] = connectargs[ key ] |
| 58 | |
| 59 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 60 | Connection will establish to the remote host using ssh. |
| 61 | It will take user_name ,ip_address and password as arguments<br> |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 62 | and will return the handle. |
| 63 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 64 | for key in connectargs: |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 65 | vars( self )[ key ] = connectargs[ key ] |
| 66 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 67 | ssh_newkey = 'Are you sure you want to continue connecting' |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 68 | refused = "ssh: connect to host " + \ |
| 69 | self.ip_address + " port 22: Connection refused" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 70 | if self.port: |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 71 | 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" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 104 | return main.FALSE |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 105 | elif i == 3: # timeout |
| 106 | main.log.error( |
| 107 | "No route to the Host " + |
| 108 | self.user_name + |
| 109 | "@" + |
| 110 | self.ip_address ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 111 | return main.FALSE |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 112 | elif i == 4: |
| 113 | main.log.error( |
| 114 | "ssh: connect to host " + |
| 115 | self.ip_address + |
| 116 | " port 22: Connection refused" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 117 | return main.FALSE |
| 118 | |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 119 | self.handle.sendline( "\r" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 120 | return main.TRUE |