blob: 6abd35651991af9e3620ab71f3cb832fe6895ac8 [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
39def get_controllers():
40 controllers = os.getenv('ONOS_CONTROLLER_IP') or 'localhost'
41 return controllers.split(',')
42
43def get_controller():
44 controllers = get_controllers()
45 return controllers[0]