Python Forum
something going wrong.. code is not working - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: something going wrong.. code is not working (/thread-7207.html)



something going wrong.. code is not working - anna - Dec-28-2017

#!/usr/bin/python
from easysnmp import Session
# load easysnmp module
community ='public'
#Community for the device
oid ='SNMPv2-MIB::sysDescr.0'
#sysDescr OID
sr_no = 0
with open('DSLAMIP.txt') as ipfile:
    line = ipfile.readlines()
#    print line
    for line in ipfile:
        try:
            session = Session(hostname=line, community=community, version=2)
            snmp_get = session.get(oid)
            result = snmp_get.value.encode('ascii')
            if 'IES1248-51' in result:
                model = 'ZyXEL'
            elif 'ECI' in result:
                model = 'ECI'
            elif 'IP' in result:
                model = 'UT'
            sr_no += 1
            print sr_no+" "+line+ " " +model
        except snmp_exceptions as e:
            print('Connection timed out ', ipfile['line'], e)
would like to print Sr_no IP address Model, but this code is not working


RE: something going wrong.. code is not working - Larz60+ - Dec-28-2017

What exactly does not working mean?
Need details like:
Inputs,
Outputs
Error messages
Expected output


RE: something going wrong.. code is not working - anna - Dec-28-2017

I need to send one ip address at a time from DSLAMIP.txt to check device model, I am able to print line post opening file, however ips are not passed to session to check model type.
Python is not showing any error or output.

thanks


RE: something going wrong.. code is not working - Larz60+ - Dec-28-2017

What do you see when you remove comment from the print statement
Does the ip statement look ok?

And (on another subject), why are you using antique version of python?


RE: something going wrong.. code is not working - buran - Dec-28-2017

On line#10 you read the entire txt file in a variable called line. This is actually a list of lines in the file. At that point you are at the end of the file.
Next you want to iterate over the file ipfile line by line, however you are already at the end of the file, thus there is nothing left to iterate over.
To solve the problem just remove line#10 of your code. then you will iterate over the file line by line.


RE: something going wrong.. code is not working - anna - Dec-28-2017

Thanks Buran,
Changes in code as below

#!/usr/bin/python
from easysnmp import Session
# load easysnmp module
community ='public'
#Community for the device
oid ='SNMPv2-MIB::sysDescr.0'
#sysDescr OID
sr_no = 0
with open('DSLAMIP.txt') as ipfile:
#    line = ipfile.readlines()
#    print line
    for line in ipfile:
#        print line
        try:
            session = Session(hostname=line, community=community, version=2)
            snmp_get = session.get(oid)
            result = snmp_get.value.encode('ascii')
            if 'IES1248-51' in result:
                model = 'ZyXEL'
            elif 'ECI' in result:
                model = 'ECI'
            elif 'IP' in result:
                model = 'UT'
            sr_no += 1
            print sr_no+" "+line+ " " +model
        except:
            print('Connection timed out') 
easysnmp session is not working,
Output is:- Connection timed out
Connection timed out
Connection timed out
Connection timed out
Connection timed out
Connection timed out
Connection timed out
Connection timed out
Connection timed out
Connection timed out

whereas :- i am able to do this manually
[root@localhost scripts]# snmpwalk -v2c -c public 10.113.1.1 SNMPv2-MIB::sysDescr.0
SNMPv2-MIB::sysDescr.0 = STRING: ECI telecom HiFOCuS broadband access system


RE: something going wrong.. code is not working - buran - Dec-28-2017

Obviously I'm not able to try, but in order to debug I would remove the try/except thus it will show what the actual error is. i.e. Connection time out is something you print, but because you have all-catching except it masks what the actual error is - i.e. it could be simple syntax error for example. Using all-catching except is generally considered bad practice and only acceptable if you actually print what the error description is
also line will have '\n' at the end, so you may need to strip this before supply line as argument hostname


RE: something going wrong.. code is not working - anna - Dec-28-2017

Hi Buran,

I am able to print Device model now.
changes in code is as below
#!/usr/bin/python
from easysnmp import Session
# load easysnmp module
community ='public'
#Community for the device
oid ='SNMPv2-MIB::sysDescr.0'
#sysDescr OID
sr_no = 0
with open('DSLAMIP.txt') as ipfile:
#    line = ipfile.readlines()
#    print line
    for line in ipfile:
#        print line
        try:
            session = Session(hostname=line, community=community, version=2)
            snmp_get = session.get(oid)
            result = snmp_get.value.encode('ascii')
            if 'IES1248-51' in result:
                model = 'ZyXEL'
            elif 'ECI' in result:
                model = 'ECI'
            elif 'IP' in result:
                model = 'UT'
            sr_no += 1
            print (str(sr_no)+' '+str(line)+' '+model)
        except:
            print(str(sr_no)+' ' +str(line)+' ' +'Connection timed out')
But output is printing like 1469 10.16.1.59
UT
model on new line where as i want this like 1469 10.16.1.59 UT --- on same line


RE: something going wrong.. code is not working - buran - Dec-28-2017

as I said line will have new lien - '\n' at the end and you need to strip it.

#!/usr/bin/python

# load easysnmp module
from easysnmp import Session
#Community for the device
community ='public'
#sysDescr OID
oid ='SNMPv2-MIB::sysDescr.0'
with open('DSLAMIP.txt') as ipfile:
    for sr_no, line in enumerate(ipfile, start=1):
        ip = line.strip()
        try:
            session = Session(hostname=ip, community=community, version=2)
            snmp_get = session.get(oid)
            result = snmp_get.value.encode('ascii')
            if 'IES1248-51' in result:
                model = 'ZyXEL'
            elif 'ECI' in result:
                model = 'ECI'
            elif 'IP' in result:
                model = 'UT'
            print('{} {} {}'.format(sr_no, ip, model))
        except Exception as excp:
            print('Something went wrong!')
            print(excp)



RE: something going wrong.. code is not working - anna - Dec-28-2017

Thanks, Worked...