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, |
| 67 | env={ |
| 68 | "TERM": "vt100" }, |
| 69 | maxread=50000 ) |
| 70 | else: |
| 71 | self.handle = pexpect.spawn( |
| 72 | 'ssh -X ' + |
| 73 | self.user_name + |
| 74 | '@' + |
| 75 | self.ip_address, |
| 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, |
pingping-lin | 24e79e5 | 2015-01-21 11:39:51 -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: |
| 97 | main.log.info( |
| 98 | "ssh connection asked for password, gave password" ) |
| 99 | self.handle.sendline( self.pwd ) |
| 100 | self.handle.expect( '>|#|\$' ) |
| 101 | elif i == 2: |
| 102 | main.log.error( "Connection timeout" ) |
| 103 | return main.FALSE |
| 104 | elif i == 3: # timeout |
| 105 | main.log.error( |
| 106 | "No route to the Host " + |
| 107 | self.user_name + |
| 108 | "@" + |
| 109 | self.ip_address ) |
| 110 | return main.FALSE |
| 111 | elif i == 4: |
| 112 | main.log.error( |
| 113 | "ssh: connect to host " + |
| 114 | self.ip_address + |
| 115 | " port 22: Connection refused" ) |
| 116 | return main.FALSE |
| 117 | elif i == 6: |
| 118 | main.log.info( "Password not required logged in" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 119 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 120 | self.handle.sendline( "" ) |
| 121 | self.handle.expect( '>|#|\$' ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 122 | return self.handle |
| 123 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 124 | def disconnect( self ): |
| 125 | result = super( CLI, self ).disconnect( self ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 126 | result = main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 127 | # self.execute( cmd="exit",timeout=120,prompt="(.*)" ) |
| 128 | |
| 129 | def execute( self, **execparams ): |
| 130 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 131 | It facilitates the command line execution of a given command. It has arguments as : |
| 132 | cmd => represents command to be executed, |
| 133 | prompt => represents expect command prompt or output, |
| 134 | timeout => timeout for command execution, |
| 135 | more => to provide a key press if it is on. |
| 136 | |
| 137 | It will return output of command exection. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 138 | """ |
| 139 | result = super( CLI, self ).execute( self ) |
admin | aef0055 | 2014-05-08 09:18:36 -0700 | [diff] [blame] | 140 | defaultPrompt = '.*[$>\#]' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 141 | args = utilities.parse_args( [ |
| 142 | "CMD", |
| 143 | "TIMEOUT", |
| 144 | "PROMPT", |
| 145 | "MORE" ], |
| 146 | **execparams ) |
| 147 | |
| 148 | expectPrompt = args[ "PROMPT" ] if args[ "PROMPT" ] else defaultPrompt |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 149 | self.LASTRSP = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 150 | timeoutVar = args[ "TIMEOUT" ] if args[ "TIMEOUT" ] else 10 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 151 | cmd = '' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 152 | if args[ "CMD" ]: |
| 153 | cmd = args[ "CMD" ] |
| 154 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 155 | return 0 |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 156 | if args[ "MORE" ] is None: |
| 157 | args[ "MORE" ] = " " |
| 158 | self.handle.sendline( cmd ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 159 | self.lastCommand = cmd |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 160 | index = self.handle.expect( [ |
| 161 | expectPrompt, |
| 162 | "--More--", |
| 163 | 'Command not found.', |
| 164 | pexpect.TIMEOUT, |
| 165 | "^:$" ], |
| 166 | timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 167 | if index == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 168 | self.LASTRSP = self.LASTRSP + \ |
| 169 | self.handle.before + self.handle.after |
| 170 | main.log.info( |
| 171 | "Executed :" + str( |
| 172 | cmd ) + " \t\t Expected Prompt '" + str( |
| 173 | expectPrompt) + "' Found" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 174 | elif index == 1: |
| 175 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 176 | self.handle.send( args[ "MORE" ] ) |
| 177 | main.log.info( |
| 178 | "Found More screen to go , Sending a key to proceed" ) |
| 179 | indexMore = self.handle.expect( |
| 180 | [ "--More--", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 181 | while indexMore == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 182 | main.log.info( |
| 183 | "Found anoother More screen to go , Sending a key to proceed" ) |
| 184 | self.handle.send( args[ "MORE" ] ) |
| 185 | indexMore = self.handle.expect( |
| 186 | [ "--More--", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 187 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 188 | elif index == 2: |
| 189 | main.log.error( "Command not found" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 190 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 191 | elif index == 3: |
| 192 | main.log.error( "Expected Prompt not found , Time Out!!" ) |
| 193 | main.log.error( expectPrompt ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 194 | return "Expected Prompt not found , Time Out!!" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 195 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 196 | elif index == 4: |
| 197 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 198 | # self.handle.send( args[ "MORE" ] ) |
| 199 | self.handle.sendcontrol( "D" ) |
| 200 | main.log.info( |
| 201 | "Found More screen to go , Sending a key to proceed" ) |
| 202 | indexMore = self.handle.expect( |
| 203 | [ "^:$", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 204 | while indexMore == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 205 | main.log.info( |
| 206 | "Found another More screen to go , Sending a key to proceed" ) |
| 207 | self.handle.sendcontrol( "D" ) |
| 208 | indexMore = self.handle.expect( |
| 209 | [ "^:$", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 210 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 211 | |
| 212 | main.last_response = self.remove_contol_chars( self.LASTRSP ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 213 | return self.LASTRSP |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 214 | |
| 215 | def remove_contol_chars( self, response ): |
| 216 | # 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 ) ) |
| 217 | # response = re.sub( RE_XML_ILLEGAL, "\n", response ) |
| 218 | response = re.sub( r"[\x01-\x1F\x7F]", "", response ) |
| 219 | # response = re.sub( r"\[\d+\;1H", "\n", response ) |
| 220 | response = re.sub( r"\[\d+\;\d+H", "", response ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 221 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 222 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 223 | def runAsSudoUser( self, handle, pwd, default ): |
| 224 | |
| 225 | i = handle.expect( [ ".ssword:*", default, pexpect.EOF ] ) |
| 226 | if i == 0: |
| 227 | handle.sendline( pwd ) |
| 228 | handle.sendline( "\r" ) |
| 229 | |
| 230 | if i == 1: |
| 231 | handle.expect( default ) |
| 232 | |
| 233 | if i == 2: |
| 234 | main.log.error( "Unable to run as Sudo user" ) |
| 235 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 236 | return handle |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 237 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 238 | def onfail( self ): |
| 239 | if 'onfail' in main.componentDictionary[ self.name ]: |
| 240 | commandList = main.componentDictionary[ |
| 241 | self.name ][ 'onfail' ].split( "," ) |
| 242 | for command in commandList: |
| 243 | response = self.execute( |
| 244 | cmd=command, |
| 245 | prompt="(.*)", |
| 246 | timeout=120 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 247 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 248 | def secureCopy( self, user_name, ip_address, filepath, pwd, dst_path ): |
| 249 | |
| 250 | # scp openflow@192.168.56.101:/home/openflow/sample |
| 251 | # /home/paxterra/Desktop/ |
| 252 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 253 | Connection will establish to the remote host using ssh. |
| 254 | It will take user_name ,ip_address and password as arguments<br> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 255 | and will return the handle. |
| 256 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 257 | ssh_newkey = 'Are you sure you want to continue connecting' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 258 | refused = "ssh: connect to host " + \ |
| 259 | ip_address + " port 22: Connection refused" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 260 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 261 | cmd = 'scp ' + str( user_name ) + '@' + str( ip_address ) + ':' + \ |
| 262 | str( filepath ) + ' ' + str(dst_path ) |
| 263 | |
| 264 | main.log.info( "Sending: " + cmd ) |
| 265 | self.handle = pexpect.spawn( cmd ) |
| 266 | i = self.handle.expect( [ |
| 267 | ssh_newkey, |
| 268 | 'password:', |
| 269 | pexpect.EOF, |
| 270 | pexpect.TIMEOUT, |
| 271 | refused ], |
| 272 | 120 ) |
| 273 | |
| 274 | if i == 0: |
| 275 | main.log.info( "ssh key confirmation received, send yes" ) |
| 276 | self.handle.sendline( 'yes' ) |
| 277 | i = self.handle.expect( [ ssh_newkey, 'password:', pexpect.EOF ] ) |
| 278 | if i == 1: |
| 279 | main.log.info( "ssh connection asked for password, gave password" ) |
| 280 | self.handle.sendline( pwd ) |
| 281 | # self.handle.expect( user_name ) |
| 282 | |
| 283 | elif i == 2: |
| 284 | main.log.error( "Connection timeout" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 285 | pass |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 286 | elif i == 3: # timeout |
| 287 | main.log.error( |
| 288 | "No route to the Host " + |
| 289 | user_name + |
| 290 | "@" + |
| 291 | ip_address ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 292 | return main.FALSE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 293 | elif i == 4: |
| 294 | main.log.error( |
| 295 | "ssh: connect to host " + |
| 296 | ip_address + |
| 297 | " port 22: Connection refused" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 298 | return main.FALSE |
| 299 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 300 | self.handle.sendline( "" ) |
| 301 | self.handle.expect( "$" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 302 | print self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 303 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 304 | return self.handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 305 | |