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 | |
| 70 | self.handle.logfile = self.logfile_handler |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 71 | i = 5 |
| 72 | while i == 5: |
| 73 | i = self.handle.expect( [ |
acsmars | e2be32c | 2015-06-29 16:16:28 -0700 | [diff] [blame] | 74 | ssh_newkey, |
Jon Hall | 05f8868 | 2015-06-09 14:57:53 -0700 | [diff] [blame] | 75 | 'password:|Password:', |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 76 | pexpect.EOF, |
| 77 | pexpect.TIMEOUT, |
| 78 | refused, |
| 79 | 'teston>', |
| 80 | '>|#|\$' ], |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 81 | 120 ) |
acsmars | 32de0bc | 2015-06-30 09:57:12 -0700 | [diff] [blame] | 82 | if i == 0: # Accept key, then expect either a password prompt or access |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 83 | main.log.info( "ssh key confirmation received, send yes" ) |
| 84 | self.handle.sendline( 'yes' ) |
acsmars | 32de0bc | 2015-06-30 09:57:12 -0700 | [diff] [blame] | 85 | i = 5 # Run the loop again |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 86 | continue |
| 87 | if i == 1: # Password required |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 88 | if self.pwd: |
| 89 | main.log.info( |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 90 | "ssh connection asked for password, gave password" ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 91 | else: |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 92 | main.log.info( "Server asked for password, but none was " |
| 93 | "given in the .topo file. Trying " |
| 94 | "no password.") |
| 95 | self.pwd = "" |
| 96 | self.handle.sendline( self.pwd ) |
| 97 | j = self.handle.expect( [ |
| 98 | '>|#|\$', |
| 99 | 'password:|Password:', |
| 100 | pexpect.EOF, |
| 101 | pexpect.TIMEOUT ], |
| 102 | 120 ) |
| 103 | if j != 0: |
| 104 | main.log.error( "Incorrect Password" ) |
| 105 | return main.FALSE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 106 | elif i == 2: |
| 107 | main.log.error( "Connection timeout" ) |
| 108 | return main.FALSE |
| 109 | elif i == 3: # timeout |
| 110 | main.log.error( |
| 111 | "No route to the Host " + |
| 112 | self.user_name + |
| 113 | "@" + |
| 114 | self.ip_address ) |
| 115 | return main.FALSE |
| 116 | elif i == 4: |
| 117 | main.log.error( |
| 118 | "ssh: connect to host " + |
| 119 | self.ip_address + |
| 120 | " port 22: Connection refused" ) |
| 121 | return main.FALSE |
| 122 | elif i == 6: |
| 123 | main.log.info( "Password not required logged in" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 124 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 125 | self.handle.sendline( "" ) |
| 126 | self.handle.expect( '>|#|\$' ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 127 | return self.handle |
| 128 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 129 | def disconnect( self ): |
| 130 | result = super( CLI, self ).disconnect( self ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 131 | result = main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 132 | # self.execute( cmd="exit",timeout=120,prompt="(.*)" ) |
| 133 | |
| 134 | def execute( self, **execparams ): |
| 135 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 136 | It facilitates the command line execution of a given command. It has arguments as : |
| 137 | cmd => represents command to be executed, |
| 138 | prompt => represents expect command prompt or output, |
| 139 | timeout => timeout for command execution, |
| 140 | more => to provide a key press if it is on. |
| 141 | |
| 142 | It will return output of command exection. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 143 | """ |
| 144 | result = super( CLI, self ).execute( self ) |
admin | aef0055 | 2014-05-08 09:18:36 -0700 | [diff] [blame] | 145 | defaultPrompt = '.*[$>\#]' |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 146 | args = utilities.parse_args( [ "CMD", |
| 147 | "TIMEOUT", |
| 148 | "PROMPT", |
| 149 | "MORE" ], |
| 150 | **execparams ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 151 | |
| 152 | expectPrompt = args[ "PROMPT" ] if args[ "PROMPT" ] else defaultPrompt |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 153 | self.LASTRSP = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 154 | timeoutVar = args[ "TIMEOUT" ] if args[ "TIMEOUT" ] else 10 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 155 | cmd = '' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 156 | if args[ "CMD" ]: |
| 157 | cmd = args[ "CMD" ] |
| 158 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 159 | return 0 |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 160 | if args[ "MORE" ] is None: |
| 161 | args[ "MORE" ] = " " |
| 162 | self.handle.sendline( cmd ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 163 | self.lastCommand = cmd |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 164 | index = self.handle.expect( [ expectPrompt, |
| 165 | "--More--", |
| 166 | 'Command not found.', |
| 167 | pexpect.TIMEOUT, |
| 168 | "^:$" ], |
| 169 | timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 170 | if index == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 171 | self.LASTRSP = self.LASTRSP + \ |
| 172 | self.handle.before + self.handle.after |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 173 | main.log.info( "Executed :" + str(cmd ) + |
| 174 | " \t\t Expected Prompt '" + str( expectPrompt) + |
| 175 | "' Found" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 176 | elif index == 1: |
| 177 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 178 | self.handle.send( args[ "MORE" ] ) |
| 179 | main.log.info( |
| 180 | "Found More screen to go , Sending a key to proceed" ) |
| 181 | indexMore = self.handle.expect( |
| 182 | [ "--More--", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 183 | while indexMore == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 184 | main.log.info( |
| 185 | "Found anoother More screen to go , Sending a key to proceed" ) |
| 186 | self.handle.send( args[ "MORE" ] ) |
| 187 | indexMore = self.handle.expect( |
| 188 | [ "--More--", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 189 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 190 | elif index == 2: |
| 191 | main.log.error( "Command not found" ) |
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 == 3: |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 194 | main.log.error( "Expected Prompt not found, Time Out!!" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 195 | main.log.error( expectPrompt ) |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 196 | self.LASTRSP = self.LASTRSP + self.handle.before |
| 197 | return self.LASTRSP |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 198 | elif index == 4: |
| 199 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 200 | # self.handle.send( args[ "MORE" ] ) |
| 201 | self.handle.sendcontrol( "D" ) |
| 202 | main.log.info( |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 203 | "Found More screen to go, Sending a key to proceed" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 204 | indexMore = self.handle.expect( |
| 205 | [ "^:$", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 206 | while indexMore == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 207 | main.log.info( |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 208 | "Found another More screen to go, Sending a key to proceed" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 209 | self.handle.sendcontrol( "D" ) |
| 210 | indexMore = self.handle.expect( |
| 211 | [ "^:$", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 212 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 213 | main.last_response = self.remove_contol_chars( self.LASTRSP ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 214 | return self.LASTRSP |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 215 | |
| 216 | def remove_contol_chars( self, response ): |
| 217 | # 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 ) ) |
| 218 | # response = re.sub( RE_XML_ILLEGAL, "\n", response ) |
| 219 | response = re.sub( r"[\x01-\x1F\x7F]", "", response ) |
| 220 | # response = re.sub( r"\[\d+\;1H", "\n", response ) |
| 221 | response = re.sub( r"\[\d+\;\d+H", "", response ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 222 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 223 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 224 | def runAsSudoUser( self, handle, pwd, default ): |
| 225 | |
| 226 | i = handle.expect( [ ".ssword:*", default, pexpect.EOF ] ) |
| 227 | if i == 0: |
| 228 | handle.sendline( pwd ) |
Jon Hall | 5ec6b1b | 2015-09-17 18:20:14 -0700 | [diff] [blame] | 229 | handle.sendline( "\n" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 230 | |
| 231 | if i == 1: |
| 232 | handle.expect( default ) |
| 233 | |
| 234 | if i == 2: |
| 235 | main.log.error( "Unable to run as Sudo user" ) |
| 236 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 237 | return handle |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 238 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 239 | def onfail( self ): |
| 240 | if 'onfail' in main.componentDictionary[ self.name ]: |
| 241 | commandList = main.componentDictionary[ |
| 242 | self.name ][ 'onfail' ].split( "," ) |
| 243 | for command in commandList: |
| 244 | response = self.execute( |
| 245 | cmd=command, |
| 246 | prompt="(.*)", |
| 247 | timeout=120 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 248 | |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 249 | def secureCopy( self, userName, ipAddress, filePath, dstPath, pwd="", |
| 250 | direction="from" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 251 | """ |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 252 | Definition: |
| 253 | Execute scp command in linux to copy to/from a remote host |
| 254 | Required: |
| 255 | str userName - User name of the remote host |
| 256 | str ipAddress - IP address of the remote host |
| 257 | str filePath - File path including the file it self |
| 258 | str dstPath - Destination path |
| 259 | Optional: |
| 260 | str pwd - Password of the host |
| 261 | str direction - Direction of the scp, default to "from" which means |
| 262 | copy "from" the remote machine to local machine, |
| 263 | while "to" means copy "to" the remote machine from |
| 264 | local machine |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 265 | """ |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 266 | returnVal = main.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 267 | ssh_newkey = 'Are you sure you want to continue connecting' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 268 | refused = "ssh: connect to host " + \ |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 269 | ipAddress + " port 22: Connection refused" |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 270 | |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 271 | if direction == "from": |
| 272 | cmd = 'scp ' + str( userName ) + '@' + str( ipAddress ) + ':' + \ |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 273 | str( filePath ) + ' ' + str( dstPath ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 274 | elif direction == "to": |
| 275 | cmd = 'scp ' + str( filePath ) + ' ' + str( userName ) + \ |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 276 | '@' + str( ipAddress ) + ':' + str( dstPath ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 277 | else: |
| 278 | main.log.debug( "Wrong direction using secure copy command!" ) |
| 279 | return main.FALSE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 280 | |
| 281 | main.log.info( "Sending: " + cmd ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 282 | self.handle.sendline( cmd ) |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 283 | i = 0 |
| 284 | while i < 2: |
| 285 | i = self.handle.expect( [ |
| 286 | ssh_newkey, |
| 287 | 'password:', |
| 288 | "100%", |
| 289 | refused, |
| 290 | "No such file or directory", |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 291 | "Permission denied", |
| 292 | "\$", |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 293 | pexpect.EOF, |
| 294 | pexpect.TIMEOUT ], |
| 295 | 120 ) |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 296 | if i == 0: # ask for ssh key confirmation |
| 297 | main.log.info( "ssh key confirmation received, sending yes" ) |
| 298 | self.handle.sendline( 'yes' ) |
| 299 | elif i == 1: # Asked for ssh password |
| 300 | main.log.info( "ssh connection asked for password, gave password" ) |
| 301 | self.handle.sendline( pwd ) |
| 302 | elif i == 2: # File finished transfering |
| 303 | main.log.info( "Secure copy successful" ) |
| 304 | returnVal = main.TRUE |
| 305 | elif i == 3: # Connection refused |
| 306 | main.log.error( |
| 307 | "ssh: connect to host " + |
| 308 | ipAddress + |
| 309 | " port 22: Connection refused" ) |
| 310 | returnVal = main.FALSE |
| 311 | elif i == 4: # File Not found |
| 312 | main.log.error( "No such file found" ) |
| 313 | returnVal = main.FALSE |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 314 | elif i == 5: # Permission denied |
| 315 | main.log.error( "Permission denied. Check folder permissions" ) |
| 316 | returnVal = main.FALSE |
| 317 | elif i == 6: # prompt returned |
| 318 | return returnVal |
| 319 | elif i == 7: # EOF |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 320 | main.log.error( "Pexpect.EOF found!!!" ) |
| 321 | main.cleanup() |
| 322 | main.exit() |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 323 | elif i == 8: # timeout |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 324 | main.log.error( |
| 325 | "No route to the Host " + |
| 326 | userName + |
| 327 | "@" + |
| 328 | ipAddress ) |
| 329 | returnVal = main.FALSE |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 330 | self.handle.expect( "\$" ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 331 | return returnVal |
| 332 | |
| 333 | def scp( self, remoteHost, filePath, dstPath, direction="from" ): |
| 334 | """ |
| 335 | Definition: |
| 336 | Execute scp command in linux to copy to/from a remote host |
| 337 | Required: |
| 338 | * remoteHost - Test ON component to be parsed |
| 339 | str filePath - File path including the file it self |
| 340 | str dstPath - Destination path |
| 341 | Optional: |
| 342 | str direction - Direction of the scp, default to "from" which means |
| 343 | copy "from" the remote machine to local machine, |
| 344 | while "to" means copy "to" the remote machine from |
| 345 | local machine |
| 346 | """ |
| 347 | return self.secureCopy( remoteHost.user_name, |
| 348 | remoteHost.ip_address, |
| 349 | filePath, |
| 350 | dstPath, |
| 351 | pwd=remoteHost.pwd, |
| 352 | direction=direction ) |