A.R Karthick | be7768c | 2017-03-17 11:39:41 -0700 | [diff] [blame] | 1 | import subprocess |
| 2 | import socket |
| 3 | import fcntl |
| 4 | import struct |
| 5 | import os |
A R Karthick | 933f5b5 | 2017-03-27 15:27:16 -0700 | [diff] [blame] | 6 | import logging |
| 7 | |
| 8 | log_test = logging.getLogger('cordTester') |
| 9 | test_consolehandler = logging.StreamHandler() |
| 10 | #test_consolehandler.setFormatter(logging.Formatter("%(levelname)s:%(message)s")) |
| 11 | log_test.addHandler(test_consolehandler) |
A.R Karthick | be7768c | 2017-03-17 11:39:41 -0700 | [diff] [blame] | 12 | |
| 13 | # we use subprocess as commands.getstatusoutput would be deprecated |
| 14 | def getstatusoutput(cmd): |
| 15 | command = [ '/bin/sh', '-c', cmd ] |
| 16 | p = subprocess.Popen(command, stdout = subprocess.PIPE) |
| 17 | out, _ = p.communicate() |
| 18 | return p.returncode, out.strip() |
| 19 | |
| 20 | def get_ip(iface): |
| 21 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 22 | try: |
| 23 | info = fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', bytes(iface[:15]))) |
| 24 | except: |
| 25 | info = None |
| 26 | s.close() |
| 27 | if info: |
| 28 | return '.'.join( [ str(ord(c)) for c in info[20:24] ] ) |
| 29 | return None |
| 30 | |
| 31 | def get_mac(iface = None, pad = 4): |
| 32 | if iface is None: |
| 33 | iface = os.getenv('TEST_SWITCH', 'ovsbr0') |
| 34 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 35 | try: |
| 36 | info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', bytes(iface[:15]))) |
| 37 | except: |
| 38 | info = ['0'] * 24 |
| 39 | s.close() |
| 40 | sep = '' |
| 41 | if pad == 0: |
| 42 | sep = ':' |
| 43 | return '0'*pad + sep.join(['%02x' %ord(char) for char in info[18:24]]) |
| 44 | |
A.R Karthick | 33cfdbe | 2017-03-17 18:03:48 -0700 | [diff] [blame] | 45 | def get_default_gw(): |
| 46 | cmd = "ip route show | grep default | head -1 | awk '{print $3}'" |
| 47 | cmd_dev = "ip route show | grep default | head -1 | awk '{print $NF}'" |
| 48 | st, gw = getstatusoutput(cmd) |
| 49 | st2, gw_device = getstatusoutput(cmd_dev) |
| 50 | if st != 0: |
| 51 | gw = None |
| 52 | if st2 != 0: |
| 53 | gw_device = None |
| 54 | return gw, gw_device |
| 55 | |
A.R Karthick | be7768c | 2017-03-17 11:39:41 -0700 | [diff] [blame] | 56 | def get_controllers(): |
| 57 | controllers = os.getenv('ONOS_CONTROLLER_IP') or 'localhost' |
| 58 | return controllers.split(',') |
| 59 | |
| 60 | def get_controller(): |
| 61 | controllers = get_controllers() |
| 62 | return controllers[0] |