Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errors about RFC3413 / HLAPI/ ASYNCORE #405

Open
nathanbdr opened this issue Jul 8, 2021 · 0 comments
Open

errors about RFC3413 / HLAPI/ ASYNCORE #405

nathanbdr opened this issue Jul 8, 2021 · 0 comments

Comments

@nathanbdr
Copy link

Good morning all,
first of all, I should point out that I am new to python, it may be that I have basic errors, which is why I am interested in any modification suggestions.

I tried to implement the following code:

coding=utf8

import os
import csv
from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.proto import rfc1902
from pysnmp.smi import builder, view, compiler

with open('FichiersTest.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile, delimiter=';')
for row in reader:
ip = row['NetworkDeviceIpAddr'].split(" ")[0].split("/")[0]
unite = row['PortName'].split(" ")[1].split("/")[0]
port = row['PortName'].split(" ")[1].split("/")[1]
EquipementType = row['NetworkDeviceType'].split(" ")[0].split("/")[0]
print(row['DeviceDNSName'],' ; ',row['NetworkDeviceIpAddr'],' ; ',row['NetworkDeviceType'],' ; ',row['PortName'],' ; ',row['PortNumber'],' ;')

SNMP_HOST = ip
SNMP_PORT = 161
SNMP_COMMUNITY = 'cumulus'
x=1
y=1
cmdGen = cmdgen.CommandGenerator()

#fonction disable port 4524
def disableport_4524(EquipementType='ERS-4524GT-PWR',SNMP_HOST= ip,x=1,y=1,SNMP_PORT = 161,SNMP_COMMUNITY = 'cumulus'):
x=1
while x<25:
errorIndication, errorStatus, errorIndex, varBinds=cmdGen.getCmd(
#varBinds=cmdGen.getCmd(
cmdgen.CommunityData(SNMP_COMMUNITY),
cmdgen.UdpTransportTarget((SNMP_HOST, SNMP_PORT)),
'1.3.6.1.2.1.2.2.1.7.'+str(x)

                            )
    
    for name,val in varBinds:
            print('%s  = %s' % (name.prettyPrint(), val.prettyPrint()))
    x=x+1

y=1
while y<25:
errorIndication, errorStatus, errorIndex, varBinds=cmdGen.setCmd(
cmdgen.CommunityData(SNMP_COMMUNITY),
cmdgen.UdpTransportTarget((SNMP_HOST, SNMP_PORT)),
('1.3.6.1.2.1.2.2.1.7.'+str(x),rfc1902.Integer('2'))
)
for name,val in varBinds:
print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
y=y+1

    if EquipementType==('ERS-4524GT-PWR'):
            SNMP_HOST=('NetworkDeviceIpAddr')
            disableport_4524()

def disableport_4850(EquipementType='4950GTS-PWR+',SNMP_HOST= ip,x=1,y=1,SNMP_PORT = 161,SNMP_COMMUNITY = 'cumulus'):
x=1
while x<49:
errorIndication, errorStatus, errorIndex, varBinds=cmdGen.getCmd(
#varBinds=cmdGen.getCmd(
cmdgen.CommunityData(SNMP_COMMUNITY),
cmdgen.UdpTransportTarget((SNMP_HOST, SNMP_PORT)),
'1.3.6.1.2.1.2.2.1.7.'+str(x)

                            )
    print(str(x))
    for name,val in varBinds:
            print('%s  = %s' % (name.prettyPrint(), val.prettyPrint()))
    x=x+1

y=1
while y<49:
errorIndication, errorStatus, errorIndex, varBinds=cmdGen.setCmd(
cmdgen.CommunityData(SNMP_COMMUNITY),
cmdgen.UdpTransportTarget((SNMP_HOST, SNMP_PORT)),
('1.3.6.1.2.1.2.2.1.7.)'+str(y),rfc1902.Integer('2'))
)
print(str(x))
for name,val in varBinds:
print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
y=y+1

if EquipementType==('4950GTS-PWR+'):
SNMP_HOST=('NetworkDeviceIpAddr')
print("c'est un switch 48 ports!")
disableport_4850()

from a csv file, we interrogate the switches in SNMP. Then we modify the value of the oid to desactivate the ports. For that I have two functions which are similar except that I have the following errors once I run the script.

CTraceback (most recent call last):
File "test copy.py", line 60, in
'1.3.6.1.2.1.2.2.1.7.'+str(x)
File "/usr/lib/python3/dist-packages/pysnmp/entity/rfc3413/oneliner/cmdgen.py" , line 183, in getCmd
**kwargs):
File "/usr/lib/python3/dist-packages/pysnmp/hlapi/asyncore/sync/cmdgen.py", li ne 113, in getCmd
snmpEngine.transportDispatcher.runDispatcher()
File "/usr/lib/python3/dist-packages/pysnmp/carrier/asyncore/dispatch.py", lin e 46, in runDispatcher
use_poll=True, map=self.__sockMap, count=1)
File "/usr/lib/python3.6/asyncore.py", line 207, in loop
poll_fun(timeout, map)
File "/usr/lib/python3.6/asyncore.py", line 183, in poll2
r = pollster.poll(timeout)

small precision: my functions work correctly independently without the csv file.

I thank you in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant