Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | """ |
| 3 | Copyright 2018 Open Networking Foundation (ONF) |
| 4 | |
| 5 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 6 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 7 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
| 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 |
| 12 | ( at your option ) any later version. |
| 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 |
| 20 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 21 | """ |
| 22 | |
| 23 | import pexpect |
| 24 | import re |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 25 | import os |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 26 | from drivers.common.cli.emulator.scapyclidriver import ScapyCliDriver |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 27 | |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 28 | class HostDriver( ScapyCliDriver ): |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 29 | """ |
| 30 | This class is created as a standalone host driver. |
| 31 | """ |
| 32 | def __init__( self ): |
| 33 | super( HostDriver, self ).__init__() |
| 34 | self.handle = self |
| 35 | self.name = None |
| 36 | self.shortName = None |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 37 | self.interfaces = [] |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 38 | self.home = None |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 39 | self.inband = False |
| 40 | self.prompt = "\$" |
| 41 | self.scapyPrompt = ">>>" |
Jon Hall | 62ab675 | 2021-08-22 16:47:43 -0700 | [diff] [blame] | 42 | self.tempRoutes = [] |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 43 | |
| 44 | def connect( self, **connectargs ): |
| 45 | """ |
| 46 | Creates ssh handle for host. |
| 47 | NOTE: |
| 48 | The ip_address would come from the topo file using the host tag, the |
| 49 | value can be an environment variable as well as a "localhost" to get |
| 50 | the ip address needed to ssh to the "bench" |
| 51 | """ |
| 52 | try: |
| 53 | for key in connectargs: |
| 54 | vars( self )[ key ] = connectargs[ key ] |
| 55 | self.name = self.options[ 'name' ] |
| 56 | self.shortName = self.options[ 'shortName' ] |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 57 | self.interfaces.append( { 'ips': [ self.options[ 'ip' ] ], |
| 58 | 'isUp': True, |
| 59 | 'mac': self.options[ 'mac' ], |
Jon Hall | 4d956de | 2021-06-09 10:09:20 -0700 | [diff] [blame] | 60 | 'dhcp': self.options.get( 'dhcp', "False" ), |
Jon Hall | 8a99e8d | 2021-09-01 11:18:12 -0700 | [diff] [blame] | 61 | 'name': self.options.get( 'ifaceName', None ) } ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 62 | |
| 63 | try: |
| 64 | if os.getenv( str( self.ip_address ) ) is not None: |
| 65 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 66 | else: |
| 67 | main.log.info( self.name + |
| 68 | ": Trying to connect to " + |
| 69 | self.ip_address ) |
| 70 | except KeyError: |
| 71 | main.log.info( "Invalid host name," + |
| 72 | " connecting to local host instead" ) |
| 73 | self.ip_address = 'localhost' |
| 74 | except Exception as inst: |
| 75 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 76 | |
| 77 | self.handle = super( |
| 78 | HostDriver, |
| 79 | self ).connect( |
| 80 | user_name=self.user_name, |
| 81 | ip_address=self.ip_address, |
| 82 | port=None, |
| 83 | pwd=self.pwd ) |
| 84 | |
Jon Hall | ceed0a3 | 2021-06-08 11:07:00 -0700 | [diff] [blame] | 85 | # Update IP of interfaces if using dhcp |
| 86 | for intf in self.interfaces: |
| 87 | if intf[ 'dhcp' ].lower() == "true": |
| 88 | ip = self.getIPAddress( iface=intf[ 'name' ] ) |
| 89 | if ip: |
| 90 | intf['ips'] = [ ip ] |
| 91 | else: |
| 92 | main.log.warn( self.name + ": Could not find IP of %s" % intf[ 'name' ] ) |
| 93 | |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 94 | if self.handle: |
| 95 | main.log.info( "Connection successful to the " + |
| 96 | self.user_name + |
| 97 | "@" + |
| 98 | self.ip_address ) |
| 99 | self.handle.sendline( "" ) |
| 100 | self.handle.expect( self.prompt ) |
| 101 | return main.TRUE |
| 102 | else: |
| 103 | main.log.error( "Connection failed to " + |
| 104 | self.user_name + |
| 105 | "@" + |
| 106 | self.ip_address ) |
| 107 | return main.FALSE |
| 108 | except pexpect.EOF: |
| 109 | main.log.error( self.name + ": EOF exception found" ) |
| 110 | main.log.error( self.name + ": " + self.handle.before ) |
| 111 | main.cleanAndExit() |
| 112 | except Exception: |
| 113 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 114 | main.cleanAndExit() |
| 115 | |
| 116 | def disconnect( self, **connectargs ): |
| 117 | """ |
| 118 | Called when test is complete to disconnect the handle. |
| 119 | """ |
| 120 | response = main.TRUE |
| 121 | try: |
| 122 | if self.handle: |
Jon Hall | 49d781c | 2021-07-29 11:23:46 -0700 | [diff] [blame] | 123 | self.preDisconnect() |
Jon Hall | 62ab675 | 2021-08-22 16:47:43 -0700 | [diff] [blame] | 124 | if self.tempRoutes: |
| 125 | for r in self.tempRoutes: |
| 126 | self.deleteRoute( *r ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 127 | # Disconnect from the host |
Jon Hall | 026bd71 | 2021-08-31 16:30:09 -0700 | [diff] [blame] | 128 | if not self.options.get( 'inband', False ) == 'True': |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 129 | self.handle.sendline( "" ) |
| 130 | self.handle.expect( self.prompt ) |
| 131 | self.handle.sendline( "exit" ) |
| 132 | i = self.handle.expect( [ "closed", pexpect.TIMEOUT ] ) |
| 133 | if i == 1: |
| 134 | main.log.error( self.name + ": timeout when waiting for response" ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 135 | main.log.error( self.name + ": response: " + str( self.handle.before ) ) |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 136 | else: |
| 137 | self.handle.sendline( "" ) |
| 138 | i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ], timeout=2 ) |
| 139 | if i == 1: |
| 140 | main.log.warn( self.name + ": timeout when waiting for response" ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 141 | main.log.warn( self.name + ": response: " + str( self.handle.before ) ) |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 142 | self.handle.sendline( "exit" ) |
| 143 | i = self.handle.expect( [ "closed", pexpect.TIMEOUT ], timeout=2 ) |
| 144 | if i == 1: |
| 145 | main.log.warn( self.name + ": timeout when waiting for response" ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 146 | main.log.warn( self.name + ": response: " + str( self.handle.before ) ) |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 147 | return main.TRUE |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 148 | except TypeError: |
| 149 | main.log.exception( self.name + ": Object not as expected" ) |
| 150 | response = main.FALSE |
| 151 | except pexpect.EOF: |
| 152 | main.log.error( self.name + ": EOF exception found" ) |
| 153 | main.log.error( self.name + ": " + self.handle.before ) |
| 154 | except ValueError: |
| 155 | main.log.exception( "Exception in disconnect of " + self.name ) |
| 156 | response = main.TRUE |
| 157 | except Exception: |
| 158 | main.log.exception( self.name + ": Connection failed to the host" ) |
| 159 | response = main.FALSE |
| 160 | return response |
| 161 | |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 162 | def connectInband( self ): |
| 163 | """ |
| 164 | ssh to the host using its data plane IP |
| 165 | """ |
| 166 | try: |
| 167 | if not self.options[ 'inband' ] == 'True': |
| 168 | main.log.info( "Skip connecting the host via data plane" ) |
| 169 | return main.TRUE |
| 170 | self.handle.sendline( "" ) |
| 171 | self.handle.expect( self.prompt ) |
| 172 | self.handle.sendline( "ssh {}@{}".format( self.options[ 'username' ], |
| 173 | self.options[ 'ip' ] ) ) |
| 174 | i = self.handle.expect( [ "password:|Password:", self.prompt, pexpect.TIMEOUT ], timeout=30 ) |
| 175 | if i == 0: |
| 176 | self.handle.sendline( self.options[ 'password' ] ) |
| 177 | j = self.handle.expect( [ "password:|Password:", self.prompt, pexpect.TIMEOUT ], timeout=10 ) |
| 178 | if j != 1: |
| 179 | main.log.error( "Incorrect password" ) |
| 180 | return main.FALSE |
| 181 | elif i == 1: |
| 182 | main.log.info( "Password not required logged in" ) |
| 183 | else: |
| 184 | main.log.error( "Failed to connect to the host" ) |
| 185 | return main.FALSE |
| 186 | self.inband = True |
| 187 | return main.TRUE |
| 188 | except KeyError: |
| 189 | main.log.error( self.name + ": host component not as expected" ) |
| 190 | main.log.error( self.name + ": " + self.handle.before ) |
| 191 | return main.FALSE |
| 192 | except pexpect.EOF: |
| 193 | main.log.error( self.name + ": EOF exception found" ) |
| 194 | main.log.error( self.name + ": " + self.handle.before ) |
| 195 | return main.FALSE |
| 196 | except Exception: |
| 197 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 198 | main.log.error( self.name + ": " + self.handle.before ) |
| 199 | return main.FALSE |
| 200 | |
| 201 | def disconnectInband( self ): |
| 202 | """ |
| 203 | Terminate the ssh connection to the host's data plane IP |
| 204 | """ |
| 205 | try: |
| 206 | if not self.options[ 'inband' ] == 'True': |
| 207 | main.log.info( "Skip disconnecting the host via data plane" ) |
| 208 | return main.TRUE |
| 209 | self.handle.sendline( "" ) |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 210 | self.handle.expect( self.prompt, timeout=2 ) |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 211 | self.handle.sendline( "exit" ) |
| 212 | i = self.handle.expect( [ "closed", pexpect.TIMEOUT ], timeout=2 ) |
| 213 | if i == 1: |
| 214 | main.log.error( self.name + ": timeout when waiting for response" ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 215 | main.log.error( self.name + ": response: " + str( self.handle.before ) ) |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 216 | return main.TRUE |
| 217 | except pexpect.EOF: |
| 218 | main.log.error( self.name + ": EOF exception found" ) |
| 219 | main.log.error( self.name + ": " + self.handle.before ) |
| 220 | return main.FALSE |
| 221 | except Exception: |
| 222 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 223 | main.log.error( self.name + ": " + self.handle.before ) |
| 224 | return main.FALSE |
| 225 | |
Jon Hall | 62ab675 | 2021-08-22 16:47:43 -0700 | [diff] [blame] | 226 | def getRoutes( self ): |
| 227 | """ |
| 228 | Returns the output of 'ip route show' command |
| 229 | # TODO: process and return a a list or json object |
| 230 | """ |
| 231 | try: |
| 232 | cmd = "ip route show" |
| 233 | self.handle.sendline( cmd ) |
| 234 | i = self.handle.expect( [ "password|Password", self.prompt ] ) |
| 235 | if i == 0: |
| 236 | self.handle.sendline( self.pwd ) |
| 237 | j = self.handle.expect( [ "password|Password", self.prompt ] ) |
| 238 | if j != 1: |
| 239 | main.log.error( "Incorrect password" ) |
| 240 | return main.FALSE |
| 241 | return self.handle.before |
| 242 | except pexpect.EOF: |
| 243 | main.log.error( self.name + ": EOF exception found" ) |
| 244 | main.log.error( self.name + ": " + self.handle.before ) |
| 245 | return main.FALSE |
| 246 | except Exception: |
| 247 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 248 | main.log.error( self.name + ": " + self.handle.before ) |
| 249 | return main.FALSE |
| 250 | |
Jon Hall | c03ade9 | 2021-09-13 17:19:10 -0700 | [diff] [blame^] | 251 | def addRouteToHost( self, route, gw, interface=None, sudoRequired=True, purgeOnDisconnect=True, cmdPath='/sbin/ip' ): |
Jon Hall | 62ab675 | 2021-08-22 16:47:43 -0700 | [diff] [blame] | 252 | """ |
| 253 | Adds a static route to the host |
| 254 | Arguments: |
| 255 | * route - String, CIDR notation for the destination subnet |
| 256 | * gw - String, IP address of gateway |
| 257 | Optional Arguments: |
| 258 | * interface - String, The interface to use for this route, defaults to None, or the default interface |
| 259 | * sudoRequired - Boolean, whether sudo is needed for this command, defaults to True |
| 260 | * purgeOnDisconnect - Boolean, remove this route before disconnecting from component |
| 261 | """ |
| 262 | try: |
Jon Hall | c03ade9 | 2021-09-13 17:19:10 -0700 | [diff] [blame^] | 263 | if cmdPath: |
| 264 | cmd = cmdPath |
| 265 | else: |
| 266 | cmd = "ip" |
| 267 | cmd += " route add %s via %s" % ( route, gw ) |
Jon Hall | 62ab675 | 2021-08-22 16:47:43 -0700 | [diff] [blame] | 268 | if sudoRequired: |
| 269 | cmd = "sudo %s" % cmd |
| 270 | if interface: |
| 271 | cmd = "%s dev %s" % ( cmd, interface ) |
| 272 | if purgeOnDisconnect: |
| 273 | self.tempRoutes.append( (route, gw, interface, sudoRequired ) ) |
| 274 | self.handle.sendline( cmd ) |
| 275 | i = self.handle.expect( [ "password|Password", self.prompt, "Error" ] ) |
| 276 | output = "" |
| 277 | if i == 2: |
| 278 | output += self.handle.before + self.handle.after |
| 279 | self.handle.expect( self.prompt ) |
| 280 | output += self.handle.before + self.handle.after |
| 281 | main.log.error( "%s: Erorr in ip route command: %s" % ( self.name, output ) ) |
| 282 | return main.FALSE |
| 283 | elif i == 0: |
| 284 | self.handle.sendline( self.pwd ) |
| 285 | j = self.handle.expect( [ "password|Password", self.prompt, "Error" ] ) |
| 286 | if j == 0: |
| 287 | main.log.error( "Incorrect password" ) |
| 288 | return main.FALSE |
| 289 | elif j == 2: |
| 290 | output += self.handle.before + self.handle.after |
| 291 | self.handle.expect( self.prompt ) |
| 292 | output += self.handle.before + self.handle.after |
| 293 | main.log.error( "%s: Erorr in ip route command: %s" % ( self.name, output ) ) |
| 294 | return main.FALSE |
| 295 | output += self.handle.before + self.handle.after |
| 296 | main.log.debug( output ) |
| 297 | return self.handle.before |
| 298 | except pexpect.EOF: |
| 299 | main.log.error( self.name + ": EOF exception found" ) |
| 300 | main.log.error( self.name + ": " + self.handle.before ) |
| 301 | return main.FALSE |
| 302 | except Exception: |
| 303 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 304 | main.log.error( self.name + ": " + self.handle.before ) |
| 305 | return main.FALSE |
| 306 | |
Jon Hall | c03ade9 | 2021-09-13 17:19:10 -0700 | [diff] [blame^] | 307 | def deleteRoute( self, route, gw, interface=None, sudoRequired=True, cmdPath='/sbin/ip' ): |
Jon Hall | 62ab675 | 2021-08-22 16:47:43 -0700 | [diff] [blame] | 308 | """ |
| 309 | Deletess a static route from the host |
| 310 | Arguments: |
| 311 | * route - String, CIDR notation for the destination subnet |
| 312 | * gw - String, IP address of gateway |
| 313 | Optional Arguments: |
| 314 | * interface - String, The interface to use for this route, defaults to None, or the default interface |
| 315 | * sudoRequired - Boolean, whether sudo is needed for this command, defaults to True |
| 316 | """ |
| 317 | try: |
Jon Hall | c03ade9 | 2021-09-13 17:19:10 -0700 | [diff] [blame^] | 318 | if cmdPath: |
| 319 | cmd = cmdPath |
| 320 | else: |
| 321 | cmd = "ip" |
| 322 | cmd += " route del %s via %s" % ( route, gw ) |
Jon Hall | 62ab675 | 2021-08-22 16:47:43 -0700 | [diff] [blame] | 323 | if sudoRequired: |
| 324 | cmd = "sudo %s" % cmd |
| 325 | if interface: |
| 326 | cmd = "%s dev %s" % ( cmd, interface ) |
| 327 | self.handle.sendline( cmd ) |
| 328 | i = self.handle.expect( [ "password|Password", self.prompt, "Error" ] ) |
| 329 | output = "" |
| 330 | if i == 2: |
| 331 | output += self.handle.before + self.handle.after |
| 332 | self.handle.expect( self.prompt ) |
| 333 | output += self.handle.before + self.handle.after |
| 334 | main.log.error( "%s: Erorr in ip route command: %s" % ( self.name, output ) ) |
| 335 | return main.FALSE |
| 336 | elif i == 0: |
| 337 | self.handle.sendline( self.pwd ) |
| 338 | j = self.handle.expect( [ "password|Password", self.prompt, "Error" ] ) |
| 339 | if j == 0: |
| 340 | main.log.error( "Incorrect password" ) |
| 341 | return main.FALSE |
| 342 | elif j == 2: |
| 343 | output += self.handle.before + self.handle.after |
| 344 | self.handle.expect( self.prompt ) |
| 345 | output += self.handle.before + self.handle.after |
| 346 | main.log.error( "%s: Erorr in ip route command: %s" % ( self.name, output ) ) |
| 347 | return main.FALSE |
| 348 | output += self.handle.before + self.handle.after |
| 349 | main.log.debug( output ) |
| 350 | return self.handle.before |
| 351 | except pexpect.EOF: |
| 352 | main.log.error( self.name + ": EOF exception found" ) |
| 353 | main.log.error( self.name + ": " + self.handle.before ) |
| 354 | return main.FALSE |
| 355 | except Exception: |
| 356 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 357 | main.log.error( self.name + ": " + self.handle.before ) |
| 358 | return main.FALSE |
| 359 | |
Jon Hall | ceed0a3 | 2021-06-08 11:07:00 -0700 | [diff] [blame] | 360 | def getIPAddress( self, iface=None, proto='IPV4' ): |
| 361 | """ |
| 362 | Returns IP address of the host |
| 363 | """ |
| 364 | cmd = "ifconfig %s" % iface if iface else "" |
| 365 | response = self.command( cmd ) |
Jon Hall | 32c90f3 | 2021-06-24 16:32:44 -0700 | [diff] [blame] | 366 | if "Command 'ifconfig' not found" in response: |
| 367 | # ip a show dev |
| 368 | cmd = "ip addr show %s" % iface if iface else "" |
| 369 | response = self.command( cmd ) |
| 370 | pattern = '' |
| 371 | if proto == 'IPV4': |
| 372 | pattern = "inet\s(\d+\.\d+\.\d+\.\d+)/\d+" |
| 373 | else: |
| 374 | pattern = "inet6\s([\w,:]*)/\d+" |
Jon Hall | ceed0a3 | 2021-06-08 11:07:00 -0700 | [diff] [blame] | 375 | else: |
Jon Hall | 32c90f3 | 2021-06-24 16:32:44 -0700 | [diff] [blame] | 376 | pattern = '' |
| 377 | if proto == 'IPV4': |
| 378 | pattern = "inet\s(\d+\.\d+\.\d+\.\d+)\s\snetmask" |
| 379 | else: |
| 380 | pattern = "inet6\s([\w,:]*)/\d+\s\sprefixlen" |
Jon Hall | ceed0a3 | 2021-06-08 11:07:00 -0700 | [diff] [blame] | 381 | ipAddressSearch = re.search( pattern, response ) |
| 382 | if not ipAddressSearch: |
| 383 | return None |
| 384 | main.log.info( |
| 385 | self.name + |
| 386 | ": IP-Address is " + |
| 387 | ipAddressSearch.group( 1 ) ) |
| 388 | return ipAddressSearch.group( 1 ) |
| 389 | |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 390 | def ping( self, dst, ipv6=False, interface=None, wait=3 ): |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 391 | """ |
| 392 | Description: |
| 393 | Ping from this host to another |
| 394 | Required: |
| 395 | dst: IP address of destination host |
| 396 | Optional: |
| 397 | ipv6: will use ping6 command if True; otherwise use ping command |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 398 | interface: Specify which interface to use for the ping |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 399 | wait: timeout for ping command |
| 400 | """ |
| 401 | try: |
| 402 | command = "ping6" if ipv6 else "ping" |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 403 | if interface: |
| 404 | command += " -I %s " % interface |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 405 | command += " -c 1 -i 1 -W " + str( wait ) + " " + str( dst ) |
| 406 | main.log.info( self.name + ": Sending: " + command ) |
| 407 | self.handle.sendline( command ) |
| 408 | i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ], |
Jon Hall | a604fd4 | 2018-05-04 14:27:27 -0700 | [diff] [blame] | 409 | timeout=wait + 5 ) |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 410 | response = self.handle.before |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 411 | if i == 1: |
| 412 | main.log.error( |
| 413 | self.name + |
| 414 | ": timeout when waiting for response" ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 415 | main.log.error( self.name + ": response: " + str( self.handle.before ) ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 416 | if re.search( ',\s0\%\spacket\sloss', response ): |
| 417 | main.log.info( self.name + ": no packets lost, host is reachable" ) |
| 418 | return main.TRUE |
| 419 | else: |
| 420 | main.log.warn( |
| 421 | self.name + |
| 422 | ": PACKET LOST, HOST IS NOT REACHABLE" ) |
| 423 | return main.FALSE |
| 424 | except pexpect.EOF: |
| 425 | main.log.error( self.name + ": EOF exception found" ) |
| 426 | main.log.error( self.name + ": " + self.handle.before ) |
| 427 | main.cleanAndExit() |
| 428 | except Exception: |
| 429 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 430 | main.cleanAndExit() |
| 431 | |
You Wang | 0fc2170 | 2018-11-02 17:49:18 -0700 | [diff] [blame] | 432 | def pingHostSetAlternative( self, dstIPList, wait=1, IPv6=False ): |
| 433 | """ |
| 434 | Description: |
| 435 | Ping a set of destination host. |
| 436 | Params: |
| 437 | dstIPList is a list of destination ip addresses |
| 438 | Returns: |
| 439 | main.TRUE if the destination host is reachable |
| 440 | main.FALSE otherwise |
| 441 | """ |
| 442 | isReachable = main.TRUE |
| 443 | wait = int( wait ) |
| 444 | cmd = "ping" |
| 445 | if IPv6: |
| 446 | cmd = cmd + "6" |
| 447 | cmd = cmd + " -c 1 -i 1 -W " + str( wait ) |
| 448 | try: |
| 449 | for dstIP in dstIPList: |
| 450 | pingCmd = cmd + " " + dstIP |
| 451 | self.handle.sendline( pingCmd ) |
| 452 | i = self.handle.expect( [ self.prompt, |
| 453 | pexpect.TIMEOUT ], |
| 454 | timeout=wait + 5 ) |
| 455 | if i == 0: |
| 456 | response = self.handle.before |
| 457 | if not re.search( ',\s0\%\spacket\sloss', response ): |
| 458 | main.log.debug( "Ping failed between %s and %s" % ( self.name, dstIP ) ) |
| 459 | isReachable = main.FALSE |
| 460 | elif i == 1: |
| 461 | main.log.error( self.name + ": timeout when waiting for response" ) |
| 462 | isReachable = main.FALSE |
| 463 | else: |
| 464 | main.log.error( self.name + ": unknown response: " + self.handle.before ) |
| 465 | isReachable = main.FALSE |
| 466 | except pexpect.TIMEOUT: |
| 467 | main.log.exception( self.name + ": TIMEOUT exception" ) |
| 468 | self.exitFromCmd( [ self.prompt ] ) |
| 469 | isReachable = main.FALSE |
| 470 | except pexpect.EOF: |
| 471 | main.log.error( self.name + ": EOF exception found" ) |
| 472 | main.log.error( self.name + ": " + self.handle.before ) |
| 473 | main.cleanAndExit() |
| 474 | except Exception: |
| 475 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 476 | main.cleanAndExit() |
| 477 | return isReachable |
| 478 | |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 479 | def ifconfig( self, wait=3 ): |
| 480 | """ |
| 481 | Run ifconfig command on host and return output |
| 482 | """ |
| 483 | try: |
| 484 | command = "ifconfig" |
| 485 | main.log.info( self.name + ": Sending: " + command ) |
| 486 | self.handle.sendline( command ) |
Jon Hall | 32c90f3 | 2021-06-24 16:32:44 -0700 | [diff] [blame] | 487 | i = self.handle.expect( [ "Command 'ifconfig' not found", self.prompt, pexpect.TIMEOUT ], |
Jon Hall | a604fd4 | 2018-05-04 14:27:27 -0700 | [diff] [blame] | 488 | timeout=wait + 5 ) |
Jon Hall | 32c90f3 | 2021-06-24 16:32:44 -0700 | [diff] [blame] | 489 | if i == 0: |
| 490 | response = self.handle.before |
| 491 | self.handle.expect( [ self.prompt, pexpect.TIMEOUT ] ) |
| 492 | response += str( self.handle.before ) |
| 493 | main.log.error( self.name + ": Error running ifconfig: %s " % response ) |
| 494 | return response |
| 495 | if i == 2: |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 496 | main.log.error( |
| 497 | self.name + |
| 498 | ": timeout when waiting for response" ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 499 | main.log.error( self.name + ": response: " + str( self.handle.before ) ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 500 | response = self.handle.before |
| 501 | return response |
| 502 | except pexpect.EOF: |
| 503 | main.log.error( self.name + ": EOF exception found" ) |
| 504 | main.log.error( self.name + ": " + self.handle.before ) |
| 505 | main.cleanAndExit() |
| 506 | except Exception: |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 507 | main.log.exception( self.name + ": uncaught exception!" ) |
| 508 | main.cleanAndExit() |
| 509 | |
| 510 | def ip( self, options="a", wait=3 ): |
| 511 | """ |
| 512 | Run ip command on host and return output |
| 513 | """ |
| 514 | try: |
| 515 | command = "ip {}".format( options ) |
| 516 | main.log.info( self.name + ": Sending: " + command ) |
| 517 | self.handle.sendline( command ) |
| 518 | i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ], |
| 519 | timeout=wait + 5 ) |
| 520 | if i == 1: |
| 521 | main.log.error( self.name + ": timeout when waiting for response" ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 522 | main.log.error( self.name + ": response: " + str( self.handle.before ) ) |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 523 | response = self.handle.before |
| 524 | return response |
| 525 | except pexpect.EOF: |
| 526 | main.log.error( self.name + ": EOF exception found" ) |
| 527 | main.log.error( self.name + ": " + self.handle.before ) |
| 528 | main.cleanAndExit() |
| 529 | except Exception: |
| 530 | main.log.exception( self.name + ": uncaught exception!" ) |
| 531 | main.cleanAndExit() |
| 532 | |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 533 | def command( self, cmd, wait=3, debug=False): |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 534 | """ |
| 535 | Run shell command on host and return output |
| 536 | Required: |
| 537 | cmd: command to run on the host |
| 538 | """ |
| 539 | try: |
| 540 | main.log.info( self.name + ": Sending: " + cmd ) |
| 541 | self.handle.sendline( cmd ) |
| 542 | i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ], |
| 543 | timeout=wait + 5 ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 544 | response = self.handle.before |
| 545 | if debug: |
| 546 | main.log.debug( response ) |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 547 | if i == 1: |
| 548 | main.log.error( self.name + ": timeout when waiting for response" ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 549 | main.log.error( self.name + ": response: " + str( response ) ) |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 550 | return response |
| 551 | except pexpect.EOF: |
| 552 | main.log.error( self.name + ": EOF exception found" ) |
| 553 | main.log.error( self.name + ": " + self.handle.before ) |
| 554 | main.cleanAndExit() |
| 555 | except Exception: |
| 556 | main.log.exception( self.name + ": uncaught exception!" ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 557 | main.cleanAndExit() |