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 |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 4 | Modified 2015 by ON.Lab |
| 5 | |
| 6 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 7 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 8 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 9 | |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 10 | author:: Anil Kumar ( anilkumar.s@paxterrasolutions.com ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 11 | |
| 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-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 16 | ( at your option ) any later version. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 17 | |
| 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-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 24 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 25 | |
| 26 | |
| 27 | RemoteVMDriver is the basic driver which will handle the Mininet functions |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 28 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 29 | import pexpect |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 30 | |
| 31 | from drivers.common.cli.remotetestbeddriver import RemoteTestBedDriver |
| 32 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 33 | |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 34 | class RemoteVMDriver( RemoteTestBedDriver ): |
| 35 | |
| 36 | """ |
| 37 | RemoteVMDriver is the basic driver which will handle the Mininet functions |
| 38 | """ |
| 39 | def __init__( self ): |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 40 | super( RemoteVMDriver, self ).__init__() |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 41 | |
| 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 " ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 57 | return self.handle |
| 58 | return main.TRUE |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 59 | |
| 60 | def SSH( self, **connectargs ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 61 | for key in connectargs: |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 62 | vars( self )[ key ] = connectargs[ key ] |
| 63 | |
| 64 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 65 | Connection will establish to the remote host using ssh. |
| 66 | It will take user_name ,ip_address and password as arguments<br> |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 67 | and will return the handle. |
| 68 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 69 | for key in connectargs: |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 70 | vars( self )[ key ] = connectargs[ key ] |
| 71 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 72 | ssh_newkey = 'Are you sure you want to continue connecting' |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 73 | refused = "ssh: connect to host " + \ |
| 74 | self.ip_address + " port 22: Connection refused" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 75 | if self.port: |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 76 | 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 Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 105 | self.handle.expect( self.prompt ) |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 106 | |
| 107 | elif i == 2: |
| 108 | main.log.error( "Connection timeout" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 109 | return main.FALSE |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 110 | elif i == 3: # timeout |
| 111 | main.log.error( |
| 112 | "No route to the Host " + |
| 113 | self.user_name + |
| 114 | "@" + |
| 115 | self.ip_address ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 116 | return main.FALSE |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 117 | elif i == 4: |
| 118 | main.log.error( |
| 119 | "ssh: connect to host " + |
| 120 | self.ip_address + |
| 121 | " port 22: Connection refused" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 122 | return main.FALSE |
| 123 | |
kelvin-onlab | 66bccb7 | 2015-01-16 14:52:12 -0800 | [diff] [blame] | 124 | self.handle.sendline( "\r" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 125 | return main.TRUE |