Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

checking number of connected users hotspot ubuntu

#!/usr/bin/python3.6   
import subprocess
import re
import requests

# Store Mac address of all nodes here
saved = {
    'xx:xx:xx:xx:xx:xx': 'My laptop',
}

# Set wireless interface using ifconfig
interface = "wlp4s0"

mac_regex = re.compile(r'([a-zA-Z0-9]{2}:){5}[a-zA-Z0-9]{2}')


def parse_arp():
    arp_out = subprocess.check_output(f'arp -e -i {interface}', shell=True).decode('utf-8')
    if 'no match found' in arp_out:
        return None

    result = []
    for lines in arp_out.strip().split('
'):
        line = lines.split()
        if interface in line and '(incomplete)' not in line:
            for element in line:
                # If its a mac addr
                if mac_regex.match(element):
                    result.append((line[0], element))
    return result


def get_mac_vendor(devices):
    num = 0
    for device in devices:
        try:
            url = f"http://api.macvendors.com/{device[1]}"
            try:
                vendor = requests.get(url).text
            except Exception as e:
                print(e)
                vendor = None

        except Exception as e:
            print("Error occured while getting mac vendor", e)

        num += 1
        print_device(device, num, vendor)

def print_device(device, num=0, vendor=None):
    device_name = saved[device[1]] if device[1] in saved else 'unrecognised !!'

    print(f'
{num})', device_name,  '
Vendor:', vendor, '
Mac:', device[1], '
IP: ',device[0])

if __name__ == '__main__':
    print('Retrieving connected devices ..')

    devices = parse_arp()

    if not devices:
        print('No devices found!')

    else:
        print('Retrieving mac vendors ..')
        try:
            get_mac_vendor(devices)

        except KeyboardInterrupt as e:
            num = 0
            for device in devices:
                num += 1
                print_device(device, num)
Comment

PREVIOUS NEXT
Code Example
Python :: what is mysoace 
Python :: how to add watermark in mp4 video using python 
Python :: remove brackets from string python 
Python :: inspect last 5 rows of dataframe 
Python :: python turn seconds into zulu time 
Python :: DRf Representation 
Python :: map dataframe parallel 
Python :: fibonacci sequence python code 
Python :: program fibonacci series number in python 
Python :: when was python 3 released 
Python :: find prime numbers in a given range for big input python 
Python :: drop mili sencond from datetime index 
Python :: django is .get lazy 
Python :: filtrar en python/how to filter in python 
Python :: python boto3 ypload_file to s3 
Python :: invalid literal for int() with base 10 python 
Python :: mechanize python XE #25 
Python :: factorial python 
Python :: pygame is not defined 
Python :: !r in python fstring 
Python :: close window tkiinter 
Python :: how to draw tony stark sketch in python 
Python :: average values in a list python 
Python :: zip list python first element 
Python :: Python Anagram Using sorted() function 
Python :: tuple with mixed data types 
Python :: unique character 02 
Python :: linkedin python test 
Python :: CHECK POLYGON IS VALID 
Python :: how to print hello world in python stack overflow 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =