blob: 59b785d3db74ee8ab2ee6dc18b48730c1548ca80 [file] [log] [blame]
A.R Karthickbe7768c2017-03-17 11:39:41 -07001import subprocess
2import socket
3import fcntl
4import struct
5import os
6
7# we use subprocess as commands.getstatusoutput would be deprecated
8def 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
14def 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
25def 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 Karthick33cfdbe2017-03-17 18:03:48 -070039def 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 Karthickbe7768c2017-03-17 11:39:41 -070050def get_controllers():
51 controllers = os.getenv('ONOS_CONTROLLER_IP') or 'localhost'
52 return controllers.split(',')
53
54def get_controller():
55 controllers = get_controllers()
56 return controllers[0]