parse netstat command lines output into python named-tuples!

copyright (C) 2011-2013 iesensor.com

“”” parse netstat command lines output into python named-tuples!

update v0.2 and onwards has been uploaded to PyPI; using pip to install it

“”” parse netstat command lines output into python named-tuples!

Features:
parse output into list of named_tuple: inet_connection,but each field is still in str type;
display DNS name by default,  using ‘-n’ to get numeric repr of result,
select() and find() by varios condition;

documentation:
get the exmaple by: print pynetstat.__usage__ (support doctest)
and read __main__ function (an simple test procedure) in this script

note:
usage of netstat needs admin’s right on windows to get program name,
but it is not needed on Linux

implementation:
since  the output format of netstat is stable, it is possible to parse the output string
see readme.txt for details of implementation
all the fields will be convert to specific type, but keep as str type
if numeric format string is preferred, try “-n” options, by default, DNS name, or port repr name is used
Since each kind of protocol family has different headers, they are queried in seperated command line
Also, summery (-s) and interface (-i) also started in seperated subprocess

changelog:
v0.1 beta testing, blocking mode
v0.2 add one nonblocking runner thread, to call subprocess.Popen,
by default: AUTO_UPDATING=False and NONBLOCKING_MODE=True
These switches can be set after import and before instantiate netstat() class

Operation System Support:
detect Windows or Linux platform, python 2 and python 3
If the OS difference is distinct, using class to fit each os.
only ASCII and UTF-8 english version OS is tested

tested:
pyhton 2.7(32bit) on win7 64bit;
pyhton 2.7(64bit)  ubuntu 12.04 64bit;
python 3.3 on on win7 64bit, but test failed due to string parse operation

todo:
pyhton 3.x support:    string operation errors is found, try 2to3
currently all output fields are string type, but python 3.3 got ipaddress module,
Mac OS X support:     no such OS to test
using LINUX command options to unify all platform?:  ->not sure it is a good ideal
How to test whether user has admin right on windows?  :  -> LINUX: os.geteuid()==0
how to start the refresh in another thread, so do not block the caller -> done in v0.2
continous mode is not supported, manually refresh the stat! -> call update() manually
“how to split -aep into -a -e -p ???”

“””

from __future__ import print_function
__author__=”Qingfeng Xia”
__version__=”0.2.0″

__usage__=”””
#example of using this module in blocking mode:
import pynetstat
pynetstat.NONBLOCKING_MODE=False
from pynetstat import netstat
s=netstat(“-t”, “-e”)
# list of strings or  single strings with options seperated by a single space key
s.update()  # a bloking operation,#netstat program needs several seconds
#it can auto start after ctor(),  if pynetstat.AUTO_UPDATING=True
s.display()
s=netstat(“-u -e”)   # now show UDP connections
s.update()  # a bloking operation
if s.haserror:
print “there is some error happend!”
else:
s.tofile()  #save as csv, by default    sep=’|’,

#nonblocking mode in v0.2
import time
pynetstat.NONBLOCKING_MODE=True
s.update()
s=netstat(“-t”, “-e”)   # keyword arg:  nonblocking_mode=True
while not s.ready:     # make sure check the readiness before saving or displaying
#do other things, or just wait 1second:
time.sleep(1000)
s.display()
print “show all the functions of netstat class”
dir(netstat)
“””

 

 

source file is renamed and zipped for download v 0.1

http://www.iesensor.com/download/pynetstat.zip

update v0.2 and onwards has been uploaded to PyPI; using pip to install it

BSD documentation licensed Free for non-commercial usage only
Author: Qingfeng XIA copyright (C) 2011-2013 http://www.iesensor.com
please keep the original link in your reference. http://www.iesensor.com/blog/2012/12/11/pynetstat/
This entry was posted in Download, Programming. Bookmark the permalink.