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 |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 4 | |
| 5 | author:s: Anil Kumar ( anilkumar.s@paxterrasolutions.com ), |
| 6 | Raghav Kashyap( raghavkashyap@paxterrasolutions.com ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 7 | |
| 8 | |
| 9 | TestON is free software: you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation, either version 2 of the License, or |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 12 | ( at your option ) any later version. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 13 | |
| 14 | TestON is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 20 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 21 | |
| 22 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 23 | |
| 24 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 25 | import pexpect |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 26 | import struct |
| 27 | import fcntl |
| 28 | import os |
| 29 | import sys |
| 30 | import signal |
| 31 | import sys |
| 32 | import re |
| 33 | sys.path.append( "../" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 34 | |
| 35 | from drivers.component import Component |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 36 | |
| 37 | |
| 38 | class CLI( Component ): |
| 39 | |
| 40 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 41 | This will define common functions for CLI included. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 42 | """ |
| 43 | def __init__( self ): |
| 44 | super( Component, self ).__init__() |
| 45 | |
| 46 | def connect( self, **connectargs ): |
| 47 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 48 | Connection will establish to the remote host using ssh. |
| 49 | It will take user_name ,ip_address and password as arguments<br> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 50 | and will return the handle. |
| 51 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 52 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 53 | vars( self )[ key ] = connectargs[ key ] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 54 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 55 | connect_result = super( CLI, self ).connect() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 56 | ssh_newkey = 'Are you sure you want to continue connecting' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 57 | refused = "ssh: connect to host " + \ |
| 58 | self.ip_address + " port 22: Connection refused" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 59 | if self.port: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 60 | self.handle = pexpect.spawn( |
| 61 | 'ssh -p ' + |
| 62 | self.port + |
| 63 | ' ' + |
| 64 | self.user_name + |
| 65 | '@' + |
| 66 | self.ip_address, |
Jon Hall | 9aaba88 | 2015-01-19 15:05:15 -0800 | [diff] [blame] | 67 | env={ "TERM": "xterm-mono" }, |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 68 | maxread=50000 ) |
| 69 | else: |
| 70 | self.handle = pexpect.spawn( |
| 71 | 'ssh -X ' + |
| 72 | self.user_name + |
| 73 | '@' + |
| 74 | self.ip_address, |
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 | |
| 79 | self.handle.logfile = self.logfile_handler |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 80 | i = 5 |
| 81 | while i == 5: |
| 82 | i = self.handle.expect( [ |
| 83 | ssh_newkey, |
Jon Hall | 21270ac | 2015-02-16 17:59:55 -0800 | [diff] [blame] | 84 | 'password:', |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 85 | pexpect.EOF, |
| 86 | pexpect.TIMEOUT, |
| 87 | refused, |
| 88 | 'teston>', |
| 89 | '>|#|\$' ], |
| 90 | 120 ) |
| 91 | if i == 0: |
| 92 | main.log.info( "ssh key confirmation received, send yes" ) |
| 93 | self.handle.sendline( 'yes' ) |
| 94 | i = self.handle.expect( |
| 95 | [ ssh_newkey, 'password:', pexpect.EOF ] ) |
| 96 | if i == 1: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 97 | if self.pwd: |
| 98 | main.log.info( |
| 99 | "ssh connection asked for password, gave password" ) |
| 100 | self.handle.sendline( self.pwd ) |
| 101 | self.handle.expect( '>|#|\$' ) |
| 102 | else: |
| 103 | # FIXME: TestON does not support a username having no |
| 104 | # password |
| 105 | main.log.error( "Server asked for password, but none was " |
| 106 | "given in the .topo file" ) |
| 107 | main.exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 108 | elif i == 2: |
| 109 | main.log.error( "Connection timeout" ) |
| 110 | return main.FALSE |
| 111 | elif i == 3: # timeout |
| 112 | main.log.error( |
| 113 | "No route to the Host " + |
| 114 | self.user_name + |
| 115 | "@" + |
| 116 | self.ip_address ) |
| 117 | return main.FALSE |
| 118 | elif i == 4: |
| 119 | main.log.error( |
| 120 | "ssh: connect to host " + |
| 121 | self.ip_address + |
| 122 | " port 22: Connection refused" ) |
| 123 | return main.FALSE |
| 124 | elif i == 6: |
| 125 | main.log.info( "Password not required logged in" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 126 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 127 | self.handle.sendline( "" ) |
| 128 | self.handle.expect( '>|#|\$' ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 129 | return self.handle |
| 130 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 131 | def disconnect( self ): |
| 132 | result = super( CLI, self ).disconnect( self ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 133 | result = main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 134 | # self.execute( cmd="exit",timeout=120,prompt="(.*)" ) |
| 135 | |
| 136 | def execute( self, **execparams ): |
| 137 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 138 | It facilitates the command line execution of a given command. It has arguments as : |
| 139 | cmd => represents command to be executed, |
| 140 | prompt => represents expect command prompt or output, |
| 141 | timeout => timeout for command execution, |
| 142 | more => to provide a key press if it is on. |
| 143 | |
| 144 | It will return output of command exection. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 145 | """ |
| 146 | result = super( CLI, self ).execute( self ) |
admin | aef0055 | 2014-05-08 09:18:36 -0700 | [diff] [blame] | 147 | defaultPrompt = '.*[$>\#]' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 148 | args = utilities.parse_args( [ |
| 149 | "CMD", |
| 150 | "TIMEOUT", |
| 151 | "PROMPT", |
| 152 | "MORE" ], |
| 153 | **execparams ) |
| 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 |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 167 | index = self.handle.expect( [ |
| 168 | expectPrompt, |
| 169 | "--More--", |
| 170 | 'Command not found.', |
| 171 | pexpect.TIMEOUT, |
| 172 | "^:$" ], |
| 173 | timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 174 | if index == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 175 | self.LASTRSP = self.LASTRSP + \ |
| 176 | self.handle.before + self.handle.after |
| 177 | main.log.info( |
| 178 | "Executed :" + str( |
| 179 | cmd ) + " \t\t Expected Prompt '" + str( |
| 180 | expectPrompt) + "' Found" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 181 | elif index == 1: |
| 182 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 183 | self.handle.send( args[ "MORE" ] ) |
| 184 | main.log.info( |
| 185 | "Found More screen to go , Sending a key to proceed" ) |
| 186 | indexMore = self.handle.expect( |
| 187 | [ "--More--", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 188 | while indexMore == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 189 | main.log.info( |
| 190 | "Found anoother More screen to go , Sending a key to proceed" ) |
| 191 | self.handle.send( args[ "MORE" ] ) |
| 192 | indexMore = self.handle.expect( |
| 193 | [ "--More--", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 194 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 195 | elif index == 2: |
| 196 | main.log.error( "Command not found" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 197 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 198 | elif index == 3: |
| 199 | main.log.error( "Expected Prompt not found , Time Out!!" ) |
| 200 | main.log.error( expectPrompt ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 201 | return "Expected Prompt not found , Time Out!!" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 202 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 203 | elif index == 4: |
| 204 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 205 | # self.handle.send( args[ "MORE" ] ) |
| 206 | self.handle.sendcontrol( "D" ) |
| 207 | main.log.info( |
| 208 | "Found More screen to go , Sending a key to proceed" ) |
| 209 | indexMore = self.handle.expect( |
| 210 | [ "^:$", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 211 | while indexMore == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 212 | main.log.info( |
| 213 | "Found another More screen to go , Sending a key to proceed" ) |
| 214 | self.handle.sendcontrol( "D" ) |
| 215 | indexMore = self.handle.expect( |
| 216 | [ "^:$", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 217 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 218 | |
| 219 | main.last_response = self.remove_contol_chars( self.LASTRSP ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 220 | return self.LASTRSP |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 221 | |
| 222 | def remove_contol_chars( self, response ): |
| 223 | # 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 ) ) |
| 224 | # response = re.sub( RE_XML_ILLEGAL, "\n", response ) |
| 225 | response = re.sub( r"[\x01-\x1F\x7F]", "", response ) |
| 226 | # response = re.sub( r"\[\d+\;1H", "\n", response ) |
| 227 | response = re.sub( r"\[\d+\;\d+H", "", response ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 228 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 229 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 230 | def runAsSudoUser( self, handle, pwd, default ): |
| 231 | |
| 232 | i = handle.expect( [ ".ssword:*", default, pexpect.EOF ] ) |
| 233 | if i == 0: |
| 234 | handle.sendline( pwd ) |
| 235 | handle.sendline( "\r" ) |
| 236 | |
| 237 | if i == 1: |
| 238 | handle.expect( default ) |
| 239 | |
| 240 | if i == 2: |
| 241 | main.log.error( "Unable to run as Sudo user" ) |
| 242 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 243 | return handle |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 244 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 245 | def onfail( self ): |
| 246 | if 'onfail' in main.componentDictionary[ self.name ]: |
| 247 | commandList = main.componentDictionary[ |
| 248 | self.name ][ 'onfail' ].split( "," ) |
| 249 | for command in commandList: |
| 250 | response = self.execute( |
| 251 | cmd=command, |
| 252 | prompt="(.*)", |
| 253 | timeout=120 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 254 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 255 | def secureCopy( self, user_name, ip_address, filepath, pwd, dst_path ): |
| 256 | |
| 257 | # scp openflow@192.168.56.101:/home/openflow/sample |
| 258 | # /home/paxterra/Desktop/ |
| 259 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 260 | Connection will establish to the remote host using ssh. |
| 261 | It will take user_name ,ip_address and password as arguments<br> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 262 | and will return the handle. |
| 263 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 264 | ssh_newkey = 'Are you sure you want to continue connecting' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 265 | refused = "ssh: connect to host " + \ |
| 266 | ip_address + " port 22: Connection refused" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 267 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 268 | cmd = 'scp ' + str( user_name ) + '@' + str( ip_address ) + ':' + \ |
| 269 | str( filepath ) + ' ' + str(dst_path ) |
| 270 | |
| 271 | main.log.info( "Sending: " + cmd ) |
| 272 | self.handle = pexpect.spawn( cmd ) |
| 273 | i = self.handle.expect( [ |
| 274 | ssh_newkey, |
| 275 | 'password:', |
| 276 | pexpect.EOF, |
| 277 | pexpect.TIMEOUT, |
| 278 | refused ], |
| 279 | 120 ) |
| 280 | |
| 281 | if i == 0: |
| 282 | main.log.info( "ssh key confirmation received, send yes" ) |
| 283 | self.handle.sendline( 'yes' ) |
| 284 | i = self.handle.expect( [ ssh_newkey, 'password:', pexpect.EOF ] ) |
| 285 | if i == 1: |
| 286 | main.log.info( "ssh connection asked for password, gave password" ) |
| 287 | self.handle.sendline( pwd ) |
| 288 | # self.handle.expect( user_name ) |
| 289 | |
| 290 | elif i == 2: |
| 291 | main.log.error( "Connection timeout" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 292 | pass |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 293 | elif i == 3: # timeout |
| 294 | main.log.error( |
| 295 | "No route to the Host " + |
| 296 | user_name + |
| 297 | "@" + |
| 298 | ip_address ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 299 | return main.FALSE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 300 | elif i == 4: |
| 301 | main.log.error( |
| 302 | "ssh: connect to host " + |
| 303 | ip_address + |
| 304 | " port 22: Connection refused" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 305 | return main.FALSE |
| 306 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 307 | self.handle.sendline( "" ) |
| 308 | self.handle.expect( "$" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 309 | print self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 310 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 311 | return self.handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 312 | |