Reputation: 3209
I am not able to print the IP inside the following IF condition
def call_openNMS_service(node):
#print node
url = "http://localhost:8980/opennms/rest/nodes/{}/ipinterfaces/{}/services"
dict_urls = [url.format(i, j) for i, j in zip(node["id"], node["ip"])]
print dict_urls
headers = {"Accept": "application/json"}
for url in dict_urls:
x = requests.get(url, headers=headers, auth=("admin", "0p35"))
parsed = json.loads(x.content)
##print json.dumps(parsed, indent=4, sort_keys=True)
for i in parsed["service"]:
#print i["serviceType"]["name"]
if i["serviceType"]["name"] == "SSH": # So if i find SSH i print the node[ip]
print node["ip"] ## this is where it should just print the one IP which has SSH running
else:
print "No IP found with SSH running"
but this prints me the dicts of ip , not the particular IP
node = {
'ip': [u'10.21.204.174', u'10.21.205.148', u'10.21.50.153', u'10.21.50.192', u'10.21.50.198', u'10.21.51.149', u'10.21.51.158', u'10.21.51.200', u'10.21.51.252', u'10.21.52.202', u'10.21.52.53', u'10.21.54.12', u'10.21.54.149', u'10.21.55.132', u'10.21.55.176', u'10.21.55.239', u'10.21.56.169', u'10.21.56.48', u'10.21.65.106', u'10.21.65.125', u'10.21.65.34', u'10.21.67.131', u'10.21.67.179', u'10.21.67.194', u'10.21.67.230', u'10.21.67.249', u'10.21.67.45', u'10.21.67.70', u'10.21.68.127', u'10.21.68.180', u'10.21.68.73', u'10.21.69.200', u'10.21.69.38', u'10.21.70.121', u'10.21.70.56'],
'id': [u'564', u'561', u'462', u'389', u'352', u'353', u'390', u'354', u'356', u'454', u'348', u'349', u'455', u'563', u'359', u'360', u'363', u'362', u'525', u'426', u'503', u'466', u'431', u'527', u'529', u'373', u'414', u'518', u'430', u'425', u'413', u'368', u'404', u'517', u'502']
}
Upvotes: 0
Views: 88
Reputation: 7880
As written, the only place you have the individual IP in that for
loop is embedded in url
. Looks like you'd be better off looping over the zip()
itself instead of using dict_urls
as a middleman. Lose these two lines:
dict_urls = [url.format(i,j) for i,j in zip(node['id'], node['ip'])]
print dict_urls
Then modify your for
loop to loop over the node
elements and form your URL in the loop:
for nodeid, nodeip in zip(node['id'], node['ip']):
nodeurl = url.format(nodeid, nodeip)
Replace all references to url
in the loop with nodeurl
(needed to change the name so you don't overwrite your url
template). Then when you want to print the IP, just:
print nodeip
Here's how the final modified code would look:
def call_openNMS_service(node):
url = "http://localhost:8980/opennms/rest/nodes/{}/ipinterfaces/{}/services"
headers = {"Accept": "application/json"}
for nodeid, nodeip in zip(node['id'], node['ip']):
nodeurl = url.format(nodeid, nodeip)
x = requests.get(nodeurl, headers=headers, auth=("admin", "0p35"))
parsed = json.loads(x.content)
for i in parsed["service"]:
if i["serviceType"]["name"] == "SSH":
print nodeip
else:
print "No IP found with SSH running"
Upvotes: 3