Skip to content

Instantly share code, notes, and snippets.

@goyalankit
Last active May 9, 2017 05:37
Show Gist options
  • Save goyalankit/7ae7e967e68b1c2465646962e842ed2a to your computer and use it in GitHub Desktop.
Save goyalankit/7ae7e967e68b1c2465646962e842ed2a to your computer and use it in GitHub Desktop.
Iterate through all the interfaces and check if they are in promiscuous mode.
# This script goes through all the network devices on the system
# and determines if the device in promiscuous mode.
#
# More information at: http://goyalankit.com/blog/promiscuous-mode-detection
#
# Usage: python is_promiscuous.py
# Author: Ankit Goyal (http://github.com/goyalankit)
import os
NET_DEVICE_PATH = '/sys/devices/virtual/net/'
IFF_PROMISC = 0x100
def print_results(table):
header = [('Device', 'Promisc Mode'), ('------', '---------------')]
table = header + table
col_width = [max(len(x) for x in col) for col in zip(*table)]
for line in table:
print "| " + " | ".join("{0:{1}}".format(x, col_width[i])
for i, x in enumerate(line)) + " |"
def iterate_and_determine_promisc():
device_promisc = []
for device in os.listdir(NET_DEVICE_PATH):
with open(os.path.join(NET_DEVICE_PATH, device, 'flags')) as f:
device_flags = f.read()
if device_flags:
if int(hex(int(device_flags, 16) & IFF_PROMISC), 16):
device_promisc.append((device, 'ON'))
else:
device_promisc.append((device, 'OFF'))
else:
device_promisc.append((device, 'UNKNOWN'))
print_results(device_promisc)
iterate_and_determine_promisc()
@goyalankit
Copy link
Author

Sample output:

$ python is_promiscuous.py
| Device     | Promisc Mode    |
| ------     | --------------- |
| mybond0    | ON              |
| virbr0     | OFF             |
| virbr1     | OFF             |
| vpeer0     | OFF             |
| virbr0-nic | ON              |
| virbr1-nic | ON              |
| lo         | OFF             |
| br0        | OFF             |
| veth1      | ON              |
| veth0      | ON              |

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