Python Forum

Full Version: how to combine mumtiple for loops in single loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
from easysnmp import Session
import time
start = time.time()
host = '10.10.10.1'
intnames = []
intOpes  = []
intAdm   = []
session = Session(hostname=host, community='xxxxxx', version=2)
interface_index = session.walk('1.3.6.1.2.1.2.2.1.2')
interface_opes  = session.walk('1.3.6.1.2.1.2.2.1.7')
interface_adm   = session.walk('1.3.6.1.2.1.2.2.1.8')
for item_int in interface_index:
    intnames.append(item_int)
for item_ops in interface_opes:
    intOpes.append(item_ops)
for item_adm in interface_adm:
    intAdm.append(item_adm)

for i in range(len(intnames)):
    print '{} {} {} {}'.format(host,intnames[i].value,intOpes[i].value,intAdm[i].value)
print('It took', time.time()-start, 'seconds.')
how can i combine for loops?
You can shorten this in
intnames = list(interface_index)
intOpes = list(interface_opes)
intAdms = list(interface_adm)
or even
intnames, intOpes, intAdms = (list(session.walk('1.3.6.1.2.1.2.2.1.' + c)) for c in '278')
from easysnmp import Session
import time
start = time.time()
host = '10.10.10.1'
intnames = []
intOpes  = []
intAdm   = []
session = Session(hostname=host, community='xxxxxx', version=2)
intnames, intOpes, intAdms = (list(session.walk('1.3.6.1.2.1.2.2.1.' + c)) for c in '278')
#interface_index = session.walk('1.3.6.1.2.1.2.2.1.2')
#interface_opes  = session.walk('1.3.6.1.2.1.2.2.1.7')
#interface_adm   = session.walk('1.3.6.1.2.1.2.2.1.8')
for item_int in interface_index:
    intnames.append(item_int)
for item_ops in interface_opes:
    intOpes.append(item_ops)
for item_adm in interface_adm:
    intAdm.append(item_adm)

for i in range(len(intnames)):
#         if 'Vlan' in intnames[i].value:
#             continue
#         if 'StackPort' in intnames[i].value:
#             continue
#         else:
    print '{} {} {} {}'.format(host,intnames[i].value,intOpes[i].value,intAdm[i].value)
print('It took', time.time()-start, 'seconds.')
Error:
Traceback (most recent call last): File "python-snmp.py", line 9, in <module> intnames, intOpes, intAdms = (list(session.walk('1.3.6.1.2.1.2.2.1.' + c)) for c in '278') File "python-snmp.py", line 9, in <genexpr> intnames, intOpes, intAdms = (list(session.walk('1.3.6.1.2.1.2.2.1.' + c)) for c in '278') File "build/bdist.linux-x86_64/egg/easysnmp/session.py", line 467, in walk easysnmp.exceptions.EasySNMPTimeoutError: timed out while connecting to remote host
showing error

Sorry... there was some mistake in snmpcommunity.
Is this correct implementation of your suggestion?
from easysnmp import Session
import time
start = time.time()
host = '172.21.160.3'
intnames = []
intOpes  = []
intAdm   = []
session = Session(hostname=host, community='readvsnl', version=2)
intnames, intOpes, intAdms = (list(session.walk('1.3.6.1.2.1.2.2.1.' + c)) for c in '278')
#interface_index = session.walk('1.3.6.1.2.1.2.2.1.2')
#interface_opes  = session.walk('1.3.6.1.2.1.2.2.1.7')
#interface_adm   = session.walk('1.3.6.1.2.1.2.2.1.8')
#for item_int in interface_index:
#    intnames.append(item_int)
#for item_ops in interface_opes:
#    intOpes.append(item_ops)
#for item_adm in interface_adm:
#    intAdm.append(item_adm)

for i in range(len(intnames)):
#         if 'Vlan' in intnames[i].value:
#             continue
#         if 'StackPort' in intnames[i].value:
#             continue
#         else:
    print '{} {} {} {}'.format(host,intnames[i].value,intOpes[i].value,intAdm[i].value)
print('It took', time.time()-start, 'seconds.')
Error:
Traceback (most recent call last): File "python-snmp.py", line 26, in <module> print '{} {} {} {}'.format(host,intnames[i].value,intOpes[i].value,intAdm[i].value) IndexError: list index out of range
I only mean this
from easysnmp import Session
import time
start = time.time()
host = '10.10.10.1'

session = Session(hostname=host, community='xxxxxx', version=2)
intnames, intOpes, intAdms = (list(session.walk('1.3.6.1.2.1.2.2.1.' + c)) for c in '278')
 
for i in range(len(intnames)):
    print '{} {} {} {}'.format(host,intnames[i].value,intOpes[i].value,intAdm[i].value)
print('It took', time.time()-start, 'seconds.')
Your error has to do with the session.walk() call. Are you sure the remote host is reachable?

About the list index error, do you know if the three lists have equal length ?
(Mar-01-2018, 12:41 PM)Gribouillis Wrote: [ -> ]I only mean this
from easysnmp import Session
import time
start = time.time()
host = '10.10.10.1'

session = Session(hostname=host, community='xxxxxx', version=2)
intnames, intOpes, intAdms = (list(session.walk('1.3.6.1.2.1.2.2.1.' + c)) for c in '278')
 
for i in range(len(intnames)):
    print '{} {} {} {}'.format(host,intnames[i].value,intOpes[i].value,intAdm[i].value)
print('It took', time.time()-start, 'seconds.')
Your error has to do with the session.walk() call. Are you sure the remote host is reachable?

About the list index error, do you know if the three lists have equal length ?


Thanks.. its working, there was some snmp community issue.

can you please explain, this code "list(session.walk('1.3.6.1.2.1.2.2.1.' + c)) for c in '278'"

Quote:do you know if the three lists have equal length ?
yes... same lennth
(Mar-01-2018, 02:30 PM)anna Wrote: [ -> ]can you please explain, this code "list(session.walk('1.3.6.1.2.1.2.2.1.' + c)) for c in '278'"
Well this is a generator expression such as
(expression(c) for c in '278')
which value is an iterable yielding expression('2'), expression('7'), expression('8').
Here, expression('x') is list(session.walk('1.3.6.1.2.1.2.2.1.x')).

You could perhaps improve things if the values are attributes name, op, adm of the same interface object by instanciating such objects with the help of namedtuples:
from collections import namedtuple
Interface = namedtuple('Interface', 'name op adm')
inters = [Interface(*uple) for uple in zip(*[intnames, intOpes, intAdm])] # a list of interfaces
for inte in inters:
    print '{} {} {} {}'.format(host, inte.name.value, inte.op.value, inte.adm.value)
Hi Gribouillis,

what could the expression statement if OID last octate is double digit.

like 1.3.6.1.2.1.2.2.1.18, 1.3.6.1.2.1.2.2.1.19, 1.3.6.1.2.1.2.2.1.20

Regards
Anna Patil
(Mar-05-2018, 11:20 AM)anna Wrote: [ -> ]what could the expression statement if OID last octate is double digit.
You can write
(list(session.walk('1.3.6.1.2.1.2.2.1.' + c)) for c in ('18', '19', '20'))
Hi Gribouillis,

Thanks, I have modified code to work with snmpbulkwak with MAC address OID
1.3.6.1.4.1.25053.1.3.2.1.1.2.2.1.1

import re
from easysnmp import *
import time
start = time.time()
host = '10.124.115.10'

session = Session(hostname=host, community='wifi', version=2)

WLCip,SCGAPNumSta,SCGAPConnStatus,SCGAPDescription= (list(session.bulkwalk('1.3.6.1.4.1.25053.1.3.2.1.1.2.2.1.' + c ,non_repeaters=0, max_repetitions=50)) for c in ('1','10', '15', '16','22'))

for i in range(len(WLCip)):
      mac_address = mac2norm(SCGAPMac[i].value)
      print SCGAPMac[i].value
      print mac_address
      print ('{} {} {} {} {}'.format(host,mac_address,WLCip[i].value,SCGAPNumSta[i].value,SCGAPConnStatus[i].value,SCGAPDescription[i].value))

def mac2norm(mac):
           return ('{}'.format(':'.join(a.split())))

if __name__ == "__main__":
   main()
print('It took', time.time()-start, 'seconds.')
output of snmpwalk for MAC address oid is as
Output:
SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.4.79.170.52.162.128 = Hex-STRING: 04 4F AA 34 A2 80 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.4.79.170.52.167.32 = Hex-STRING: 04 4F AA 34 A7 20 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.1.204.80 = Hex-STRING: 1C B9 C4 01 CC 50 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.1.233.176 = Hex-STRING: 1C B9 C4 01 E9 B0 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.121.112 = Hex-STRING: 1C B9 C4 03 79 70 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.122.160 = Hex-STRING: 1C B9 C4 03 7A A0 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.123.0 = Hex-STRING: 1C B9 C4 03 7B 00 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.124.16 = Hex-STRING: 1C B9 C4 03 7C 10 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.125.128 = Hex-STRING: 1C B9 C4 03 7D 80
to convert mac to normal MAC, one function is used

Python 2.7.5 (default, Aug  4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = "04 4F AA 34 A2 80"
>>> print('{}'.format(':'.join(a.split())))
04:4F:AA:34:A2:80
and getting expected conversion.
but getting error post running this script

Error:
Traceback (most recent call last): File "WLC.py", line 9, in <module> WLCip,SCGAPNumSta,SCGAPConnStatus,SCGAPDescription= (list(session.bulkwalk('1.3.6.1.4.1.25053.1.3.2.1.1.2.2.1.' + c ,non_repeaters=0, max_repetitions=50)) for c in ('1','10', '15', '16','22')) ValueError: too many values to unpack
If you have five values on the right of =, you need five variable names on the left.
>>> a, b, c, d = 8, 5, 23, 2, 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
Pages: 1 2 3