Created
August 9, 2012 08:05
-
-
Save nfarring/3302189 to your computer and use it in GitHub Desktop.
Parsing netstat in Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return_value = [] | |
output = subprocess.Popen( | |
['netstat','--interfaces','all'], | |
stdout=subprocess.PIPE).communicate()[0] | |
#Kernel Interface table | |
#Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg | |
#eth0 1500 0 1229230 0 0 0 1279754 0 0 0 BMRU | |
#lo 16436 0 163 0 0 0 163 0 0 0 LRU | |
lines = output.strip().splitlines() | |
lines.pop(0) | |
lines.pop(0) | |
for line in lines: | |
line = line.strip().split() | |
return_value.append(line[0]) | |
return return_value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This chunk of code parses the output of netstat to list all of the network interfaces on the host. I found an easier way: os.listdir('/sys/class/net').