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 Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4 | Copyright 2012 Open Networking Foundation (ONF) |
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 |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [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 ): |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 35 | super( CLI, self ).__init__() |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 36 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 37 | def checkPrompt( self ): |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 38 | for key in self.options: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 39 | if key == "prompt" and self.options[ 'prompt' ] is not None: |
| 40 | self.prompt = self.options[ 'prompt' ] |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 41 | break |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 42 | |
| 43 | def connect( self, **connectargs ): |
| 44 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 45 | Connection will establish to the remote host using ssh. |
| 46 | It will take user_name ,ip_address and password as arguments<br> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 47 | and will return the handle. |
| 48 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 49 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 50 | vars( self )[ key ] = connectargs[ key ] |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 51 | self.checkPrompt() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 52 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 53 | connect_result = super( CLI, self ).connect() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 54 | ssh_newkey = 'Are you sure you want to continue connecting' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 55 | refused = "ssh: connect to host " + \ |
| 56 | self.ip_address + " port 22: Connection refused" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 57 | if self.port: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 58 | self.handle = pexpect.spawn( |
Jon Hall | 7a5e0a2 | 2017-12-11 10:35:50 -0800 | [diff] [blame] | 59 | 'ssh -X -p ' + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 60 | self.port + |
| 61 | ' ' + |
| 62 | self.user_name + |
| 63 | '@' + |
Jon Hall | 7a5e0a2 | 2017-12-11 10:35:50 -0800 | [diff] [blame] | 64 | self.ip_address + |
| 65 | ' -o ServerAliveInterval=120 -o TCPKeepAlive=yes', |
Jon Hall | 9aaba88 | 2015-01-19 15:05:15 -0800 | [diff] [blame] | 66 | env={ "TERM": "xterm-mono" }, |
Jon Hall | 7a5e0a2 | 2017-12-11 10:35:50 -0800 | [diff] [blame] | 67 | maxread=1000000 ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 68 | else: |
| 69 | self.handle = pexpect.spawn( |
| 70 | 'ssh -X ' + |
| 71 | self.user_name + |
| 72 | '@' + |
Jon Hall | 7a5e0a2 | 2017-12-11 10:35:50 -0800 | [diff] [blame] | 73 | self.ip_address + |
| 74 | ' -o ServerAliveInterval=120 -o TCPKeepAlive=yes', |
Jon Hall | 9aaba88 | 2015-01-19 15:05:15 -0800 | [diff] [blame] | 75 | env={ "TERM": "xterm-mono" }, |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 76 | maxread=1000000, |
| 77 | timeout=60 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 78 | |
Jon Hall | 73057ee | 2016-08-23 09:57:26 -0700 | [diff] [blame] | 79 | # set tty window size |
| 80 | self.handle.setwinsize( 24, 250 ) |
| 81 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 82 | self.handle.logfile = self.logfile_handler |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 83 | i = 5 |
| 84 | while i == 5: |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 85 | i = self.handle.expect( [ ssh_newkey, |
| 86 | 'password:|Password:', |
| 87 | pexpect.EOF, |
| 88 | pexpect.TIMEOUT, |
| 89 | refused, |
| 90 | 'teston>', |
| 91 | self.prompt ], |
| 92 | 120 ) |
acsmars | 32de0bc | 2015-06-30 09:57:12 -0700 | [diff] [blame] | 93 | if i == 0: # Accept key, then expect either a password prompt or access |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 94 | main.log.info( "ssh key confirmation received, send yes" ) |
| 95 | self.handle.sendline( 'yes' ) |
acsmars | 32de0bc | 2015-06-30 09:57:12 -0700 | [diff] [blame] | 96 | i = 5 # Run the loop again |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 97 | continue |
| 98 | if i == 1: # Password required |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 99 | if self.pwd: |
| 100 | main.log.info( |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 101 | "ssh connection asked for password, gave password" ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 102 | else: |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 103 | main.log.info( "Server asked for password, but none was " |
| 104 | "given in the .topo file. Trying " |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 105 | "no password." ) |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 106 | self.pwd = "" |
| 107 | self.handle.sendline( self.pwd ) |
| 108 | j = self.handle.expect( [ |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 109 | self.prompt, |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 110 | 'password:|Password:', |
| 111 | pexpect.EOF, |
| 112 | pexpect.TIMEOUT ], |
| 113 | 120 ) |
| 114 | if j != 0: |
| 115 | main.log.error( "Incorrect Password" ) |
| 116 | return main.FALSE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 117 | elif i == 2: |
| 118 | main.log.error( "Connection timeout" ) |
| 119 | return main.FALSE |
| 120 | elif i == 3: # timeout |
| 121 | main.log.error( |
| 122 | "No route to the Host " + |
| 123 | self.user_name + |
| 124 | "@" + |
| 125 | self.ip_address ) |
| 126 | return main.FALSE |
| 127 | elif i == 4: |
| 128 | main.log.error( |
| 129 | "ssh: connect to host " + |
| 130 | self.ip_address + |
| 131 | " port 22: Connection refused" ) |
| 132 | return main.FALSE |
| 133 | elif i == 6: |
| 134 | main.log.info( "Password not required logged in" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 135 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 136 | self.handle.sendline( "" ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 137 | self.handle.expect( self.prompt ) |
Jeremy Ronquillo | 0f2008a | 2017-06-23 15:32:51 -0700 | [diff] [blame] | 138 | self.handle.sendline( "cd" ) |
| 139 | self.handle.expect( self.prompt ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 140 | return self.handle |
| 141 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 142 | def disconnect( self ): |
| 143 | result = super( CLI, self ).disconnect( self ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 144 | result = main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 145 | # self.execute( cmd="exit",timeout=120,prompt="(.*)" ) |
| 146 | |
| 147 | def execute( self, **execparams ): |
| 148 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 149 | It facilitates the command line execution of a given command. It has arguments as : |
| 150 | cmd => represents command to be executed, |
| 151 | prompt => represents expect command prompt or output, |
| 152 | timeout => timeout for command execution, |
| 153 | more => to provide a key press if it is on. |
| 154 | |
| 155 | It will return output of command exection. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 156 | """ |
| 157 | result = super( CLI, self ).execute( self ) |
admin | aef0055 | 2014-05-08 09:18:36 -0700 | [diff] [blame] | 158 | defaultPrompt = '.*[$>\#]' |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 159 | args = utilities.parse_args( [ "CMD", |
| 160 | "TIMEOUT", |
| 161 | "PROMPT", |
| 162 | "MORE" ], |
| 163 | **execparams ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 164 | |
| 165 | expectPrompt = args[ "PROMPT" ] if args[ "PROMPT" ] else defaultPrompt |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 166 | self.LASTRSP = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 167 | timeoutVar = args[ "TIMEOUT" ] if args[ "TIMEOUT" ] else 10 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 168 | cmd = '' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 169 | if args[ "CMD" ]: |
| 170 | cmd = args[ "CMD" ] |
| 171 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 172 | return 0 |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 173 | if args[ "MORE" ] is None: |
| 174 | args[ "MORE" ] = " " |
| 175 | self.handle.sendline( cmd ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 176 | self.lastCommand = cmd |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 177 | index = self.handle.expect( [ expectPrompt, |
| 178 | "--More--", |
| 179 | 'Command not found.', |
| 180 | pexpect.TIMEOUT, |
| 181 | "^:$" ], |
| 182 | timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 183 | if index == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 184 | self.LASTRSP = self.LASTRSP + \ |
| 185 | self.handle.before + self.handle.after |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 186 | main.log.info( "Executed :" + str( cmd ) + |
| 187 | " \t\t Expected Prompt '" + str( expectPrompt ) + |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 188 | "' Found" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 189 | elif index == 1: |
| 190 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 191 | self.handle.send( args[ "MORE" ] ) |
| 192 | main.log.info( |
| 193 | "Found More screen to go , Sending a key to proceed" ) |
| 194 | indexMore = self.handle.expect( |
| 195 | [ "--More--", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 196 | while indexMore == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 197 | main.log.info( |
| 198 | "Found anoother More screen to go , Sending a key to proceed" ) |
| 199 | self.handle.send( args[ "MORE" ] ) |
| 200 | indexMore = self.handle.expect( |
| 201 | [ "--More--", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 202 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 203 | elif index == 2: |
| 204 | main.log.error( "Command not found" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 205 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 206 | elif index == 3: |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 207 | main.log.error( "Expected Prompt not found, Time Out!!" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 208 | main.log.error( expectPrompt ) |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 209 | self.LASTRSP = self.LASTRSP + self.handle.before |
| 210 | return self.LASTRSP |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 211 | elif index == 4: |
| 212 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 213 | # self.handle.send( args[ "MORE" ] ) |
| 214 | self.handle.sendcontrol( "D" ) |
| 215 | main.log.info( |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 216 | "Found More screen to go, Sending a key to proceed" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 217 | indexMore = self.handle.expect( |
| 218 | [ "^:$", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 219 | while indexMore == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 220 | main.log.info( |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 221 | "Found another More screen to go, Sending a key to proceed" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 222 | self.handle.sendcontrol( "D" ) |
| 223 | indexMore = self.handle.expect( |
| 224 | [ "^:$", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 225 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 226 | main.last_response = self.remove_contol_chars( self.LASTRSP ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 227 | return self.LASTRSP |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 228 | |
| 229 | def remove_contol_chars( self, response ): |
| 230 | # 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 ) ) |
| 231 | # response = re.sub( RE_XML_ILLEGAL, "\n", response ) |
| 232 | response = re.sub( r"[\x01-\x1F\x7F]", "", response ) |
| 233 | # response = re.sub( r"\[\d+\;1H", "\n", response ) |
| 234 | response = re.sub( r"\[\d+\;\d+H", "", response ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 235 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 236 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 237 | def runAsSudoUser( self, handle, pwd, default ): |
| 238 | |
| 239 | i = handle.expect( [ ".ssword:*", default, pexpect.EOF ] ) |
| 240 | if i == 0: |
| 241 | handle.sendline( pwd ) |
Jon Hall | 5ec6b1b | 2015-09-17 18:20:14 -0700 | [diff] [blame] | 242 | handle.sendline( "\n" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 243 | |
| 244 | if i == 1: |
| 245 | handle.expect( default ) |
| 246 | |
| 247 | if i == 2: |
| 248 | main.log.error( "Unable to run as Sudo user" ) |
| 249 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 250 | return handle |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 251 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 252 | def onfail( self ): |
| 253 | if 'onfail' in main.componentDictionary[ self.name ]: |
| 254 | commandList = main.componentDictionary[ |
| 255 | self.name ][ 'onfail' ].split( "," ) |
| 256 | for command in commandList: |
| 257 | response = self.execute( |
| 258 | cmd=command, |
| 259 | prompt="(.*)", |
| 260 | timeout=120 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 261 | |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 262 | def secureCopy( self, userName, ipAddress, filePath, dstPath, pwd="", |
| 263 | direction="from" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 264 | """ |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 265 | Definition: |
| 266 | Execute scp command in linux to copy to/from a remote host |
| 267 | Required: |
| 268 | str userName - User name of the remote host |
| 269 | str ipAddress - IP address of the remote host |
| 270 | str filePath - File path including the file it self |
| 271 | str dstPath - Destination path |
| 272 | Optional: |
| 273 | str pwd - Password of the host |
| 274 | str direction - Direction of the scp, default to "from" which means |
| 275 | copy "from" the remote machine to local machine, |
| 276 | while "to" means copy "to" the remote machine from |
| 277 | local machine |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 278 | """ |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 279 | returnVal = main.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 280 | ssh_newkey = 'Are you sure you want to continue connecting' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 281 | refused = "ssh: connect to host " + \ |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 282 | ipAddress + " port 22: Connection refused" |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 283 | |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 284 | if direction == "from": |
| 285 | cmd = 'scp ' + str( userName ) + '@' + str( ipAddress ) + ':' + \ |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 286 | str( filePath ) + ' ' + str( dstPath ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 287 | elif direction == "to": |
| 288 | cmd = 'scp ' + str( filePath ) + ' ' + str( userName ) + \ |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 289 | '@' + str( ipAddress ) + ':' + str( dstPath ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 290 | else: |
| 291 | main.log.debug( "Wrong direction using secure copy command!" ) |
| 292 | return main.FALSE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 293 | |
| 294 | main.log.info( "Sending: " + cmd ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 295 | self.handle.sendline( cmd ) |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 296 | i = 0 |
| 297 | while i < 2: |
| 298 | i = self.handle.expect( [ |
| 299 | ssh_newkey, |
| 300 | 'password:', |
| 301 | "100%", |
| 302 | refused, |
| 303 | "No such file or directory", |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 304 | "Permission denied", |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 305 | self.prompt, |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 306 | pexpect.EOF, |
| 307 | pexpect.TIMEOUT ], |
| 308 | 120 ) |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 309 | if i == 0: # ask for ssh key confirmation |
| 310 | main.log.info( "ssh key confirmation received, sending yes" ) |
| 311 | self.handle.sendline( 'yes' ) |
| 312 | elif i == 1: # Asked for ssh password |
| 313 | main.log.info( "ssh connection asked for password, gave password" ) |
| 314 | self.handle.sendline( pwd ) |
| 315 | elif i == 2: # File finished transfering |
| 316 | main.log.info( "Secure copy successful" ) |
| 317 | returnVal = main.TRUE |
| 318 | elif i == 3: # Connection refused |
| 319 | main.log.error( |
| 320 | "ssh: connect to host " + |
| 321 | ipAddress + |
| 322 | " port 22: Connection refused" ) |
| 323 | returnVal = main.FALSE |
| 324 | elif i == 4: # File Not found |
| 325 | main.log.error( "No such file found" ) |
| 326 | returnVal = main.FALSE |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 327 | elif i == 5: # Permission denied |
| 328 | main.log.error( "Permission denied. Check folder permissions" ) |
| 329 | returnVal = main.FALSE |
| 330 | elif i == 6: # prompt returned |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 331 | return returnVal |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 332 | elif i == 7: # EOF |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 333 | main.log.error( "Pexpect.EOF found!!!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 334 | main.cleanAndExit() |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 335 | elif i == 8: # timeout |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 336 | main.log.error( |
| 337 | "No route to the Host " + |
| 338 | userName + |
| 339 | "@" + |
| 340 | ipAddress ) |
| 341 | returnVal = main.FALSE |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 342 | self.handle.expect( self.prompt ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 343 | return returnVal |
| 344 | |
| 345 | def scp( self, remoteHost, filePath, dstPath, direction="from" ): |
| 346 | """ |
| 347 | Definition: |
| 348 | Execute scp command in linux to copy to/from a remote host |
| 349 | Required: |
| 350 | * remoteHost - Test ON component to be parsed |
| 351 | str filePath - File path including the file it self |
| 352 | str dstPath - Destination path |
| 353 | Optional: |
| 354 | str direction - Direction of the scp, default to "from" which means |
| 355 | copy "from" the remote machine to local machine, |
| 356 | while "to" means copy "to" the remote machine from |
| 357 | local machine |
| 358 | """ |
| 359 | return self.secureCopy( remoteHost.user_name, |
| 360 | remoteHost.ip_address, |
| 361 | filePath, |
| 362 | dstPath, |
| 363 | pwd=remoteHost.pwd, |
| 364 | direction=direction ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 365 | |
| 366 | def sshToNode( self, ipAddress, uName="sdn", pwd="rocks" ): |
| 367 | ssh_newkey = 'Are you sure you want to continue connecting' |
| 368 | refused = "ssh: connect to host " + ipAddress + " port 22: Connection refused" |
| 369 | handle = pexpect.spawn( 'ssh -X ' + |
| 370 | uName + |
| 371 | '@' + |
| 372 | ipAddress, |
| 373 | env={ "TERM": "xterm-mono" }, |
| 374 | maxread=1000000, |
| 375 | timeout=60 ) |
| 376 | |
| 377 | # set tty window size |
| 378 | handle.setwinsize( 24, 250 ) |
| 379 | |
| 380 | i = 5 |
| 381 | while i == 5: |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 382 | i = handle.expect( [ ssh_newkey, |
| 383 | 'password:|Password:', |
| 384 | pexpect.EOF, |
| 385 | pexpect.TIMEOUT, |
| 386 | refused, |
| 387 | 'teston>', |
| 388 | self.prompt ], |
| 389 | 120 ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 390 | if i == 0: # Accept key, then expect either a password prompt or access |
| 391 | main.log.info( "ssh key confirmation received, send yes" ) |
| 392 | handle.sendline( 'yes' ) |
| 393 | i = 5 # Run the loop again |
| 394 | continue |
| 395 | if i == 1: # Password required |
| 396 | if pwd: |
| 397 | main.log.info( |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 398 | "ssh connection asked for password, gave password" ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 399 | else: |
| 400 | main.log.info( "Server asked for password, but none was " |
| 401 | "given in the .topo file. Trying " |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 402 | "no password." ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 403 | pwd = "" |
| 404 | handle.sendline( pwd ) |
| 405 | j = handle.expect( [ self.prompt, |
| 406 | 'password:|Password:', |
| 407 | pexpect.EOF, |
| 408 | pexpect.TIMEOUT ], |
| 409 | 120 ) |
| 410 | if j != 0: |
| 411 | main.log.error( "Incorrect Password" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 412 | main.cleanAndExit() |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 413 | elif i == 2: |
| 414 | main.log.error( "Connection timeout" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 415 | main.cleanAndExit() |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 416 | elif i == 3: # timeout |
| 417 | main.log.error( |
| 418 | "No route to the Host " + |
| 419 | uName + |
| 420 | "@" + |
| 421 | ipAddress ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 422 | main.cleanAndExit() |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 423 | elif i == 4: |
| 424 | main.log.error( |
| 425 | "ssh: connect to host " + |
| 426 | ipAddress + |
| 427 | " port 22: Connection refused" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 428 | main.cleanAndExit() |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 429 | elif i == 6: |
| 430 | main.log.info( "Password not required logged in" ) |
| 431 | |
| 432 | handle.sendline( "" ) |
| 433 | handle.expect( self.prompt ) |
| 434 | handle.sendline( "cd" ) |
| 435 | handle.expect( self.prompt ) |
| 436 | |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 437 | main.log.info( "Successfully ssh to " + ipAddress + "." ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 438 | return handle |
| 439 | |
| 440 | def exitFromSsh( self, handle, ipAddress ): |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 441 | try: |
Jon Hall | 4f360bc | 2017-09-07 10:19:52 -0700 | [diff] [blame] | 442 | handle.sendline( "logout" ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 443 | handle.expect( "closed." ) |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 444 | main.log.info( "Successfully closed ssh connection from " + ipAddress ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 445 | except pexpect.EOF: |
| 446 | main.log.error( "Failed to close the connection from " + ipAddress ) |
Jon Hall | 4f360bc | 2017-09-07 10:19:52 -0700 | [diff] [blame] | 447 | try: |
| 448 | # check that this component handle still works |
| 449 | self.handle.sendline( "" ) |
| 450 | self.handle.expect( self.prompt ) |
| 451 | except pexpect.EOF: |
| 452 | main.log.error( self.handle.before ) |
| 453 | main.log.error( "EOF after closing ssh connection" ) |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 454 | |
| 455 | def folderSize( self, path, size='10', unit='M', ignoreRoot=True ): |
| 456 | """ |
| 457 | Run `du -h` on the folder path and verifies the folder(s) size is |
| 458 | less than the given size. Note that if multiple subdirectories are |
| 459 | present, the result will be the OR of all the individual subdirectories. |
| 460 | |
| 461 | Arguments: |
| 462 | path - A string containing the path supplied to the du command |
| 463 | size - The number portion of the file size that the results will be compared to |
| 464 | unit - The unit portion of the file size that the results will be compared to |
| 465 | ignoreRoot - If True, will ignore the "root" of the path supplied to du. I.E. will ignore `.` |
| 466 | |
| 467 | Returns True if the folder(s) size(s) are less than SIZE UNITS, else returns False |
| 468 | """ |
| 469 | sizeRe = r'(?P<number>\d+\.*\d*)(?P<unit>\D)' |
| 470 | unitsList = [ 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' ] |
| 471 | try: |
| 472 | # make sure we convert units if size is too big |
| 473 | size = float( size ) |
| 474 | if size >= 1000: |
| 475 | size = size / 1000 |
| 476 | unit = unitsList[ unitsList.index( unit + 1 ) ] |
| 477 | cmdStr = "du -h " + path |
| 478 | self.handle.sendline( cmdStr ) |
| 479 | self.handle.expect( self.prompt ) |
| 480 | output = self.handle.before |
| 481 | assert "cannot access" not in output |
| 482 | assert "command not found" not in output |
| 483 | main.log.debug( output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 484 | lines = [ line for line in output.split( '\r\n' ) ] |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 485 | retValue = True |
| 486 | if ignoreRoot: |
| 487 | lastIndex = -2 |
| 488 | else: |
| 489 | lastIndex = -1 |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 490 | for line in lines[ 1:lastIndex ]: |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 491 | parsed = line.split() |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 492 | sizeMatch = parsed[ 0 ] |
| 493 | folder = parsed[ 1 ] |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 494 | match = re.search( sizeRe, sizeMatch ) |
| 495 | num = match.group( 'number' ) |
| 496 | unitMatch = match.group( 'unit' ) |
| 497 | if unitsList.index( unitMatch ) < unitsList.index( unit ): |
| 498 | retValue &= True |
| 499 | elif unitsList.index( unitMatch ) == unitsList.index( unit ): |
| 500 | if float( num ) < float( size ): |
| 501 | retValue &= True |
| 502 | else: |
| 503 | retValue &= False |
| 504 | elif unitsList.index( unitMatch ) > unitsList.index( unit ): |
| 505 | retValue &= False |
| 506 | return retValue |
| 507 | except AssertionError: |
| 508 | main.log.error( self.name + ": Could not execute command: " + output ) |
| 509 | return False |
| 510 | except pexpect.TIMEOUT: |
| 511 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 512 | main.log.error( self.name + ": " + self.handle.before ) |
| 513 | return False |
| 514 | except pexpect.EOF: |
| 515 | main.log.error( self.name + ": EOF exception found" ) |
| 516 | main.log.error( self.name + ": " + self.handle.before ) |
| 517 | main.cleanAndExit() |