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