parse netstat command lines output into python named-tuples!

copyright (C) qingfeng Xia 2011-2020 CC-BY-NC 4.0

“”” 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!

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;

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

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

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

v0.1 beta testing, blocking mode
v0.2 add one nonblocking runner thread, to call subprocess.Popen,
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

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

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”

#example of using this module in blocking mode:
import pynetstat
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=netstat(“-u -e”)   # now show UDP connections
s.update()  # a bloking operation
if s.haserror:
print “there is some error happend!”
s.tofile()  #save as csv, by default    sep=’|’,

#nonblocking mode in v0.2
import time
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:
print “show all the functions of netstat class”



source file is renamed and zipped for download v 0.1

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

CC-BY-NC 4.0 licensed free for non-commercial usage
Author: Qingfeng XIA
copyright (C) 2011-2020
please keep the original link in your reference.
This entry was posted in Download, Programming. Bookmark the permalink.