#!/usr/bin/python3

import sys
import subprocess


def ping_sweep(network_prefix):
    live_hosts = []

    for i in range(1, 254):
        ip = f'{network_prefix}.{i}'
        print(f'Pinging {ip}...', end='')
        result = subprocess.run(
            ['ping', '-c', '1', '-W', '1', ip],
            stdout=subprocess.DEVNULL
        )
        if result.returncode == 0:
            print('Host is up')
            live_hosts.append(ip)
        else:
            print('No response')

    return live_hosts

# Usage
# hosts = ping_sweep('192.168.1')
# print('\nLive hosts:')
# print('\n'.join(hosts))


# Entry point
if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python3 pingsweep.py <network_prefix>")
        print("Example: python3 pingsweep.py 192.168.1")
        sys.exit(1)

    prefix = sys.argv[1]
    hosts = ping_sweep(prefix)

    print("\nLive hosts:")
    for host in hosts:
        print(host)
