Skip to content

Instantly share code, notes, and snippets.

@gnumoksha
Created March 20, 2014 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnumoksha/9672042 to your computer and use it in GitHub Desktop.
Save gnumoksha/9672042 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
# http://www.python.org.br/wiki/PythonNoLugarDeShellScript
from subprocess import Popen, PIPE
class Cmd(object):
def __init__(self, cmd):
self.cmd = cmd
def __call__(self, *args):
command = '%s %s' %(self.cmd, ' '.join(args))
result = Popen(command, stdout=PIPE, stderr=PIPE, shell=True)
return result.communicate()
class Sh(object):
def __getattr__(self, attribute):
return Cmd(attribute)
shell = Sh()
retorno = shell.snmpget("-v 1 -c public 192.168.5.13 1.3.6.1.4.1.1248.1.2.2.44.1.1.2.1.115.116.1.0.1")
retornoTratado = retorno[0].replace('iso.3.6.1.4.1.1248.1.2.2.44.1.1.2.1.115.116.1.0.1 = Hex-STRING: ', '')
"""
Nesse ponto o numero de paginas eh 1038 e retornoTratado eh
00 40 42 44 43 20 53 54 32 0D 0A 50 00 01 01 04
06 02 01 FF 0F 0D 03 03 01 69 04 02 69 05 03 69
01 00 69 10 03 01 09 4E 13 01 01 19 0C 00 00 00
00 00 75 6E 6B 6E 6F 77 6E 24 02 00 00 2F 01 01
36 14 FF FF FF FF FF FF FF FF 17 02 00 00 E0 01
00 00 17 00 00 00 37 05 02 00 00 00 00
"""
for linha in retornoTratado.split('\n'):
for valor in linha.split(' '):
if not valor:
continue
#print "convertendo valor '%s'" % valor
try:
valorDecimal = int(valor, 16)
except ValueError, e:
print "Erro ao converter. Detalhes: %s" % e
if valorDecimal > 0:
if valorDecimal > 31 and valorDecimal < 127:
#print str(valorDecimal).decode('hex')
sys.stdout.write(chr(valorDecimal))
else:
sys.stdout.write(str(valorDecimal))
#print "\nvalor nao esta nos ifs: '%s'" % valorDecimal
print ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment