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 | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 61 | 'name': self.options.get( 'interfaceName', 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 |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 128 | if not self.options[ 'inband' ] == 'True': |
| 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 | d3ab356 | 2021-08-25 16:11:54 -0700 | [diff] [blame] | 251 | def addRouteToHost( self, route, gw, interface=None, sudoRequired=True, purgeOnDisconnect=True ): |
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: |
| 263 | cmd = "ip route add %s via %s" % ( route, gw ) |
| 264 | if sudoRequired: |
| 265 | cmd = "sudo %s" % cmd |
| 266 | if interface: |
| 267 | cmd = "%s dev %s" % ( cmd, interface ) |
| 268 | if purgeOnDisconnect: |
| 269 | self.tempRoutes.append( (route, gw, interface, sudoRequired ) ) |
| 270 | self.handle.sendline( cmd ) |
| 271 | i = self.handle.expect( [ "password|Password", self.prompt, "Error" ] ) |
| 272 | output = "" |
| 273 | if i == 2: |
| 274 | output += self.handle.before + self.handle.after |
| 275 | self.handle.expect( self.prompt ) |
| 276 | output += self.handle.before + self.handle.after |
| 277 | main.log.error( "%s: Erorr in ip route command: %s" % ( self.name, output ) ) |
| 278 | return main.FALSE |
| 279 | elif i == 0: |
| 280 | self.handle.sendline( self.pwd ) |
| 281 | j = self.handle.expect( [ "password|Password", self.prompt, "Error" ] ) |
| 282 | if j == 0: |
| 283 | main.log.error( "Incorrect password" ) |
| 284 | return main.FALSE |
| 285 | elif j == 2: |
| 286 | output += self.handle.before + self.handle.after |
| 287 | self.handle.expect( self.prompt ) |
| 288 | output += self.handle.before + self.handle.after |
| 289 | main.log.error( "%s: Erorr in ip route command: %s" % ( self.name, output ) ) |
| 290 | return main.FALSE |
| 291 | output += self.handle.before + self.handle.after |
| 292 | main.log.debug( output ) |
| 293 | return self.handle.before |
| 294 | except pexpect.EOF: |
| 295 | main.log.error( self.name + ": EOF exception found" ) |
| 296 | main.log.error( self.name + ": " + self.handle.before ) |
| 297 | return main.FALSE |
| 298 | except Exception: |
| 299 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 300 | main.log.error( self.name + ": " + self.handle.before ) |
| 301 | return main.FALSE |
| 302 | |
| 303 | def deleteRoute( self, route, gw, interface=None, sudoRequired=True ): |
| 304 | """ |
| 305 | Deletess a static route from the host |
| 306 | Arguments: |
| 307 | * route - String, CIDR notation for the destination subnet |
| 308 | * gw - String, IP address of gateway |
| 309 | Optional Arguments: |
| 310 | * interface - String, The interface to use for this route, defaults to None, or the default interface |
| 311 | * sudoRequired - Boolean, whether sudo is needed for this command, defaults to True |
| 312 | """ |
| 313 | try: |
| 314 | cmd = "ip route del %s via %s" % ( route, gw ) |
| 315 | if sudoRequired: |
| 316 | cmd = "sudo %s" % cmd |
| 317 | if interface: |
| 318 | cmd = "%s dev %s" % ( cmd, interface ) |
| 319 | self.handle.sendline( cmd ) |
| 320 | i = self.handle.expect( [ "password|Password", self.prompt, "Error" ] ) |
| 321 | output = "" |
| 322 | if i == 2: |
| 323 | output += self.handle.before + self.handle.after |
| 324 | self.handle.expect( self.prompt ) |
| 325 | output += self.handle.before + self.handle.after |
| 326 | main.log.error( "%s: Erorr in ip route command: %s" % ( self.name, output ) ) |
| 327 | return main.FALSE |
| 328 | elif i == 0: |
| 329 | self.handle.sendline( self.pwd ) |
| 330 | j = self.handle.expect( [ "password|Password", self.prompt, "Error" ] ) |
| 331 | if j == 0: |
| 332 | main.log.error( "Incorrect password" ) |
| 333 | return main.FALSE |
| 334 | elif j == 2: |
| 335 | output += self.handle.before + self.handle.after |
| 336 | self.handle.expect( self.prompt ) |
| 337 | output += self.handle.before + self.handle.after |
| 338 | main.log.error( "%s: Erorr in ip route command: %s" % ( self.name, output ) ) |
| 339 | return main.FALSE |
| 340 | output += self.handle.before + self.handle.after |
| 341 | main.log.debug( output ) |
| 342 | return self.handle.before |
| 343 | except pexpect.EOF: |
| 344 | main.log.error( self.name + ": EOF exception found" ) |
| 345 | main.log.error( self.name + ": " + self.handle.before ) |
| 346 | return main.FALSE |
| 347 | except Exception: |
| 348 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 349 | main.log.error( self.name + ": " + self.handle.before ) |
| 350 | return main.FALSE |
| 351 | |
Jon Hall | ceed0a3 | 2021-06-08 11:07:00 -0700 | [diff] [blame] | 352 | def getIPAddress( self, iface=None, proto='IPV4' ): |
| 353 | """ |
| 354 | Returns IP address of the host |
| 355 | """ |
| 356 | cmd = "ifconfig %s" % iface if iface else "" |
| 357 | response = self.command( cmd ) |
Jon Hall | 32c90f3 | 2021-06-24 16:32:44 -0700 | [diff] [blame] | 358 | if "Command 'ifconfig' not found" in response: |
| 359 | # ip a show dev |
| 360 | cmd = "ip addr show %s" % iface if iface else "" |
| 361 | response = self.command( cmd ) |
| 362 | pattern = '' |
| 363 | if proto == 'IPV4': |
| 364 | pattern = "inet\s(\d+\.\d+\.\d+\.\d+)/\d+" |
| 365 | else: |
| 366 | pattern = "inet6\s([\w,:]*)/\d+" |
Jon Hall | ceed0a3 | 2021-06-08 11:07:00 -0700 | [diff] [blame] | 367 | else: |
Jon Hall | 32c90f3 | 2021-06-24 16:32:44 -0700 | [diff] [blame] | 368 | pattern = '' |
| 369 | if proto == 'IPV4': |
| 370 | pattern = "inet\s(\d+\.\d+\.\d+\.\d+)\s\snetmask" |
| 371 | else: |
| 372 | pattern = "inet6\s([\w,:]*)/\d+\s\sprefixlen" |
Jon Hall | ceed0a3 | 2021-06-08 11:07:00 -0700 | [diff] [blame] | 373 | ipAddressSearch = re.search( pattern, response ) |
| 374 | if not ipAddressSearch: |
| 375 | return None |
| 376 | main.log.info( |
| 377 | self.name + |
| 378 | ": IP-Address is " + |
| 379 | ipAddressSearch.group( 1 ) ) |
| 380 | return ipAddressSearch.group( 1 ) |
| 381 | |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 382 | def ping( self, dst, ipv6=False, interface=None, wait=3 ): |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 383 | """ |
| 384 | Description: |
| 385 | Ping from this host to another |
| 386 | Required: |
| 387 | dst: IP address of destination host |
| 388 | Optional: |
| 389 | ipv6: will use ping6 command if True; otherwise use ping command |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 390 | interface: Specify which interface to use for the ping |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 391 | wait: timeout for ping command |
| 392 | """ |
| 393 | try: |
| 394 | command = "ping6" if ipv6 else "ping" |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 395 | if interface: |
| 396 | command += " -I %s " % interface |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 397 | command += " -c 1 -i 1 -W " + str( wait ) + " " + str( dst ) |
| 398 | main.log.info( self.name + ": Sending: " + command ) |
| 399 | self.handle.sendline( command ) |
| 400 | i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ], |
Jon Hall | a604fd4 | 2018-05-04 14:27:27 -0700 | [diff] [blame] | 401 | timeout=wait + 5 ) |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 402 | response = self.handle.before |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 403 | if i == 1: |
| 404 | main.log.error( |
| 405 | self.name + |
| 406 | ": timeout when waiting for response" ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 407 | main.log.error( self.name + ": response: " + str( self.handle.before ) ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 408 | if re.search( ',\s0\%\spacket\sloss', response ): |
| 409 | main.log.info( self.name + ": no packets lost, host is reachable" ) |
| 410 | return main.TRUE |
| 411 | else: |
| 412 | main.log.warn( |
| 413 | self.name + |
| 414 | ": PACKET LOST, HOST IS NOT REACHABLE" ) |
| 415 | return main.FALSE |
| 416 | except pexpect.EOF: |
| 417 | main.log.error( self.name + ": EOF exception found" ) |
| 418 | main.log.error( self.name + ": " + self.handle.before ) |
| 419 | main.cleanAndExit() |
| 420 | except Exception: |
| 421 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 422 | main.cleanAndExit() |
| 423 | |
You Wang | 0fc2170 | 2018-11-02 17:49:18 -0700 | [diff] [blame] | 424 | def pingHostSetAlternative( self, dstIPList, wait=1, IPv6=False ): |
| 425 | """ |
| 426 | Description: |
| 427 | Ping a set of destination host. |
| 428 | Params: |
| 429 | dstIPList is a list of destination ip addresses |
| 430 | Returns: |
| 431 | main.TRUE if the destination host is reachable |
| 432 | main.FALSE otherwise |
| 433 | """ |
| 434 | isReachable = main.TRUE |
| 435 | wait = int( wait ) |
| 436 | cmd = "ping" |
| 437 | if IPv6: |
| 438 | cmd = cmd + "6" |
| 439 | cmd = cmd + " -c 1 -i 1 -W " + str( wait ) |
| 440 | try: |
| 441 | for dstIP in dstIPList: |
| 442 | pingCmd = cmd + " " + dstIP |
| 443 | self.handle.sendline( pingCmd ) |
| 444 | i = self.handle.expect( [ self.prompt, |
| 445 | pexpect.TIMEOUT ], |
| 446 | timeout=wait + 5 ) |
| 447 | if i == 0: |
| 448 | response = self.handle.before |
| 449 | if not re.search( ',\s0\%\spacket\sloss', response ): |
| 450 | main.log.debug( "Ping failed between %s and %s" % ( self.name, dstIP ) ) |
| 451 | isReachable = main.FALSE |
| 452 | elif i == 1: |
| 453 | main.log.error( self.name + ": timeout when waiting for response" ) |
| 454 | isReachable = main.FALSE |
| 455 | else: |
| 456 | main.log.error( self.name + ": unknown response: " + self.handle.before ) |
| 457 | isReachable = main.FALSE |
| 458 | except pexpect.TIMEOUT: |
| 459 | main.log.exception( self.name + ": TIMEOUT exception" ) |
| 460 | self.exitFromCmd( [ self.prompt ] ) |
| 461 | isReachable = main.FALSE |
| 462 | except pexpect.EOF: |
| 463 | main.log.error( self.name + ": EOF exception found" ) |
| 464 | main.log.error( self.name + ": " + self.handle.before ) |
| 465 | main.cleanAndExit() |
| 466 | except Exception: |
| 467 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 468 | main.cleanAndExit() |
| 469 | return isReachable |
| 470 | |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 471 | def ifconfig( self, wait=3 ): |
| 472 | """ |
| 473 | Run ifconfig command on host and return output |
| 474 | """ |
| 475 | try: |
| 476 | command = "ifconfig" |
| 477 | main.log.info( self.name + ": Sending: " + command ) |
| 478 | self.handle.sendline( command ) |
Jon Hall | 32c90f3 | 2021-06-24 16:32:44 -0700 | [diff] [blame] | 479 | i = self.handle.expect( [ "Command 'ifconfig' not found", self.prompt, pexpect.TIMEOUT ], |
Jon Hall | a604fd4 | 2018-05-04 14:27:27 -0700 | [diff] [blame] | 480 | timeout=wait + 5 ) |
Jon Hall | 32c90f3 | 2021-06-24 16:32:44 -0700 | [diff] [blame] | 481 | if i == 0: |
| 482 | response = self.handle.before |
| 483 | self.handle.expect( [ self.prompt, pexpect.TIMEOUT ] ) |
| 484 | response += str( self.handle.before ) |
| 485 | main.log.error( self.name + ": Error running ifconfig: %s " % response ) |
| 486 | return response |
| 487 | if i == 2: |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 488 | main.log.error( |
| 489 | self.name + |
| 490 | ": timeout when waiting for response" ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 491 | main.log.error( self.name + ": response: " + str( self.handle.before ) ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 492 | response = self.handle.before |
| 493 | return response |
| 494 | except pexpect.EOF: |
| 495 | main.log.error( self.name + ": EOF exception found" ) |
| 496 | main.log.error( self.name + ": " + self.handle.before ) |
| 497 | main.cleanAndExit() |
| 498 | except Exception: |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 499 | main.log.exception( self.name + ": uncaught exception!" ) |
| 500 | main.cleanAndExit() |
| 501 | |
| 502 | def ip( self, options="a", wait=3 ): |
| 503 | """ |
| 504 | Run ip command on host and return output |
| 505 | """ |
| 506 | try: |
| 507 | command = "ip {}".format( options ) |
| 508 | main.log.info( self.name + ": Sending: " + command ) |
| 509 | self.handle.sendline( command ) |
| 510 | i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ], |
| 511 | timeout=wait + 5 ) |
| 512 | if i == 1: |
| 513 | main.log.error( self.name + ": timeout when waiting for response" ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 514 | main.log.error( self.name + ": response: " + str( self.handle.before ) ) |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 515 | response = self.handle.before |
| 516 | return response |
| 517 | except pexpect.EOF: |
| 518 | main.log.error( self.name + ": EOF exception found" ) |
| 519 | main.log.error( self.name + ": " + self.handle.before ) |
| 520 | main.cleanAndExit() |
| 521 | except Exception: |
| 522 | main.log.exception( self.name + ": uncaught exception!" ) |
| 523 | main.cleanAndExit() |
| 524 | |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 525 | def command( self, cmd, wait=3, debug=False): |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 526 | """ |
| 527 | Run shell command on host and return output |
| 528 | Required: |
| 529 | cmd: command to run on the host |
| 530 | """ |
| 531 | try: |
| 532 | main.log.info( self.name + ": Sending: " + cmd ) |
| 533 | self.handle.sendline( cmd ) |
| 534 | i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ], |
| 535 | timeout=wait + 5 ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 536 | response = self.handle.before |
| 537 | if debug: |
| 538 | main.log.debug( response ) |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 539 | if i == 1: |
| 540 | main.log.error( self.name + ": timeout when waiting for response" ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 541 | main.log.error( self.name + ": response: " + str( response ) ) |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 542 | return response |
| 543 | except pexpect.EOF: |
| 544 | main.log.error( self.name + ": EOF exception found" ) |
| 545 | main.log.error( self.name + ": " + self.handle.before ) |
| 546 | main.cleanAndExit() |
| 547 | except Exception: |
| 548 | main.log.exception( self.name + ": uncaught exception!" ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 549 | main.cleanAndExit() |