Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | """ |
| 3 | 2015-2016 |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 4 | Copyright 2016 Open Networking Foundation (ONF) |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 5 | |
| 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> |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [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 |
| 13 | ( at your option ) any later version. |
| 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 |
| 21 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | ScapyCliDriver is the basic driver which will handle the Scapy functions |
| 24 | |
| 25 | TODO: Add Explanation on how to install scapy |
| 26 | """ |
| 27 | import pexpect |
| 28 | import re |
| 29 | import sys |
| 30 | import types |
| 31 | import os |
| 32 | from drivers.common.cli.emulatordriver import Emulator |
| 33 | |
| 34 | |
| 35 | class ScapyCliDriver( Emulator ): |
| 36 | |
| 37 | """ |
| 38 | ScapyCliDriver is the basic driver which will handle |
| 39 | the Scapy functions""" |
| 40 | def __init__( self ): |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 41 | super( ScapyCliDriver, self ).__init__() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 42 | self.handle = self |
| 43 | self.name = None |
| 44 | self.home = None |
| 45 | self.wrapped = sys.modules[ __name__ ] |
| 46 | self.flag = 0 |
| 47 | # TODO: Refactor driver to use these everywhere |
You Wang | dafb6e2 | 2018-01-22 17:01:00 -0800 | [diff] [blame] | 48 | self.hostPrompt = "\$" |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 49 | self.scapyPrompt = ">>>" |
| 50 | |
| 51 | def connect( self, **connectargs ): |
| 52 | """ |
| 53 | Here the main is the TestON instance after creating |
| 54 | all the log handles.""" |
| 55 | try: |
| 56 | for key in connectargs: |
| 57 | vars( self )[ key ] = connectargs[ key ] |
You Wang | dafb6e2 | 2018-01-22 17:01:00 -0800 | [diff] [blame] | 58 | self.home = self.options[ 'home' ] if 'home' in self.options.keys() else "~/" |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 59 | self.name = self.options[ 'name' ] |
You Wang | dafb6e2 | 2018-01-22 17:01:00 -0800 | [diff] [blame] | 60 | self.ifaceName = self.options[ 'ifaceName' ] if 'ifaceName' in self.options.keys() else self.name + "-eth0" |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 61 | try: |
| 62 | if os.getenv( str( self.ip_address ) ) is not None: |
| 63 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 64 | else: |
| 65 | main.log.info( self.name + |
| 66 | ": Trying to connect to " + |
| 67 | self.ip_address ) |
| 68 | |
| 69 | except KeyError: |
| 70 | main.log.info( "Invalid host name," + |
| 71 | " connecting to local host instead" ) |
| 72 | self.ip_address = 'localhost' |
| 73 | except Exception as inst: |
| 74 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 75 | |
| 76 | self.handle = super( |
| 77 | ScapyCliDriver, |
| 78 | self ).connect( |
| 79 | user_name=self.user_name, |
| 80 | ip_address=self.ip_address, |
| 81 | port=None, |
| 82 | pwd=self.pwd ) |
| 83 | |
| 84 | if self.handle: |
| 85 | main.log.info( "Connection successful to the host " + |
| 86 | self.user_name + |
| 87 | "@" + |
| 88 | self.ip_address ) |
| 89 | return main.TRUE |
| 90 | else: |
| 91 | main.log.error( "Connection failed to the host " + |
| 92 | self.user_name + |
| 93 | "@" + |
| 94 | self.ip_address ) |
| 95 | main.log.error( "Failed to connect to the Mininet Host" ) |
| 96 | return main.FALSE |
| 97 | except pexpect.EOF: |
| 98 | main.log.error( self.name + ": EOF exception found" ) |
| 99 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 100 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 101 | except Exception: |
| 102 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 103 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 104 | |
| 105 | def disconnect( self ): |
| 106 | """ |
| 107 | Called at the end of the test to stop the scapy component and |
| 108 | disconnect the handle. |
| 109 | """ |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 110 | response = main.TRUE |
You Wang | dafb6e2 | 2018-01-22 17:01:00 -0800 | [diff] [blame] | 111 | try: |
| 112 | if self.handle: |
| 113 | self.handle.sendline( "exit" ) |
| 114 | self.handle.expect( "closed" ) |
| 115 | except pexpect.EOF: |
| 116 | main.log.error( self.name + ": EOF exception found" ) |
| 117 | main.log.error( self.name + ": " + self.handle.before ) |
| 118 | except Exception: |
| 119 | main.log.exception( self.name + ": Connection failed to the host" ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 120 | response = main.FALSE |
| 121 | return response |
| 122 | |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 123 | def startScapy( self, mplsPath="" ): |
| 124 | """ |
| 125 | Start the Scapy cli |
| 126 | optional: |
| 127 | mplsPath - The path where the MPLS class is located |
| 128 | NOTE: This can be a relative path from the user's home dir |
| 129 | """ |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 130 | mplsLines = [ 'import imp', |
| 131 | 'imp.load_source( "mplsClass", "{}mplsClass.py" )'.format( mplsPath ), |
| 132 | 'from mplsClass import MPLS', |
| 133 | 'bind_layers(Ether, MPLS, type = 0x8847)', |
| 134 | 'bind_layers(MPLS, MPLS, bottom_of_label_stack = 0)', |
| 135 | 'bind_layers(MPLS, IP)' ] |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 136 | |
| 137 | try: |
| 138 | self.handle.sendline( "scapy" ) |
| 139 | self.handle.expect( self.scapyPrompt ) |
| 140 | self.handle.sendline( "conf.color_theme = NoTheme()" ) |
| 141 | self.handle.expect( self.scapyPrompt ) |
| 142 | if mplsPath: |
| 143 | main.log.info( "Adding MPLS class" ) |
| 144 | main.log.info( "MPLS class path: " + mplsPath ) |
| 145 | for line in mplsLines: |
| 146 | main.log.info( "sending line: " + line ) |
| 147 | self.handle.sendline( line ) |
| 148 | self.handle.expect( self.scapyPrompt ) |
| 149 | return main.TRUE |
| 150 | except pexpect.TIMEOUT: |
| 151 | main.log.exception( self.name + ": Command timed out" ) |
| 152 | return main.FALSE |
| 153 | except pexpect.EOF: |
| 154 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 155 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 156 | except Exception: |
| 157 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 158 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 159 | |
| 160 | def stopScapy( self ): |
| 161 | """ |
| 162 | Exit the Scapy cli |
| 163 | """ |
| 164 | try: |
| 165 | self.handle.sendline( "exit()" ) |
| 166 | self.handle.expect( self.hostPrompt ) |
| 167 | return main.TRUE |
| 168 | except pexpect.TIMEOUT: |
| 169 | main.log.exception( self.name + ": Command timed out" ) |
| 170 | return main.FALSE |
| 171 | except pexpect.EOF: |
| 172 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 173 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 174 | except Exception: |
| 175 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 176 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 177 | |
| 178 | def buildEther( self, **kwargs ): |
| 179 | """ |
| 180 | Build an Ethernet frame |
| 181 | |
| 182 | Will create a frame class with the given options. If a field is |
| 183 | left blank it will default to the below value unless it is |
| 184 | overwritten by the next frame. |
| 185 | Default frame: |
| 186 | ###[ Ethernet ]### |
| 187 | dst= ff:ff:ff:ff:ff:ff |
| 188 | src= 00:00:00:00:00:00 |
| 189 | type= 0x800 |
| 190 | |
| 191 | Returns main.TRUE or main.FALSE on error |
| 192 | """ |
| 193 | try: |
| 194 | # Set the Ethernet frame |
| 195 | cmd = 'ether = Ether( ' |
| 196 | options = [] |
| 197 | for key, value in kwargs.iteritems(): |
| 198 | if isinstance( value, str ): |
| 199 | value = '"' + value + '"' |
| 200 | options.append( str( key ) + "=" + str( value ) ) |
| 201 | cmd += ", ".join( options ) |
| 202 | cmd += ' )' |
| 203 | self.handle.sendline( cmd ) |
| 204 | self.handle.expect( self.scapyPrompt ) |
| 205 | if "Traceback" in self.handle.before: |
| 206 | # KeyError, SyntaxError, ... |
| 207 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 208 | return main.FALSE |
| 209 | self.handle.sendline( "packet = ether" ) |
| 210 | self.handle.expect( self.scapyPrompt ) |
| 211 | if "Traceback" in self.handle.before: |
| 212 | # KeyError, SyntaxError, ... |
| 213 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 214 | return main.FALSE |
| 215 | return main.TRUE |
| 216 | except pexpect.TIMEOUT: |
| 217 | main.log.exception( self.name + ": Command timed out" ) |
| 218 | return main.FALSE |
| 219 | except pexpect.EOF: |
| 220 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 221 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 222 | except Exception: |
| 223 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 224 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 225 | |
| 226 | def buildIP( self, **kwargs ): |
| 227 | """ |
| 228 | Build an IP frame |
| 229 | |
| 230 | Will create a frame class with the given options. If a field is |
| 231 | left blank it will default to the below value unless it is |
| 232 | overwritten by the next frame. |
| 233 | Default frame: |
| 234 | ###[ IP ]### |
| 235 | version= 4 |
| 236 | ihl= None |
| 237 | tos= 0x0 |
| 238 | len= None |
| 239 | id= 1 |
| 240 | flags= |
| 241 | frag= 0 |
| 242 | ttl= 64 |
| 243 | proto= hopopt |
| 244 | chksum= None |
| 245 | src= 127.0.0.1 |
| 246 | dst= 127.0.0.1 |
| 247 | \options\ |
| 248 | |
| 249 | Returns main.TRUE or main.FALSE on error |
| 250 | """ |
| 251 | try: |
| 252 | # Set the IP frame |
| 253 | cmd = 'ip = IP( ' |
| 254 | options = [] |
| 255 | for key, value in kwargs.iteritems(): |
| 256 | if isinstance( value, str ): |
| 257 | value = '"' + value + '"' |
| 258 | options.append( str( key ) + "=" + str( value ) ) |
| 259 | cmd += ", ".join( options ) |
| 260 | cmd += ' )' |
| 261 | self.handle.sendline( cmd ) |
| 262 | self.handle.expect( self.scapyPrompt ) |
| 263 | if "Traceback" in self.handle.before: |
| 264 | # KeyError, SyntaxError, ... |
| 265 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 266 | return main.FALSE |
| 267 | self.handle.sendline( "packet = ether/ip" ) |
| 268 | self.handle.expect( self.scapyPrompt ) |
| 269 | if "Traceback" in self.handle.before: |
| 270 | # KeyError, SyntaxError, ... |
| 271 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 272 | return main.FALSE |
| 273 | return main.TRUE |
| 274 | except pexpect.TIMEOUT: |
| 275 | main.log.exception( self.name + ": Command timed out" ) |
| 276 | return main.FALSE |
| 277 | except pexpect.EOF: |
| 278 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 279 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 280 | except Exception: |
| 281 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 282 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 283 | |
| 284 | def buildIPv6( self, **kwargs ): |
| 285 | """ |
| 286 | Build an IPv6 frame |
| 287 | |
| 288 | Will create a frame class with the given options. If a field is |
| 289 | left blank it will default to the below value unless it is |
| 290 | overwritten by the next frame. |
| 291 | Default frame: |
| 292 | ###[ IPv6 ]### |
| 293 | version= 6 |
| 294 | tc= 0 |
| 295 | fl= 0 |
| 296 | plen= None |
| 297 | nh= No Next Header |
| 298 | hlim= 64 |
| 299 | src= ::1 |
| 300 | dst= ::1 |
| 301 | |
| 302 | Returns main.TRUE or main.FALSE on error |
| 303 | """ |
| 304 | try: |
| 305 | # Set the IPv6 frame |
| 306 | cmd = 'ipv6 = IPv6( ' |
| 307 | options = [] |
| 308 | for key, value in kwargs.iteritems(): |
| 309 | if isinstance( value, str ): |
| 310 | value = '"' + value + '"' |
| 311 | options.append( str( key ) + "=" + str( value ) ) |
| 312 | cmd += ", ".join( options ) |
| 313 | cmd += ' )' |
| 314 | self.handle.sendline( cmd ) |
| 315 | self.handle.expect( self.scapyPrompt ) |
| 316 | if "Traceback" in self.handle.before: |
| 317 | # KeyError, SyntaxError, ... |
| 318 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 319 | return main.FALSE |
| 320 | self.handle.sendline( "packet = ether/ipv6" ) |
| 321 | self.handle.expect( self.scapyPrompt ) |
| 322 | if "Traceback" in self.handle.before: |
| 323 | # KeyError, SyntaxError, ... |
| 324 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 325 | return main.FALSE |
| 326 | return main.TRUE |
| 327 | except pexpect.TIMEOUT: |
| 328 | main.log.exception( self.name + ": Command timed out" ) |
| 329 | return main.FALSE |
| 330 | except pexpect.EOF: |
| 331 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 332 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 333 | except Exception: |
| 334 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 335 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 336 | |
| 337 | def buildTCP( self, ipVersion=4, **kwargs ): |
| 338 | """ |
| 339 | Build an TCP frame |
| 340 | |
| 341 | Will create a frame class with the given options. If a field is |
| 342 | left blank it will default to the below value unless it is |
| 343 | overwritten by the next frame. |
| 344 | |
| 345 | NOTE: Some arguments require quotes around them. It's up to you to |
| 346 | know which ones and to add them yourself. Arguments with an asterisk |
| 347 | do not need quotes. |
| 348 | |
| 349 | Options: |
| 350 | ipVersion - Either 4 (default) or 6, indicates what Internet Protocol |
| 351 | frame to use to encapsulate into |
| 352 | Default frame: |
| 353 | ###[ TCP ]### |
| 354 | sport= ftp_data * |
| 355 | dport= http * |
| 356 | seq= 0 |
| 357 | ack= 0 |
| 358 | dataofs= None |
| 359 | reserved= 0 |
| 360 | flags= S |
| 361 | window= 8192 |
| 362 | chksum= None |
| 363 | urgptr= 0 |
| 364 | options= {} |
| 365 | |
| 366 | Returns main.TRUE or main.FALSE on error |
| 367 | """ |
| 368 | try: |
| 369 | # Set the TCP frame |
| 370 | cmd = 'tcp = TCP( ' |
| 371 | options = [] |
| 372 | for key, value in kwargs.iteritems(): |
| 373 | options.append( str( key ) + "=" + str( value ) ) |
| 374 | cmd += ", ".join( options ) |
| 375 | cmd += ' )' |
| 376 | self.handle.sendline( cmd ) |
| 377 | self.handle.expect( self.scapyPrompt ) |
| 378 | if "Traceback" in self.handle.before: |
| 379 | # KeyError, SyntaxError, ... |
| 380 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 381 | return main.FALSE |
| 382 | if str( ipVersion ) is '4': |
| 383 | self.handle.sendline( "packet = ether/ip/tcp" ) |
| 384 | elif str( ipVersion ) is '6': |
| 385 | self.handle.sendline( "packet = ether/ipv6/tcp" ) |
| 386 | else: |
| 387 | main.log.error( "Unrecognized option for ipVersion, given " + |
| 388 | repr( ipVersion ) ) |
| 389 | return main.FALSE |
| 390 | self.handle.expect( self.scapyPrompt ) |
| 391 | if "Traceback" in self.handle.before: |
| 392 | # KeyError, SyntaxError, ... |
| 393 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 394 | return main.FALSE |
| 395 | return main.TRUE |
| 396 | except pexpect.TIMEOUT: |
| 397 | main.log.exception( self.name + ": Command timed out" ) |
| 398 | return main.FALSE |
| 399 | except pexpect.EOF: |
| 400 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 401 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 402 | except Exception: |
| 403 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 404 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 405 | |
| 406 | def buildUDP( self, ipVersion=4, **kwargs ): |
| 407 | """ |
| 408 | Build an UDP frame |
| 409 | |
| 410 | Will create a frame class with the given options. If a field is |
| 411 | left blank it will default to the below value unless it is |
| 412 | overwritten by the next frame. |
| 413 | |
| 414 | NOTE: Some arguments require quotes around them. It's up to you to |
| 415 | know which ones and to add them yourself. Arguments with an asterisk |
| 416 | do not need quotes. |
| 417 | |
| 418 | Options: |
| 419 | ipVersion - Either 4 (default) or 6, indicates what Internet Protocol |
| 420 | frame to use to encapsulate into |
| 421 | Default frame: |
| 422 | ###[ UDP ]### |
| 423 | sport= domain * |
| 424 | dport= domain * |
| 425 | len= None |
| 426 | chksum= None |
| 427 | |
| 428 | Returns main.TRUE or main.FALSE on error |
| 429 | """ |
| 430 | try: |
| 431 | # Set the UDP frame |
| 432 | cmd = 'udp = UDP( ' |
| 433 | options = [] |
| 434 | for key, value in kwargs.iteritems(): |
| 435 | options.append( str( key ) + "=" + str( value ) ) |
| 436 | cmd += ", ".join( options ) |
| 437 | cmd += ' )' |
| 438 | self.handle.sendline( cmd ) |
| 439 | self.handle.expect( self.scapyPrompt ) |
| 440 | if "Traceback" in self.handle.before: |
| 441 | # KeyError, SyntaxError, ... |
| 442 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 443 | return main.FALSE |
| 444 | if str( ipVersion ) is '4': |
| 445 | self.handle.sendline( "packet = ether/ip/udp" ) |
| 446 | elif str( ipVersion ) is '6': |
| 447 | self.handle.sendline( "packet = ether/ipv6/udp" ) |
| 448 | else: |
| 449 | main.log.error( "Unrecognized option for ipVersion, given " + |
| 450 | repr( ipVersion ) ) |
| 451 | return main.FALSE |
| 452 | self.handle.expect( self.scapyPrompt ) |
| 453 | if "Traceback" in self.handle.before: |
| 454 | # KeyError, SyntaxError, ... |
| 455 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 456 | return main.FALSE |
| 457 | return main.TRUE |
| 458 | except pexpect.TIMEOUT: |
| 459 | main.log.exception( self.name + ": Command timed out" ) |
| 460 | return main.FALSE |
| 461 | except pexpect.EOF: |
| 462 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 463 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 464 | except Exception: |
| 465 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 466 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 467 | |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 468 | def buildSCTP( self, ipVersion=4, **kwargs ): |
| 469 | """ |
| 470 | Build an SCTP frame |
| 471 | |
| 472 | Will create a frame class with the given options. If a field is |
| 473 | left blank it will default to the below value unless it is |
| 474 | overwritten by the next frame. |
| 475 | |
| 476 | NOTE: Some arguments require quotes around them. It's up to you to |
| 477 | know which ones and to add them yourself. Arguments with an asterisk |
| 478 | do not need quotes. |
| 479 | |
| 480 | Options: |
| 481 | ipVersion - Either 4 (default) or 6, indicates what Internet Protocol |
| 482 | frame to use to encapsulate into |
| 483 | Default frame: |
| 484 | ###[ SCTP ]### |
| 485 | sport= domain * |
| 486 | dport= domain * |
| 487 | tag = None |
| 488 | chksum = None |
| 489 | |
| 490 | Returns main.TRUE or main.FALSE on error |
| 491 | """ |
| 492 | try: |
| 493 | # Set the SCTP frame |
| 494 | cmd = 'sctp = SCTP( ' |
| 495 | options = [ ] |
| 496 | for key, value in kwargs.iteritems( ): |
| 497 | options.append( str( key ) + "=" + str( value ) ) |
| 498 | cmd += ", ".join( options ) |
| 499 | cmd += ' )' |
| 500 | self.handle.sendline( cmd ) |
| 501 | self.handle.expect( self.scapyPrompt ) |
| 502 | if "Traceback" in self.handle.before: |
| 503 | # KeyError, SyntaxError, ... |
| 504 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 505 | return main.FALSE |
| 506 | if str( ipVersion ) is '4': |
| 507 | self.handle.sendline( "packet = ether/ip/sctp" ) |
| 508 | elif str( ipVersion ) is '6': |
| 509 | self.handle.sendline( "packet = ether/ipv6/sctp" ) |
| 510 | else: |
| 511 | main.log.error( "Unrecognized option for ipVersion, given " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 512 | repr( ipVersion ) ) |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 513 | return main.FALSE |
| 514 | self.handle.expect( self.scapyPrompt ) |
| 515 | if "Traceback" in self.handle.before: |
| 516 | # KeyError, SyntaxError, ... |
| 517 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 518 | return main.FALSE |
| 519 | return main.TRUE |
| 520 | except pexpect.TIMEOUT: |
| 521 | main.log.exception( self.name + ": Command timed out" ) |
| 522 | return main.FALSE |
| 523 | except pexpect.EOF: |
| 524 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 525 | main.cleanAndExit() |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 526 | except Exception: |
| 527 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 528 | main.cleanAndExit() |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 529 | |
| 530 | def buildARP( self, **kwargs ): |
| 531 | """ |
| 532 | Build an ARP frame |
| 533 | |
| 534 | Will create a frame class with the given options. If a field is |
| 535 | left blank it will default to the below value unless it is |
| 536 | overwritten by the next frame. |
| 537 | |
| 538 | NOTE: Some arguments require quotes around them. It's up to you to |
| 539 | know which ones and to add them yourself. Arguments with an asterisk |
| 540 | do not need quotes. |
| 541 | |
| 542 | Default frame: |
| 543 | ###[ ARP ]### |
| 544 | hwtype : XShortField = (1) |
| 545 | ptype : XShortEnumField = (2048) |
| 546 | hwlen : ByteField = (6) |
| 547 | plen : ByteField = (4) |
| 548 | op : ShortEnumField = (1) |
| 549 | hwsrc : ARPSourceMACField = (None) |
| 550 | psrc : SourceIPField = (None) |
| 551 | hwdst : MACField = ('00:00:00:00:00:00') |
| 552 | pdst : IPField = ('0.0.0.0') |
| 553 | |
| 554 | Returns main.TRUE or main.FALSE on error |
| 555 | """ |
| 556 | try: |
| 557 | # Set the ARP frame |
| 558 | cmd = 'arp = ARP( ' |
| 559 | options = [] |
| 560 | for key, value in kwargs.iteritems( ): |
| 561 | if isinstance( value, str ): |
| 562 | value = '"' + value + '"' |
| 563 | options.append( str( key ) + "=" + str( value ) ) |
| 564 | cmd += ", ".join( options ) |
| 565 | cmd += ' )' |
| 566 | self.handle.sendline( cmd ) |
| 567 | self.handle.expect( self.scapyPrompt ) |
| 568 | if "Traceback" in self.handle.before: |
| 569 | # KeyError, SyntaxError, ... |
| 570 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 571 | return main.FALSE |
| 572 | self.handle.sendline( "packet = ether/arp" ) |
| 573 | self.handle.expect( self.scapyPrompt ) |
| 574 | if "Traceback" in self.handle.before: |
| 575 | # KeyError, SyntaxError, ... |
| 576 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 577 | return main.FALSE |
| 578 | return main.TRUE |
| 579 | except pexpect.TIMEOUT: |
| 580 | main.log.exception( self.name + ": Command timed out" ) |
| 581 | return main.FALSE |
| 582 | except pexpect.EOF: |
| 583 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 584 | main.cleanAndExit() |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 585 | except Exception: |
| 586 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 587 | main.cleanAndExit() |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 588 | |
| 589 | def buildICMP( self, ipVersion=4, **kwargs ): |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 590 | """ |
| 591 | Build an ICMP frame |
| 592 | |
| 593 | Will create a frame class with the given options. If a field is |
| 594 | left blank it will default to the below value unless it is |
| 595 | overwritten by the next frame. |
| 596 | Default frame: |
| 597 | ###[ ICMP ]### |
| 598 | type= echo-request |
| 599 | code= 0 |
| 600 | chksum= None |
| 601 | id= 0x0 |
| 602 | seq= 0x0 |
| 603 | |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 604 | Options: |
| 605 | ipVersion - Either 4 (default) or 6, indicates what Internet Protocol |
| 606 | frame to use to encapsulate into |
| 607 | |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 608 | Returns main.TRUE or main.FALSE on error |
| 609 | """ |
| 610 | try: |
| 611 | # Set the ICMP frame |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 612 | if str( ipVersion ) is '4': |
| 613 | cmd = 'icmp = ICMP( ' |
| 614 | elif str( ipVersion ) is '6': |
| 615 | cmd = 'icmp6 = ICMPv6EchoReply( ' |
| 616 | else: |
| 617 | main.log.error( "Unrecognized option for ipVersion, given " + |
| 618 | repr( ipVersion ) ) |
| 619 | return main.FALSE |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 620 | options = [] |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 621 | for key, value in kwargs.iteritems( ): |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 622 | if isinstance( value, str ): |
| 623 | value = '"' + value + '"' |
| 624 | options.append( str( key ) + "=" + str( value ) ) |
| 625 | cmd += ", ".join( options ) |
| 626 | cmd += ' )' |
| 627 | self.handle.sendline( cmd ) |
| 628 | self.handle.expect( self.scapyPrompt ) |
| 629 | if "Traceback" in self.handle.before: |
| 630 | # KeyError, SyntaxError, ... |
| 631 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 632 | return main.FALSE |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 633 | |
| 634 | if str( ipVersion ) is '4': |
| 635 | self.handle.sendline( "packet = ether/ip/icmp" ) |
| 636 | elif str( ipVersion ) is '6': |
| 637 | self.handle.sendline( "packet = ether/ipv6/icmp6" ) |
| 638 | else: |
| 639 | main.log.error( "Unrecognized option for ipVersion, given " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 640 | repr( ipVersion ) ) |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 641 | return main.FALSE |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 642 | self.handle.expect( self.scapyPrompt ) |
| 643 | if "Traceback" in self.handle.before: |
| 644 | # KeyError, SyntaxError, ... |
| 645 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 646 | return main.FALSE |
| 647 | return main.TRUE |
| 648 | except pexpect.TIMEOUT: |
| 649 | main.log.exception( self.name + ": Command timed out" ) |
| 650 | return main.FALSE |
| 651 | except pexpect.EOF: |
| 652 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 653 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 654 | except Exception: |
| 655 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 656 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 657 | |
| 658 | def sendPacket( self, iface=None, packet=None, timeout=1 ): |
| 659 | """ |
| 660 | Send a packet with either the given scapy packet command, or use the |
| 661 | packet saved in the variable 'packet'. |
| 662 | |
| 663 | Examples of a valid string for packet: |
| 664 | |
| 665 | Simple IP packet |
| 666 | packet='Ether(dst="a6:d9:26:df:1d:4b")/IP(dst="10.0.0.2")' |
| 667 | |
| 668 | A Ping with two vlan tags |
| 669 | packet='Ether(dst='ff:ff:ff:ff:ff:ff')/Dot1Q(vlan=1)/Dot1Q(vlan=10)/ |
| 670 | IP(dst='255.255.255.255', src='192.168.0.1')/ICMP()' |
| 671 | |
| 672 | Returns main.TRUE or main.FALSE on error |
| 673 | """ |
| 674 | try: |
| 675 | # TODO: add all params, or use kwargs |
| 676 | sendCmd = 'srp( ' |
| 677 | if packet: |
| 678 | sendCmd += packet |
| 679 | else: |
| 680 | sendCmd += "packet" |
| 681 | if iface: |
| 682 | sendCmd += ", iface='{}'".format( iface ) |
| 683 | |
| 684 | sendCmd += ', timeout=' + str( timeout ) + ')' |
| 685 | self.handle.sendline( sendCmd ) |
| 686 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | a510a8a | 2016-05-04 15:09:28 -0700 | [diff] [blame] | 687 | # main.log.warn( "Send packet response: {}".format( self.handle.before ) ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 688 | if "Traceback" in self.handle.before: |
| 689 | # KeyError, SyntaxError, ... |
| 690 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 691 | return main.FALSE |
| 692 | # TODO: Check # of packets sent? |
| 693 | return main.TRUE |
| 694 | except pexpect.TIMEOUT: |
| 695 | main.log.exception( self.name + ": Command timed out" ) |
| 696 | return main.FALSE |
| 697 | except pexpect.EOF: |
| 698 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 699 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 700 | except Exception: |
| 701 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 702 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 703 | |
| 704 | def startFilter( self, ifaceName=None, sniffCount=1, pktFilter="ip" ): |
| 705 | """ |
| 706 | Listen for packets using the given filters |
| 707 | |
| 708 | Options: |
| 709 | ifaceName - the name of the interface to listen on. If none is given, |
You Wang | dafb6e2 | 2018-01-22 17:01:00 -0800 | [diff] [blame] | 710 | defaults to self.ifaceName which is <host name>-eth0 |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 711 | pktFilter - A string in Berkeley Packet Filter (BPF) format which |
| 712 | specifies which packets to sniff |
| 713 | sniffCount - The number of matching packets to capture before returning |
| 714 | |
| 715 | Returns main.TRUE or main.FALSE on error |
| 716 | """ |
| 717 | try: |
| 718 | # TODO: add all params, or use kwargs |
You Wang | dafb6e2 | 2018-01-22 17:01:00 -0800 | [diff] [blame] | 719 | ifaceName = str( ifaceName ) if ifaceName else self.ifaceName |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 720 | # Set interface |
| 721 | self.handle.sendline( ' conf.iface = "' + ifaceName + '"' ) |
| 722 | self.handle.expect( self.scapyPrompt ) |
| 723 | cmd = 'pkt = sniff(count = ' + str( sniffCount ) +\ |
| 724 | ', filter = "' + str( pktFilter ) + '")' |
Jon Hall | a510a8a | 2016-05-04 15:09:28 -0700 | [diff] [blame] | 725 | main.log.info( "Filter on " + self.name + ' > ' + cmd ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 726 | self.handle.sendline( cmd ) |
| 727 | self.handle.expect( '"\)\r\n' ) |
| 728 | # TODO: parse this? |
| 729 | return main.TRUE |
| 730 | except pexpect.TIMEOUT: |
| 731 | main.log.exception( self.name + ": Command timed out" ) |
| 732 | return main.FALSE |
| 733 | except pexpect.EOF: |
| 734 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 735 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 736 | except Exception: |
| 737 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 738 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 739 | |
| 740 | def checkFilter( self, timeout=10 ): |
| 741 | """ |
| 742 | Check that a filter returned and returns the reponse |
| 743 | """ |
| 744 | try: |
| 745 | i = self.handle.expect( [ self.scapyPrompt, pexpect.TIMEOUT ], timeout=timeout ) |
| 746 | if i == 0: |
| 747 | return main.TRUE |
| 748 | else: |
| 749 | return main.FALSE |
| 750 | except pexpect.EOF: |
| 751 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 752 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 753 | except Exception: |
| 754 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 755 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 756 | |
| 757 | def killFilter( self ): |
| 758 | """ |
| 759 | Kill a scapy filter |
| 760 | """ |
| 761 | try: |
| 762 | self.handle.send( "\x03" ) # Send a ctrl-c to kill the filter |
| 763 | self.handle.expect( self.scapyPrompt ) |
| 764 | return self.handle.before |
| 765 | except pexpect.TIMEOUT: |
| 766 | main.log.exception( self.name + ": Command timed out" ) |
| 767 | return None |
| 768 | except pexpect.EOF: |
| 769 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 770 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 771 | except Exception: |
| 772 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 773 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 774 | |
| 775 | def readPackets( self ): |
| 776 | """ |
| 777 | Read all the packets captured by the previous filter |
| 778 | """ |
| 779 | try: |
| 780 | self.handle.sendline( "for p in pkt: p \n") |
| 781 | self.handle.expect( "for p in pkt: p \r\n... \r\n" ) |
| 782 | self.handle.expect( self.scapyPrompt ) |
| 783 | except pexpect.TIMEOUT: |
| 784 | main.log.exception( self.name + ": Command timed out" ) |
| 785 | return None |
| 786 | except pexpect.EOF: |
| 787 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 788 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 789 | except Exception: |
| 790 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 791 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 792 | return self.handle.before |
| 793 | |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 794 | def updateSelf( self, IPv6=False ): |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 795 | """ |
| 796 | Updates local MAC and IP fields |
| 797 | """ |
| 798 | self.hostMac = self.getMac() |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 799 | if IPv6: |
| 800 | self.hostIp = self.getIp( IPv6=True ) |
| 801 | else: |
| 802 | self.hostIp = self.getIp() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 803 | |
| 804 | def getMac( self, ifaceName=None ): |
| 805 | """ |
| 806 | Save host's MAC address |
| 807 | """ |
| 808 | try: |
You Wang | dafb6e2 | 2018-01-22 17:01:00 -0800 | [diff] [blame] | 809 | ifaceName = str( ifaceName ) if ifaceName else self.ifaceName |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 810 | cmd = 'get_if_hwaddr("' + str( ifaceName ) + '")' |
| 811 | self.handle.sendline( cmd ) |
| 812 | self.handle.expect( self.scapyPrompt ) |
| 813 | pattern = r'(([0-9a-f]{2}[:-]){5}([0-9a-f]{2}))' |
| 814 | match = re.search( pattern, self.handle.before ) |
| 815 | if match: |
| 816 | return match.group() |
| 817 | else: |
| 818 | # the command will have an exception if iface doesn't exist |
| 819 | return None |
| 820 | except pexpect.TIMEOUT: |
| 821 | main.log.exception( self.name + ": Command timed out" ) |
| 822 | return None |
| 823 | except pexpect.EOF: |
| 824 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 825 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 826 | except Exception: |
| 827 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 828 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 829 | |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 830 | def getIp( self, ifaceName=None, IPv6=False ): |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 831 | """ |
| 832 | Save host's IP address |
| 833 | |
| 834 | Returns the IP of the first interface that is not a loopback device. |
| 835 | If no IP could be found then it will return 0.0.0.0. |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 836 | |
| 837 | If IPv6 is equal to True, returns IPv6 of the first interface that is not a loopback device. |
| 838 | If no IPv6 could be found then it will return :: . |
| 839 | |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 840 | """ |
| 841 | def getIPofInterface( ifaceName ): |
| 842 | cmd = 'get_if_addr("' + str( ifaceName ) + '")' |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 843 | if IPv6: |
| 844 | cmd = 'get_if_raw_addr6("' + str( ifaceName ) + '")' |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 845 | self.handle.sendline( cmd ) |
| 846 | self.handle.expect( self.scapyPrompt ) |
| 847 | |
| 848 | pattern = r'(((2[0-5]|1[0-9]|[0-9])?[0-9]\.){3}((2[0-5]|1[0-9]|[0-9])?[0-9]))' |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 849 | if IPv6: |
| 850 | pattern = r'(\\x([0-9]|[a-f]|[A-F])([0-9]|[a-f]|[A-F])){16}' |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 851 | match = re.search( pattern, self.handle.before ) |
| 852 | if match: |
| 853 | # NOTE: The command will return 0.0.0.0 if the iface doesn't exist |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 854 | if IPv6 is not True: |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 855 | if match.group() == '0.0.0.0': |
| 856 | main.log.warn( 'iface {0} has no IPv4 address'.format( ifaceName ) ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 857 | return match.group() |
| 858 | else: |
| 859 | return None |
| 860 | try: |
| 861 | if not ifaceName: |
| 862 | # Get list of interfaces |
| 863 | ifList = self.getIfList() |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 864 | if IPv6: |
| 865 | for ifaceName in ifList: |
| 866 | if ifaceName == "lo": |
| 867 | continue |
| 868 | ip = getIPofInterface( ifaceName ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 869 | if ip is not None: |
| 870 | newip = ip |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 871 | tmp = newip.split( "\\x" ) |
| 872 | ip = "" |
| 873 | counter = 0 |
| 874 | for i in tmp: |
| 875 | if i != "": |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 876 | counter = counter + 1 |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 877 | if counter % 2 == 0 and counter < 16: |
| 878 | ip = ip + i + ":" |
| 879 | else: |
| 880 | ip = ip + i |
| 881 | return ip |
| 882 | return "::" |
| 883 | else: |
| 884 | for ifaceName in ifList: |
| 885 | if ifaceName == "lo": |
| 886 | continue |
| 887 | ip = getIPofInterface( ifaceName ) |
| 888 | if ip != "0.0.0.0": |
| 889 | return ip |
| 890 | return "0.0.0.0" |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 891 | else: |
| 892 | return getIPofInterface( ifaceName ) |
| 893 | |
| 894 | except pexpect.TIMEOUT: |
| 895 | main.log.exception( self.name + ": Command timed out" ) |
| 896 | return None |
| 897 | except pexpect.EOF: |
| 898 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 899 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 900 | except Exception: |
| 901 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 902 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 903 | |
| 904 | def getIfList( self ): |
| 905 | """ |
| 906 | Return List of Interfaces |
| 907 | """ |
| 908 | try: |
| 909 | self.handle.sendline( 'get_if_list()' ) |
| 910 | self.handle.expect( self.scapyPrompt ) |
| 911 | ifList = self.handle.before.split( '\r\n' ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 912 | ifList = ifList[ 1 ].replace( "'", "" )[ 1:-1 ].split( ', ' ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 913 | return ifList |
| 914 | |
| 915 | except pexpect.TIMEOUT: |
| 916 | main.log.exception( self.name + ": Command timed out" ) |
| 917 | return None |
| 918 | except pexpect.EOF: |
| 919 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 920 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 921 | except Exception: |
| 922 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 923 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 924 | |
| 925 | if __name__ != "__main__": |
| 926 | sys.modules[ __name__ ] = ScapyCliDriver() |