weblog d’un abbe

20070326

SNMPing

Filed under: Hacking — abbe @ 0252

Some python code snippets I hacked while playing with SNMP. I’ve used PySNMP python module.

Script to enumerate network interfaces.

#!/usr/bin/env python

from pysnmp.entity.rfc3413.oneliner import cmdgen

host='localhost'

class Network:
	def __init__(self,pid):
		self.values = []
		self.values.append(pid)
	def __str__(self):
		return str(self.values[0])
	def set(self,id,value):
		self.values.insert(id-1,value)
	def get_name(self):
		return self.values[1]
	def get_mtu(self):
		return self.values[3]
	def get_speed(self):
		return self.values[4]
	def get_mac(self):
		return self.values[5]
	def get_admin_status(self):
		return self.values[6]
	def get_oper_status(self):
		return self.values[7]
	def get_last_change(self):
		return self.values[8]
	def get_octets_in(self):
		return self.values[9]
	def get_octets_out(self):
		return self.values[10]ls

errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().nextCmd(
	cmdgen.CommunityData('test-agent','public'),
	cmdgen.UdpTransportTarget((host, 161)),
	(1,3,6,1,2,1,2,2,1))

networks = dict()

for varBT in varBinds:
	key   = varBT[0][0]
	value = varBT[0][1]

	if(key[10] in networks):
		networks[key[10]].set(key[9],value)
	else:
		networks[key[10]] = Network(key[10])

for netid in networks:
	net = networks[netid]
	print netid, net.get_name(), net.get_mtu(), net.get_speed(), net.get_octets_in()

Script to generate list of softwares installed.

#!/usr/bin/env python

from pysnmp.entity.rfc3413.oneliner import cmdgen

host='localhost'

errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().nextCmd(
	cmdgen.CommunityData('test-agent','public'),
	cmdgen.UdpTransportTarget((host, 161)),
	(1,3,6,1,2,1,25,6,3,1,2))


for varBT in varBinds:
	key   = varBT[0][0]
	value = varBT[0][1]
	print repr(key[11]).rjust(6), value

Script to print system information

#!/usr/bin/env python

from pysnmp.entity.rfc3413.oneliner import cmdgen

host='localhost'

class SystemInformation:
	def __init__(self):
		self.values = []
	def __str__(self):
		return str(self.values[0])
	def set(self,id,value):
		self.values.insert(id-1,value)
	def get_uptime(self):
		return self.values[0]
	def get_system_date(self):
		return self.values[1]
	def get_load_device(self):
		return self.values[2]
	def get_cmd_line(self):
		return self.values[3]
	def get_num_users(self):
		return self.values[4]
	def get_num_procs(self):
		return self.values[5]
	def get_max_procs(self):
		return self.values[6]
	def get_max_mem(self):
		return self.values[7]

errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().nextCmd(
	cmdgen.CommunityData('test-agent','public'),
	cmdgen.UdpTransportTarget((host, 161)),
	(1,3,6,1,2,1,25,1))

sysinfo = SystemInformation()

for varBT in varBinds:
	key   = varBT[0][0]
	value = varBT[0][1]

	sysinfo.set(key[8], value)

print "Users logged in: ", sysinfo.get_num_users()
print "Processes running: ", sysinfo.get_num_procs()
print "Command Line: ", sysinfo.get_cmd_line()

A stripped down ‘ps’ using PySNMP

#!/usr/bin/env python

# A stripped down 'ps' using SNMP in Python
from pysnmp.entity.rfc3413.oneliner import cmdgen

host='localhost'

class Process:
	def __init__(self,pid):
		self.values = []
		self.values.append(pid)
	def __str__(self):
		return str(self.values[0])
	def set(self,id,value):
		self.values.insert(id-1,value)
	def get_name(self):
		return self.values[1]
	def get_path(self):
		return self.values[3]
	def get_cmdline(self):
		return self.values[4]

errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().nextCmd(
	cmdgen.CommunityData('test-agent','public'),
	cmdgen.UdpTransportTarget((host, 161)),
	(1,3,6,1,2,1,25,4,2,1))

processes = dict()

for varBT in varBinds:
	key   = varBT[0][0]
	value = varBT[0][1]

	if(key[11] in processes):
		processes[key[11]].set(key[10],value)
	else:
		processes[key[11]] = Process(key[11])

for pid in processes:
	proc = processes[pid]
	print pid, proc.get_name(), proc.get_path(), proc.get_cmdline()
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: