Automate SNMP lookup in the code that follows

0
import os
import commands
import time
def ipagente():
 ctoctok=0
 while ctoctok<4:
  ctoctok=0
  stroct=["","","",""]
  ctp=0
  ipa=raw_input("IP (xxx.xxx.xxx.xxx):")
  for car in ipa:
   if car>="0" and car<="9": stroct[ctp]+=car
   if car==".": ctp+=1
  for car in stroct:
   if car.isdigit() and int(car)>=0 and int(car)<=255: ctoctok+=1
 return ipa

def walk(ip):
 cmd="snmpwalk -v1 -cpublic "+ip+" |head"
 var=commands.getoutput(cmd)
 if var.find("Timeout")==-1:
  listvar=var.split("\n")
  listdesc=listvar[0].split(": ")
  listtime=listvar[2].split(": ")
  listname=listvar[4].split(": ")
  return listdesc[1], listtime[1], listname[1]
 else:
  return "Sem resposta", "Sem resposta", "Sem resposta"

def get(opc,ip):
 cmd=""
 if opc.lower()=="d":
  cmd="snmpget -v1 -cpublic "+ip+" sysDescr.0"
  nomevar="Descricao: "
 if opc.lower()=="t":
  cmd="snmpget -v1 -cpublic "+ip+" sysUpTimeInstance"
  nomevar="Tempo on-line: "
 if opc.lower()=="n":
  cmd="snmpget -v1 -cpublic "+ip+" sysName.0"
  nomevar="Nome do host: "
 if cmd!="":
  var=commands.getoutput(cmd)
  if var.find("Timeout")==-1:
   listmsg=var.split(": ")
   return nomevar+listmsg[1]
  else:
   return "Sem resposta"
 else:
  return "Opcao invalida"

print "PEDE O IP DO AGENTE"
ip=ipagente()
print "USO DO SNMPWALK"
vardesc, vartime, varname = walk(ip)
print "Descricao    :",vardesc
print "Tempo on-line:",vartime
print "Nome do host :",varname
print "USO DO SNMPGET"
num=1
ct=0
opcao=raw_input("Opcao (D)escricao, (T)empo, (N)ome:")
print(get(opcao,ip))
print "LISTAGEM DATAGRAMAS"
try:
 opcao=int(input("Loops[1-100]:" ))
except:
 opcao=1
if opcao<1 or opcao>100:
 print("Loops fora do intervalo [1-100] - Loops=1")
 opcao=1
for ct in range (opcao):
 cmd="snmpget -cpublic -v1 "+ip+" ipInReceives.0 ipOutRequests.0"
 var=commands.getoutput(cmd)
 if var.find("Timeout")==-1:
  print var
  print "---===---"
  time.sleep(5)
 else:
  print "sem resposta"
  break
    
asked by anonymous 27.09.2018 / 14:52

0 answers