Last active
May 9, 2017 05:37
-
-
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 file contains 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
# 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output: