admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 3 | Created on 24-Oct-2012 |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 4 | Modified 2016 by ON.Lab |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 5 | |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 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 | |
| 10 | TestON is free software: you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation, either version 2 of the License, or |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 13 | ( at your option ) any later version. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 14 | |
| 15 | TestON is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 21 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 22 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 23 | import pexpect |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 24 | import re |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 25 | |
| 26 | from drivers.component import Component |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 27 | |
| 28 | |
| 29 | class CLI( Component ): |
| 30 | |
| 31 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 32 | This will define common functions for CLI included. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 33 | """ |
| 34 | def __init__( self ): |
| 35 | super( Component, self ).__init__() |
| 36 | |
| 37 | def connect( self, **connectargs ): |
| 38 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 39 | Connection will establish to the remote host using ssh. |
| 40 | It will take user_name ,ip_address and password as arguments<br> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 41 | and will return the handle. |
| 42 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 43 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 44 | vars( self )[ key ] = connectargs[ key ] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 45 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 46 | connect_result = super( CLI, self ).connect() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 47 | ssh_newkey = 'Are you sure you want to continue connecting' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 48 | refused = "ssh: connect to host " + \ |
| 49 | self.ip_address + " port 22: Connection refused" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 50 | if self.port: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 51 | self.handle = pexpect.spawn( |
| 52 | 'ssh -p ' + |
| 53 | self.port + |
| 54 | ' ' + |
| 55 | self.user_name + |
| 56 | '@' + |
| 57 | self.ip_address, |
Jon Hall | 9aaba88 | 2015-01-19 15:05:15 -0800 | [diff] [blame] | 58 | env={ "TERM": "xterm-mono" }, |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 59 | maxread=50000 ) |
| 60 | else: |
| 61 | self.handle = pexpect.spawn( |
| 62 | 'ssh -X ' + |
| 63 | self.user_name + |
| 64 | '@' + |
| 65 | self.ip_address, |
Jon Hall | 9aaba88 | 2015-01-19 15:05:15 -0800 | [diff] [blame] | 66 | env={ "TERM": "xterm-mono" }, |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 67 | maxread=1000000, |
| 68 | timeout=60 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 69 | |
Jon Hall | 73057ee | 2016-08-23 09:57:26 -0700 | [diff] [blame] | 70 | # set tty window size |
| 71 | self.handle.setwinsize( 24, 250 ) |
| 72 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 73 | self.handle.logfile = self.logfile_handler |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 74 | i = 5 |
| 75 | while i == 5: |
| 76 | i = self.handle.expect( [ |
acsmars | e2be32c | 2015-06-29 16:16:28 -0700 | [diff] [blame] | 77 | ssh_newkey, |
Jon Hall | 05f8868 | 2015-06-09 14:57:53 -0700 | [diff] [blame] | 78 | 'password:|Password:', |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 79 | pexpect.EOF, |
| 80 | pexpect.TIMEOUT, |
| 81 | refused, |
| 82 | 'teston>', |
| 83 | '>|#|\$' ], |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 84 | 120 ) |
acsmars | 32de0bc | 2015-06-30 09:57:12 -0700 | [diff] [blame] | 85 | if i == 0: # Accept key, then expect either a password prompt or access |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 86 | main.log.info( "ssh key confirmation received, send yes" ) |
| 87 | self.handle.sendline( 'yes' ) |
acsmars | 32de0bc | 2015-06-30 09:57:12 -0700 | [diff] [blame] | 88 | i = 5 # Run the loop again |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 89 | continue |
| 90 | if i == 1: # Password required |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 91 | if self.pwd: |
| 92 | main.log.info( |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 93 | "ssh connection asked for password, gave password" ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 94 | else: |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 95 | main.log.info( "Server asked for password, but none was " |
| 96 | "given in the .topo file. Trying " |
| 97 | "no password.") |
| 98 | self.pwd = "" |
| 99 | self.handle.sendline( self.pwd ) |
| 100 | j = self.handle.expect( [ |
| 101 | '>|#|\$', |
| 102 | 'password:|Password:', |
| 103 | pexpect.EOF, |
| 104 | pexpect.TIMEOUT ], |
| 105 | 120 ) |
| 106 | if j != 0: |
| 107 | main.log.error( "Incorrect Password" ) |
| 108 | return main.FALSE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 109 | elif i == 2: |
| 110 | main.log.error( "Connection timeout" ) |
| 111 | return main.FALSE |
| 112 | elif i == 3: # timeout |
| 113 | main.log.error( |
| 114 | "No route to the Host " + |
| 115 | self.user_name + |
| 116 | "@" + |
| 117 | self.ip_address ) |
| 118 | return main.FALSE |
| 119 | elif i == 4: |
| 120 | main.log.error( |
| 121 | "ssh: connect to host " + |
| 122 | self.ip_address + |
| 123 | " port 22: Connection refused" ) |
| 124 | return main.FALSE |
| 125 | elif i == 6: |
| 126 | main.log.info( "Password not required logged in" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 127 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 128 | self.handle.sendline( "" ) |
| 129 | self.handle.expect( '>|#|\$' ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 130 | return self.handle |
| 131 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 132 | def disconnect( self ): |
| 133 | result = super( CLI, self ).disconnect( self ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 134 | result = main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 135 | # self.execute( cmd="exit",timeout=120,prompt="(.*)" ) |
| 136 | |
| 137 | def execute( self, **execparams ): |
| 138 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 139 | It facilitates the command line execution of a given command. It has arguments as : |
| 140 | cmd => represents command to be executed, |
| 141 | prompt => represents expect command prompt or output, |
| 142 | timeout => timeout for command execution, |
| 143 | more => to provide a key press if it is on. |
| 144 | |
| 145 | It will return output of command exection. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 146 | """ |
| 147 | result = super( CLI, self ).execute( self ) |
admin | aef0055 | 2014-05-08 09:18:36 -0700 | [diff] [blame] | 148 | defaultPrompt = '.*[$>\#]' |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 149 | args = utilities.parse_args( [ "CMD", |
| 150 | "TIMEOUT", |
| 151 | "PROMPT", |
| 152 | "MORE" ], |
| 153 | **execparams ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 154 | |
| 155 | expectPrompt = args[ "PROMPT" ] if args[ "PROMPT" ] else defaultPrompt |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 156 | self.LASTRSP = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 157 | timeoutVar = args[ "TIMEOUT" ] if args[ "TIMEOUT" ] else 10 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 158 | cmd = '' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 159 | if args[ "CMD" ]: |
| 160 | cmd = args[ "CMD" ] |
| 161 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 162 | return 0 |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 163 | if args[ "MORE" ] is None: |
| 164 | args[ "MORE" ] = " " |
| 165 | self.handle.sendline( cmd ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 166 | self.lastCommand = cmd |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 167 | index = self.handle.expect( [ expectPrompt, |
| 168 | "--More--", |
| 169 | 'Command not found.', |
| 170 | pexpect.TIMEOUT, |
| 171 | "^:$" ], |
| 172 | timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 173 | if index == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 174 | self.LASTRSP = self.LASTRSP + \ |
| 175 | self.handle.before + self.handle.after |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 176 | main.log.info( "Executed :" + str(cmd ) + |
| 177 | " \t\t Expected Prompt '" + str( expectPrompt) + |
| 178 | "' Found" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 179 | elif index == 1: |
| 180 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 181 | self.handle.send( args[ "MORE" ] ) |
| 182 | main.log.info( |
| 183 | "Found More screen to go , Sending a key to proceed" ) |
| 184 | indexMore = self.handle.expect( |
| 185 | [ "--More--", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 186 | while indexMore == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 187 | main.log.info( |
| 188 | "Found anoother More screen to go , Sending a key to proceed" ) |
| 189 | self.handle.send( args[ "MORE" ] ) |
| 190 | indexMore = self.handle.expect( |
| 191 | [ "--More--", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 192 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 193 | elif index == 2: |
| 194 | main.log.error( "Command not found" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 195 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 196 | elif index == 3: |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 197 | main.log.error( "Expected Prompt not found, Time Out!!" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 198 | main.log.error( expectPrompt ) |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 199 | self.LASTRSP = self.LASTRSP + self.handle.before |
| 200 | return self.LASTRSP |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 201 | elif index == 4: |
| 202 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 203 | # self.handle.send( args[ "MORE" ] ) |
| 204 | self.handle.sendcontrol( "D" ) |
| 205 | main.log.info( |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 206 | "Found More screen to go, Sending a key to proceed" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 207 | indexMore = self.handle.expect( |
| 208 | [ "^:$", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 209 | while indexMore == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 210 | main.log.info( |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 211 | "Found another More screen to go, Sending a key to proceed" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 212 | self.handle.sendcontrol( "D" ) |
| 213 | indexMore = self.handle.expect( |
| 214 | [ "^:$", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 215 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 216 | main.last_response = self.remove_contol_chars( self.LASTRSP ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 217 | return self.LASTRSP |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 218 | |
| 219 | def remove_contol_chars( self, response ): |
| 220 | # RE_XML_ILLEGAL = '([\u0000-\u0008\u000b-\u000c\u000e-\u001f\ufffe-\uffff])|([%s-%s][^%s-%s])|([^%s-%s][%s-%s])|([%s-%s]$)|(^[%s-%s])'%( unichr( 0xd800 ),unichr( 0xdbff ),unichr( 0xdc00 ),unichr( 0xdfff ),unichr( 0xd800 ),unichr( 0xdbff ),unichr( 0xdc00 ),unichr( 0xdfff ),unichr( 0xd800 ),unichr( 0xdbff ),unichr( 0xdc00 ),unichr( 0xdfff ) ) |
| 221 | # response = re.sub( RE_XML_ILLEGAL, "\n", response ) |
| 222 | response = re.sub( r"[\x01-\x1F\x7F]", "", response ) |
| 223 | # response = re.sub( r"\[\d+\;1H", "\n", response ) |
| 224 | response = re.sub( r"\[\d+\;\d+H", "", response ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 225 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 226 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 227 | def runAsSudoUser( self, handle, pwd, default ): |
| 228 | |
| 229 | i = handle.expect( [ ".ssword:*", default, pexpect.EOF ] ) |
| 230 | if i == 0: |
| 231 | handle.sendline( pwd ) |
Jon Hall | 5ec6b1b | 2015-09-17 18:20:14 -0700 | [diff] [blame] | 232 | handle.sendline( "\n" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 233 | |
| 234 | if i == 1: |
| 235 | handle.expect( default ) |
| 236 | |
| 237 | if i == 2: |
| 238 | main.log.error( "Unable to run as Sudo user" ) |
| 239 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 240 | return handle |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 241 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 242 | def onfail( self ): |
| 243 | if 'onfail' in main.componentDictionary[ self.name ]: |
| 244 | commandList = main.componentDictionary[ |
| 245 | self.name ][ 'onfail' ].split( "," ) |
| 246 | for command in commandList: |
| 247 | response = self.execute( |
| 248 | cmd=command, |
| 249 | prompt="(.*)", |
| 250 | timeout=120 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 251 | |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 252 | def secureCopy( self, userName, ipAddress, filePath, dstPath, pwd="", |
| 253 | direction="from" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 254 | """ |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 255 | Definition: |
| 256 | Execute scp command in linux to copy to/from a remote host |
| 257 | Required: |
| 258 | str userName - User name of the remote host |
| 259 | str ipAddress - IP address of the remote host |
| 260 | str filePath - File path including the file it self |
| 261 | str dstPath - Destination path |
| 262 | Optional: |
| 263 | str pwd - Password of the host |
| 264 | str direction - Direction of the scp, default to "from" which means |
| 265 | copy "from" the remote machine to local machine, |
| 266 | while "to" means copy "to" the remote machine from |
| 267 | local machine |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 268 | """ |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 269 | returnVal = main.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 270 | ssh_newkey = 'Are you sure you want to continue connecting' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 271 | refused = "ssh: connect to host " + \ |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 272 | ipAddress + " port 22: Connection refused" |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 273 | |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 274 | if direction == "from": |
| 275 | cmd = 'scp ' + str( userName ) + '@' + str( ipAddress ) + ':' + \ |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 276 | str( filePath ) + ' ' + str( dstPath ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 277 | elif direction == "to": |
| 278 | cmd = 'scp ' + str( filePath ) + ' ' + str( userName ) + \ |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 279 | '@' + str( ipAddress ) + ':' + str( dstPath ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 280 | else: |
| 281 | main.log.debug( "Wrong direction using secure copy command!" ) |
| 282 | return main.FALSE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 283 | |
| 284 | main.log.info( "Sending: " + cmd ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 285 | self.handle.sendline( cmd ) |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 286 | i = 0 |
| 287 | while i < 2: |
| 288 | i = self.handle.expect( [ |
| 289 | ssh_newkey, |
| 290 | 'password:', |
| 291 | "100%", |
| 292 | refused, |
| 293 | "No such file or directory", |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 294 | "Permission denied", |
| 295 | "\$", |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 296 | pexpect.EOF, |
| 297 | pexpect.TIMEOUT ], |
| 298 | 120 ) |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 299 | if i == 0: # ask for ssh key confirmation |
| 300 | main.log.info( "ssh key confirmation received, sending yes" ) |
| 301 | self.handle.sendline( 'yes' ) |
| 302 | elif i == 1: # Asked for ssh password |
| 303 | main.log.info( "ssh connection asked for password, gave password" ) |
| 304 | self.handle.sendline( pwd ) |
| 305 | elif i == 2: # File finished transfering |
| 306 | main.log.info( "Secure copy successful" ) |
| 307 | returnVal = main.TRUE |
| 308 | elif i == 3: # Connection refused |
| 309 | main.log.error( |
| 310 | "ssh: connect to host " + |
| 311 | ipAddress + |
| 312 | " port 22: Connection refused" ) |
| 313 | returnVal = main.FALSE |
| 314 | elif i == 4: # File Not found |
| 315 | main.log.error( "No such file found" ) |
| 316 | returnVal = main.FALSE |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 317 | elif i == 5: # Permission denied |
| 318 | main.log.error( "Permission denied. Check folder permissions" ) |
| 319 | returnVal = main.FALSE |
| 320 | elif i == 6: # prompt returned |
| 321 | return returnVal |
| 322 | elif i == 7: # EOF |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 323 | main.log.error( "Pexpect.EOF found!!!" ) |
| 324 | main.cleanup() |
| 325 | main.exit() |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 326 | elif i == 8: # timeout |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 327 | main.log.error( |
| 328 | "No route to the Host " + |
| 329 | userName + |
| 330 | "@" + |
| 331 | ipAddress ) |
| 332 | returnVal = main.FALSE |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 333 | self.handle.expect( "\$" ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 334 | return returnVal |
| 335 | |
| 336 | def scp( self, remoteHost, filePath, dstPath, direction="from" ): |
| 337 | """ |
| 338 | Definition: |
| 339 | Execute scp command in linux to copy to/from a remote host |
| 340 | Required: |
| 341 | * remoteHost - Test ON component to be parsed |
| 342 | str filePath - File path including the file it self |
| 343 | str dstPath - Destination path |
| 344 | Optional: |
| 345 | str direction - Direction of the scp, default to "from" which means |
| 346 | copy "from" the remote machine to local machine, |
| 347 | while "to" means copy "to" the remote machine from |
| 348 | local machine |
| 349 | """ |
| 350 | return self.secureCopy( remoteHost.user_name, |
| 351 | remoteHost.ip_address, |
| 352 | filePath, |
| 353 | dstPath, |
| 354 | pwd=remoteHost.pwd, |
| 355 | direction=direction ) |