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