Python Forum
how to combine mumtiple for loops in single loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to combine mumtiple for loops in single loop
#1
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?
Reply
#2
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')
Reply
#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)
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
Reply
#4
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 ?
Reply
#5
(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
Reply
#6
(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)
Reply
#7
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
Reply
#8
(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'))
Reply
#9
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
Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,605 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  Print output in single file using pramika loop deepakkhw 1 2,086 Jul-11-2020, 11:57 AM
Last Post: j.crater
  Unable to combine print statements in for loop adeana 2 2,004 Jun-12-2020, 05:08 PM
Last Post: adeana
  Is it possible to avoid 2 loops inside another loop? SvetlanaofVodianova 2 2,184 Nov-27-2019, 02:30 PM
Last Post: Gribouillis
  Loop through folder of Excel Files and extract single column fioranosnake 2 4,545 Oct-28-2019, 05:19 PM
Last Post: fioranosnake
  Combine two scripts and loop BMC 2 2,057 Feb-17-2019, 02:13 PM
Last Post: BMC
  Loops - new terminal output to file for each loop jm_ice 1 2,599 Dec-21-2018, 02:42 PM
Last Post: ichabod801
  Python-for loop print into single line dragan979 4 7,077 Nov-23-2018, 01:01 AM
Last Post: wavic
  Using nested for loop with a single list mikeavison 3 3,318 Aug-12-2017, 08:13 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020