Python Forum

Full Version: python list iter issue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
from easysnmp import snmp_get, snmp_set, snmp_walk
from collections import Counter
import sys
#start = time.time()
host = '172.19.2.130'
interface = []
interfaceoid = '1.3.6.1.2.1.2.2.1.1'
indexwalk = snmp_walk(interfaceoid, hostname=host, community='sky123', version=2)
interfaces = [item.value for item in indexwalk]
interface.append(interfaces)

for x in iter(interface):
    b = interfaceoid+'.'+ ''.join(x)
    print (b)
    
Output:
1.3.6.1.2.1.2.2.1.1.285278721285278722285278723285278724285278725285278726285278727285278728285278729285278730285278731285278732285278733285278734285278735285278736285278977285278978285278979285278980285278981285278982285278983285278984285278985285278986285278987285278988285278989285278990285278991285278992285280769285280770285280771285280772285280773285281025285281026285281027285281028285281029285282817553648129654311425
expected output is

Output:
1.3.6.1.2.1.2.2.1.1.285278721 1.3.6.1.2.1.2.2.1.1.285278722 1.3.6.1.2.1.2.2.1.1.285278723 1.3.6.1.2.1.2.2.1.1.285278724
Print x for each pass through the for so you know what is happening. There is no input in the post and none of us are psychic so we can't run the program to see what is happening.
(Apr-05-2018, 06:18 PM)anna Wrote: [ -> ]
interfaces = [item.value for item in indexwalk]
interface.append(interfaces)
 
for x in iter(interface):
You create a list of items you call interfaces, and then append it to an empty list called interface. So, interface is a list with only one element, and that element happens to be a list of a lot of things. Then you enter a for loop, looping over the list that only has one element, so x is a list of all your interfaces.

Output makes sense, given what you're iterating over.

You could have easily determined this yourself by using a non-empty string for your joining. ie: '!!'.join(x)
Output:
[u'285278721', u'285278722', u'285278723', u'285278724', u'285278725', u'285278726', u'285278727', u'285278728', u'285278729', u'285278730', u'285278731', u'285278732', u'285278733', u'285278734', u'285278735', u'285278736', u'285278977', u'285278978', u'285278979', u'285278980', u'285278981', u'285278982', u'285278983', u'285278984', u'285278985', u'285278986', u'285278987', u'285278988', u'285278989', u'285278990', u'285278991', u'285278992', u'285280769', u'285280770', u'285280771', u'285280772', u'285280773', u'285281025', u'285281026', u'285281027', u'285281028', u'285281029', u'285282817', u'553648129', u'654311425']
this is list of interfaces, need to append to oid.

b = interfaceoid+'.'+ ''.join(x)
current output

Output:
1.3.6.1.2.1.2.2.1.1.285278721285278722285278723285278724285278725285278726285278727285278728285278729285278730285278731285278732285278733285278734285278735285278736285278977285278978285278979285278980285278981285278982285278983285278984285278985285278986285278987285278988285278989285278990285278991285278992285280769285280770285280771285280772285280773285281025285281026285281027285281028285281029285282817553648129654311425
its appending to oid single time only, where am i going wrong?

expected output is

Output:
1.3.6.1.2.1.2.2.1.1.285278721 1.3.6.1.2.1.2.2.1.1.285278722 1.3.6.1.2.1.2.2.1.1.285278723 1.3.6.1.2.1.2.2.1.1.285278724
interface = [u'285278721', u'285278722', u'285278723', u'285278724', u'285278725']
interfaceoid = '1.3.6.1.2.1.2.2.1.1'
for value in interface:
    print('{}.{}'.format(interfaceoid, value))
Output:
1.3.6.1.2.1.2.2.1.1.285278721 1.3.6.1.2.1.2.2.1.1.285278722 1.3.6.1.2.1.2.2.1.1.285278723 1.3.6.1.2.1.2.2.1.1.285278724 1.3.6.1.2.1.2.2.1.1.285278725
(Apr-06-2018, 01:45 AM)anna Wrote: [ -> ]its appending to oid single time only, where am i going wrong?
I get from your post that you didn't read my response. So I'll try again, by showing you your code, with a few things reorganized to be more obvious:
things = [item.value for item in indexwalk]
interface = [things]

for x in interface:
You're iterating over a list that only has one element. If you iterate over all the interfaces instead, it'll work as you intend.
Wavic,Buran,nimalo,

Sorry, was out of station.