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
#11
Ooops Sorry..
Corrected now, but showing mac2norm function not defined.

Error:
Traceback (most recent call last): File "WLC.py", line 12, in <module> mac_address = mac2norm(SCGAPMac[i].value) NameError: name 'mac2norm' is not defined
Reply
#12
from easysnmp import Session
host = '10.124.xxx.xx'
session = Session(hostname=host, community='xxxwifi', version=2)

# Perform an SNMP walk
system_items = session.walk('1.3.6.1.4.1.25053.1.3.2.1.1.2.2.1.1')

# Each returned item can be used normally as its related type (str or int)
# but also has several extended attributes with SNMP-specific information
for item in system_items:
    print '{oid}.{oid_index} {snmp_type} = {value}'.format(
        oid=item.oid,
        oid_index=item.oid_index,
        snmp_type=item.snmp_type,
        value=item.value
    )
Not able to print output

Error:
Traceback (most recent call last): File "WLC1.py", line 39, in <module> value=item.value UnicodeEncodeError: 'ascii' codec can't encode character u'\xaa' in position 2: ordinal not in range(128)
snmpwalk output is as below

Output:
[anna]# snmpwalk -v2c -c xxx 10.124.xxx.xx 1.3.6.1.4.1.25053.1.3.2.1.1.2.2.1.1 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 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.125.208 = Hex-STRING: 1C B9 C4 03 7D D0 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.126.112 = Hex-STRING: 1C B9 C4 03 7E 70 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.133.192 = Hex-STRING: 1C B9 C4 03 85 C0 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.133.208 = Hex-STRING: 1C B9 C4 03 85 D0 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.134.240 = Hex-STRING: 1C B9 C4 03 86 F0 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.135.144 = Hex-STRING: 1C B9 C4 03 87 90 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.138.32 = Hex-STRING: 1C B9 C4 03 8A 20 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.139.0 = Hex-STRING: 1C B9 C4 03 8B 00 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.139.64 = Hex-STRING: 1C B9 C4 03 8B 40
Reply
#13
Why don't you upgrade to python 3? It is high time! You can perhaps try
u'{oid}.{oid_index} {snmp_type} = {value}'
Reply
#14
output from Python3.6

Output:
enterprises.25053.1.3.2.1.1.2.2.1.1.6.236.140.162.46.194.80. OCTETSTR = ì�¢.ÂP enterprises.25053.1.3.2.1.1.2.2.1.1.6.236.140.162.46.194.112. OCTETSTR = ì�¢.Âp enterprises.25053.1.3.2.1.1.2.2.1.1.6.236.140.162.46.195.0. OCTETSTR = ì�¢.à enterprises.25053.1.3.2.1.1.2.2.1.1.6.236.140.162.46.195.80. OCTETSTR = ì�¢.ÃP enterprises.25053.1.3.2.1.1.2.2.1.1.6.236.140.162.46.196.16. OCTETSTR = ì�¢.Ä enterprises.25053.1.3.2.1.1.2.2.1.1.6.236.140.162.46.196.32. OCTETSTR = ì�¢.Ä enterprises.25053.1.3.2.1.1.2.2.1.1.6.236.140.162.46.196.64. OCTETSTR = ì�¢.Ä@ enterprises.25053.1.3.2.1.1.2.2.1.1.6.236.140.162.46.196.160. OCTETSTR = ì�¢.Ä  enterprises.25053.1.3.2.1.1.2.2.1.1.6.236.140.162.46.196.240. OCTETSTR = ì�¢.Äð enterprises.25053.1.3.2.1.1.2.2.1.1.6.236.140.162.46.197.112. OCTETSTR = ì�¢.Åp enterprises.25053.1.3.2.1.1.2.2.1.1.6.236.140.162.46.198.16. OCTETSTR = ì�¢.Æ enterprises.25053.1.3.2.1.1.2.2.1.1.6.236.140.162.46.198.32. OCTETSTR = ì�¢.Æ enterprises.25053.1.3.2.1.1.2.2.1.1.6.236.140.162.46.198.64. OCTETSTR = ì�¢.Æ@ enterprises.25053.1.3.2.1.1.2.2.1.1.6.236.140.162.46.198.80. OCTETSTR = ì�¢.ÆP enterprises.25053.1.3.2.1.1.2.2.1.1.6.236.140.162.46.198.128. OCTETSTR = ì�¢.Æ� enterprises.25053.1.3.2.1.1.2.2.1.1.6.236.140.162.46.203.160. OCTETSTR = ì�¢.Ë  enterprises.25053.1.3.2.1.1.2.2.1.1.6.236.140.162.46.204.176. OCTETSTR = ì�¢.Ì° enterprises.25053.1.3.2.1.1.2.2.1.1.6.236.140.162.47.18.208. OCTETSTR = ì�¢/Ð enterprises.25053.1.3.2.1.1.2.2.1.1.6.236.140.162.47.19.112. OCTETSTR = ì�¢/p enterprises.25053.1.3.2.1.1.2.2.1.1.6.240.62.144.5.77.160. OCTETSTR = ð>�M  enterprises.25053.1.3.2.1.1.2.2.1.1.6.240.62.144.5.222.192. OCTETSTR = ð>�ÞÀ enterprises.25053.1.3.2.1.1.2.2.1.1.6.240.62.144.5.251.224. OCTETSTR = ð>�ûà enterprises.25053.1.3.2.1.1.2.2.1.1.6.240.62.144.5.253.64. OCTETSTR = ð>�ý@
command line snmpwalk output as below
Output:
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 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.125.208 = Hex-STRING: 1C B9 C4 03 7D D0 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.126.112 = Hex-STRING: 1C B9 C4 03 7E 70 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.133.192 = Hex-STRING: 1C B9 C4 03 85 C0 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.133.208 = Hex-STRING: 1C B9 C4 03 85 D0 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.134.240 = Hex-STRING: 1C B9 C4 03 86 F0 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.135.144 = Hex-STRING: 1C B9 C4 03 87 90 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.138.32 = Hex-STRING: 1C B9 C4 03 8A 20 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.139.0 = Hex-STRING: 1C B9 C4 03 8B 00 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.139.64 = Hex-STRING: 1C B9 C4 03 8B 40 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.139.96 = Hex-STRING: 1C B9 C4 03 8B 60 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.139.176 = Hex-STRING: 1C B9 C4 03 8B B0 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.142.16 = Hex-STRING: 1C B9 C4 03 8E 10 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.144.32 = Hex-STRING: 1C B9 C4 03 90 20 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.152.240 = Hex-STRING: 1C B9 C4 03 98 F0 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.153.96 = Hex-STRING: 1C B9 C4 03 99 60 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.154.160 = Hex-STRING: 1C B9 C4 03 9A A0 SNMPv2-SMI::enterprises.25053.1.3.2.1.1.2.2.1.1.6.28.185.196.3.158.240 = Hex-STRING: 1C B9 C4 03 9E F0
Reply
#15
item.value is some binary data. Can you get the type(item.value) and also the repr(item.value) for those items?
Reply
#16
Since this is python 2 try to put u'' before the string to tell Python that this is Unicode, not ASCII.

for item in system_items:
    print u'{oid}.{oid_index} {snmp_type} = {value}'.format(
        oid=item.oid,
        oid_index=item.oid_index,
        snmp_type=item.snmp_type,
        value=item.value
    )
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#17
for item in system_items:
    print(type(item.value))
    print(repr(item.value))
    print (u'{oid}.{oid_index} {snmp_type} = {value}'.format(
        oid=item.oid,
        oid_index=item.oid_index,
        snmp_type=item.snmp_type,
        value=item.value
    ))
getting below output

Output:
enterprises.25053.1.3.2.1.1.2.2.1.1.6.84.61.55.23.66.144. OCTETSTR = T=7B� <type 'unicode'> u'T=7\x17B\xc0' enterprises.25053.1.3.2.1.1.2.2.1.1.6.84.61.55.23.66.192. OCTETSTR = T=7BÀ <type 'unicode'> u'T=7\x17B\xe0' enterprises.25053.1.3.2.1.1.2.2.1.1.6.84.61.55.23.66.224. OCTETSTR = T=7BàPu TTY<type 'unicode'> u'T=7\x17C\x10' enterprises.25053.1.3.2.1.1.2.2.1.1.6.84.61.55.23.67.16. OCTETSTR = T=7C <type 'unicode'> u'T=7\x17C ' enterprises.25053.1.3.2.1.1.2.2.1.1.6.84.61.55.23.67.32. OCTETSTR = T=7C <type 'unicode'> u'T=7\x17CP' enterprises.25053.1.3.2.1.1.2.2.1.1.6.84.61.55.23.67.80. OCTETSTR = T=7CP <type 'unicode'> u'T=7\x17C`' enterprises.25053.1.3.2.1.1.2.2.1.1.6.84.61.55.23.67.96. OCTETSTR = T=7C` <type 'unicode'> u'T=7\x17C\xa0' enterprises.25053.1.3.2.1.1.2.2.1.1.6.84.61.55.23.67.160. OCTETSTR = T=7C PuTTY <type 'unicode'> u'T=7\x17C\xf0' enterprises.25053.1.3.2.1.1.2.2.1.1.6.84.61.55.23.67.240. OCTETSTR = T=7Cð <type 'unicode'> u'T=7\x17D\xd0' enterprises.25053.1.3.2.1.1.2.2.1.1.6.84.61.55.23.68.208. OCTETSTR = T=7DÐ <type 'unicode'> u'T=7\x17E\x10' enterprises.25053.1.3.2.1.1.2.2.1.1.6.84.61.55.23.69.16. OCTETSTR = T=7E <type 'unicode'> u'T=7\x17G`' enterprises.25053.1.3.2.1.1.2.2.1.1.6.84.61.55.23.71.96. OCTETSTR = T=7G` <type 'unicode'> PuTTYu'T=7\x17G\xc0' enterprises.25053.1.3.2.1.1.2.2.1.1.6.84.61.55.23.71.192. OCTETSTR = T=7GÀ <type 'unicode'> u'T=7\x17H\x00' enterprises.25053.1.3.2.1.1.2.2.1.1.6.84.61.55.23.72.0. OCTETSTR = T=7H <type 'unicode'> u'T=7\x17H\x10' enterprises.25053.1.3.2.1.1.2.2.1.1.6.84.61.55.23.72.16. OCTETSTR = T=7H <type 'unicode'> u'T=7\x17H@' enterprises.25053.1.3.2.1.1.2.2.1.1.6.84.61.55.23.72.64. OCTETSTR = T=7H@ <type 'unicode'> u'T=7\x17HP' enterprises.25053.1.3.2.1.1.2.2.1.1.6.84.61.55.23.72.80. OCTETSTR = T=7HPPuT TY<type 'unicode'> u'T=7\x17Hp' enterprises.25053.1.3.2.1.1.2.2.1.1.6.84.61.55.23.72.112. OCTETSTR = T=7Hp <type 'unicode'> u'T=7\x17H\x80'
Reply
#18
Why you enclose it in brackets? See my example
Also your terminal have to support unicode
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#19
Wavic, same result without brackets

for item in system_items:
    print(type(item.value))
    print(repr(item.value))
    print u'{oid}.{oid_index} {snmp_type} = {value}'.format(
        oid=item.oid,
        oid_index=item.oid_index,
        snmp_type=item.snmp_type,
        value=item.value
    )
output

Output:
enterprises.25053.1.3.2.1.1.2.2.1.1.6.240.62.144.31.221.32. OCTETSTR = ð>�Ý <type 'unicode'> u'\xf0>\x90\x1f\xddP' enterprises.25053.1.3.2.1.1.2.2.1.1.6.240.62.144.31.221.80. OCTETSTR = ð>�ÝP <type 'unicode'> u'\xf0>\x90\x1f\xdd`' enterprises.25053.1.3.2.1.1.2.2.1.1.6.240.62.144.31.221.96. OCTETSTR = ð>�Ý` <type 'unicode'> u'\xf0>\x90\x1f\xdd\xb0' enterprises.25053.1.3.2.1.1.2.2.1.1.6.240.62.144.31.221.176. OCTETSTR = ð>�Ý° <type 'unicode'> u'\xf0>\x90\x1f\xdd\xf0' enterprises.25053.1.3.2.1.1.2.2.1.1.6.240.62.144.31.221.240. OCTETSTR = ð>�Ýð <type 'unicode'> u'\xf0>\x90\x1f\xde\x00' enterprises.25053.1.3.2.1.1.2.2.1.1.6.240.62.144.31.222.0. OCTETSTR = ð>�Þ <type 'unicode'> u'\xf0>\x90\x1f\xde\x10' enterprises.25053.1.3.2.1.1.2.2.1.1.6.240.62.144.31.222.16. OCTETSTR = ð>�Þ <type 'unicode'> u'\xf0>\x90\x1f\xde ' enterprises.25053.1.3.2.1.1.2.2.1.1.6.240.62.144.31.222.32. OCTETSTR = ð>�Þ <type 'unicode'> u'\xf0>\x90\x1f\xde0'
Reply
#20
I think that it is because of the fonts. Not any font can represent any character.

This is what I get in python3:
>>> print('\xf0>\x90\x1f\xde0')
ð>Þ0
See the missing characters - "\x90\x1f"? They are unprintable.
http://www.fileformat.info/info/unicode/.../index.htm
https://www.fileformat.info/info/unicode.../index.htm
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
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,606 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,007 Jun-12-2020, 05:08 PM
Last Post: adeana
  Is it possible to avoid 2 loops inside another loop? SvetlanaofVodianova 2 2,185 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,059 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,078 Nov-23-2018, 01:01 AM
Last Post: wavic
  Using nested for loop with a single list mikeavison 3 3,320 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