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

20070319

Setting up an rsync server

Filed under: Experiences — abbe @ 0159

Just 30 minutes back, I’d setup an rsync server. And thanks to Arindam Biswas who gave me the idea of using rsync for upstream transfer. Setting up an rsync server is very easy. Just create an /etc/rsyncd.conf with following content:

uid = nobody
gid = nobody
use chroot = no

[content]
path = /path/to/content
comment = Content

And, now start rsync in server mode with following command:

root@box# rsync --daemon

When done rsyncing kill the daemon process.

20070317

Connection to mercedes.homelinux.org closed.

Filed under: Personal — abbe @ 0229
Connection to mercedes.homelinux.org closed.

20070309

Re: ports/109744: Update port: net/mugshot: Update to v1.1.37

Filed under: Hacking — abbe @ 1921
Date: Mon, 5 Mar 2007 15:51:18 GMT
From: Cheng-Lung Sung 
Message-Id: 
To: wahjava@gmail.com, clsung@FreeBSD.org, clsung@FreeBSD.org
Subject: Re: ports/109744: Update port: net/mugshot: Update to v1.1.37

Synopsis: Update port: net/mugshot: Update to v1.1.37

State-Changed-From-To: open->closed
State-Changed-By: clsung
State-Changed-When: Mon Mar 5 15:51:18 UTC 2007
State-Changed-Why: 
Committed. Thank You.

http://www.freebsd.org/cgi/query-pr.cgi?pr=109744

20070304

Google Bookmarks via RSS

Filed under: Experiences — abbe @ 2128

Google Bookmarks via RSS
Cool, accessing Google bookmarks via RSS.

Create a free website or blog at WordPress.com.