2017-12-07 25 views
0

저는 python을 처음 사용하여 인쇄 출력을 행과 열로 포맷하는 데 도움이 필요합니다. 이 데이터는 결국 csv 파일로 전송됩니다.PYsnmp 4.4 여러 행 서식 지정 행 및 열 인쇄 출력 Python 2.7

스크립트는 여러 호스트의 데이터를 가져옵니다. 줄 수는 인터페이스 이름과 설명의 길이뿐만 아니라 가변적입니다.

현재 출력은 다음과 같습니다

hostname IF-MIB::ifDescr.1 = GigabitEthernet0/0/0<br/> 
hostname IF-MIB::ifAlias.1 = --> InterfaceDesc<br/> 
hostname IF-MIB::ifOperStatus.1 = 'up'<br/> 
hostname IF-MIB::ifDescr.2 = GigabitEthernet0/0/1<br/> 
hostname IF-MIB::ifAlias.2 = --> InterfaceDesc<br/> 
hostname IF-MIB::ifOperStatus.2 = 'up'<br/> 
hostname IF-MIB::ifDescr.3 = GigabitEthernet0/0/2<br/> 
hostname IF-MIB::ifAlias.3 = --> InterfaceDesc<br/> 
hostname IF-MIB::ifOperStatus.3 = 'up'<br/> 

내가 각 행의 헤더 (호스트 이름, 인터페이스, 인터페이스 내림차순 및 상태) 다음과 같은 행과 열을 포맷하기 위해 노력하고있어.

hostname  interface    interface desc  status 
hostname  GigabitEthernet0/0/0 InterfaceDesc  up 
hostname  GigabitEthernet0/0/1 InterfaceDesc  up 
hostname  GigabitEthernet0/0/2 InterfaceDesc  up 

현재 인쇄중인 코드는 여기에 있습니다. print 문에 오류가 발생하지 않도록하고 싶습니다.

for errorIndication, errorStatus, errorIndex, varBinds in snmp_iter: 
      # Check for errors and print out results 
      if errorIndication: 
       print(errorIndication) 
      elif errorStatus: 
       print('%s at %s' % (errorStatus.prettyPrint(), 
           errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) 
      else: 
       for varBind in varBinds: 
        print(hostip), 
        print(' = '.join([x.prettyPrint() for x in varBind])) 

전체 스크립트를 당신이 제공 할 수있는 어떤 도움이 많이 감사합니다

from pysnmp.hlapi import * 

routers = ["router1"] 

#adds routers to bulkCmd 
def snmpquery (hostip): 
    snmp_iter = bulkCmd(SnmpEngine(), 
         CommunityData('Community'), 
         UdpTransportTarget((hostip, 161)), 
         ContextData(), 
         0, 50, # fetch up to 50 OIDs 
         ObjectType(ObjectIdentity('IF-MIB', 'ifDescr')), 
         ObjectType(ObjectIdentity('IF-MIB', 'ifAlias')), 
         ObjectType(ObjectIdentity('IF-MIB', 'ifOperStatus')), 
         lexicographicMode=False) # End bulk request once outside of OID child objects 
    for errorIndication, errorStatus, errorIndex, varBinds in snmp_iter: 
     # Check for errors and print out results 
     if errorIndication: 
      print(errorIndication) 
     elif errorStatus: 
      print('%s at %s' % (errorStatus.prettyPrint(), 
          errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) 
     else: 
      for rowId, varBind in enumerate(varBindTable): 
       oid, value = varBind 
       print('%20.20s' % value) 
       if not rowId and rowId % 3 == 0: 
        print('\n') 

# calls snmpquery for all routers in list 
for router in routers: 
    snmpquery(router) 

. 감사합니다. snmp_iter 가정

답변

0

세 SNMP 테이블 컬럼으로 초기화됩니다 :

snmp_iter = bulkCmd(SnmpEngine(), 
        UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), 
        Udp6TransportTarget(('demo.snmplabs.com', 161)), 
        ContextData(), 
        0, 25, 
        ObjectType(ObjectIdentity('IF-MIB', 'ifDescr')), 
        ObjectType(ObjectIdentity('IF-MIB', 'ifAlias')), 
        ObjectType(ObjectIdentity('IF-MIB', 'ifOperStatus'))) 

당신이합니다 (GETNEXTgetBulk를 명령에 대해) pysnmp 항상 행 방식에 의해 연속적으로 직사각형 테이블을 반환 확신 할 수 있습니다 .

요청한 열 수를 알고 (3) 행에 의해 출력 행을 인쇄 할 수 있어야한다 :

for rowId, varBind in enumerate(varBindTable): 
    oid, value = varBind 
    print('%20.20s' % value) 
    if not rowId and rowId % 3 == 0: 
     print('\n') 
+0

감사 IIya, 내가 varBindTable을 정의하고 그것을 varBinds의 값을 할당해야합니까? – Ev9432

+0

히트 보내기 곧. 예 snmp_inter는 세 개의 SNMP 테이블로 초기화됩니다. 전체 스크립트를 추가했습니다. – Ev9432