blob: eeb15049667a3a4c1943f30ad981fed3c96b5665 [file] [log] [blame]
admin9529d112013-11-22 15:00:05 -08001#! /usr/bin/env python
2import json
3import os
4import sys
5
6
7
8def find_host(RestIP,RestPort,RestAPI,hostMAC):
9 retcode = 0
10 url ="http://%s:%s/wm/device/" %(RestIP,RestPort)
11
12 try:
13 command = "curl -s %s" % (url)
14 result = os.popen(command).read()
15 parsedResult = json.loads(result)
16 except:
17 print "REST IF %s has issue" % command
18 parsedResult = ""
19
20 if type(parsedResult) == 'dict' and parsedResult.has_key('code'):
21 print "REST %s returned code %s" % (command, parsedResult['code'])
22 parsedResult = ""
23
24
25
26 if parsedResult == "":
27 return (retcode, "Rest API has an error")
28 else:
29 found = [item for item in parsedResult if item['mac'] == [str(hostMAC)]]
30 retcode = 1
31 return (retcode, found)
32
33
34if __name__ == "__main__":
35 ip = "10.128.100.1"
36 port = 8080
37 hostMAC = "00:00:00:00:00:06"
38 RestAPI = "/wm/device/"
39 Reststat,Hoststat = find_host(ip,port,RestAPI,hostMAC)
40
41 if Reststat == 1:
42 print "Found device with MAC:" + hostMAC +" attached to switch(DPID):" + str(Hoststat[0]['attachmentPoint'][0]['switchDPID'])
43 else:
44 print " Device with MAC:" + hostMAC + " is not found!"