Skip to content

Instantly share code, notes, and snippets.

@nfarring
Created August 9, 2012 08:05
Show Gist options
  • Save nfarring/3302189 to your computer and use it in GitHub Desktop.
Save nfarring/3302189 to your computer and use it in GitHub Desktop.
Parsing netstat in Linux
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
@nfarring
Copy link
Author

nfarring commented Aug 9, 2012

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').

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment