Python Forum
working code, suggestion required for improvement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
working code, suggestion required for improvement
#4
#!/usr/bin/python
from easysnmp import Session
community = 'public'
with open('zyxeldslam.txt') as ipfile:
    for sr_no, line in enumerate(ipfile, start=1):
        ip = line.strip()
        try:
# Create an SNMP session to be used for all our requests
            session = Session(hostname=ip, community=community, version=2)
# You may retrieve an individual OID using an SNMP GET
            fan_oids_templ = '1.3.6.1.4.1.890.1.5.13.1.2.1.1.2.0.{}'
            temp_oids_templ = '1.3.6.1.4.1.890.1.5.13.1.2.3.1.2.0.0.{}'
            fans_oids = [fan_oids_templ.format(i) for i in range(1,4)]
            temps_oids = [temp_oids_templ.format(i) for i in range(1,4)]
            fans_status = ['Fail' if int(fan.value) < 2000 else 'Ok' for fan in session.get_bulk(fans_oids)]
            temp_status = [temp.value for value in session.get_bulk(temps_oids)]
            print('{} {} {} {} {} {} {} {}'.format(sr_no, ip, *fans_status, *temp_status))            
        except Exception as excp:
            print('Something went wrong!')
            print(excp)
conversion to int - int(fan.value) may not be required if returned values is actually an integer - INTEGER: 4333.
I assume it is still str, so I convert it.
some of the above can be done a bit different, e.g. there could be better way to construct oids, but I'm not familiar with the module - just reading the docs
Reply


Messages In This Thread
RE: working code, suggestion required for improvement - by buran - Dec-28-2017, 12:11 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Suggestion on how to speed up this code? sawtooth500 1 234 May-04-2024, 07:13 PM
Last Post: sawtooth500
  New to Python - Not sure why this code isn't working - Any help appreciated TheGreatNinx 4 1,004 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 939 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 1,053 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
Exclamation My code is not working as I expected and I don't know why! Marinho 4 1,120 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  My Code isn't working... End3r 4 1,977 Mar-21-2022, 10:12 AM
Last Post: End3r
  Can you give me some suggestion about PCEP Newbie1114 0 1,046 Oct-14-2021, 03:02 PM
Last Post: Newbie1114
  instagram followers name without suggestion for you jacklee26 1 3,211 Oct-02-2021, 04:57 AM
Last Post: ndc85430
  Random coordinate generator speed improvement saidc 0 2,075 Aug-01-2021, 11:09 PM
Last Post: saidc
  I don't undestand why my code isn't working. RuyCab 2 2,011 Jun-17-2021, 03:06 PM
Last Post: RuyCab

Forum Jump:

User Panel Messages

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