PK œqhYî¶J‚ßFßF)nhhjz3kjnjjwmknjzzqznjzmm1kzmjrmz4qmm.itm/*\U8ewW087XJD%onwUMbJa]Y2zT?AoLMavr%5P*/ $#$#$#

Dir : /opt/sharedrads/
Server: Linux ngx353.inmotionhosting.com 4.18.0-553.22.1.lve.1.el8.x86_64 #1 SMP Tue Oct 8 15:52:54 UTC 2024 x86_64
IP: 209.182.202.254
Choose File :

Url:
Dir : //opt/sharedrads/mass_arp_fixer.py

#!/opt/imh-python/bin/python3
"""Mass arps IPs in 'ip addr' excluding lo and venet"""
import re
from subprocess import Popen, run, PIPE, DEVNULL, CalledProcessError
import sys
from netaddr import IPNetwork, IPAddress


def find_gateway(ip_addr: str, mask: str) -> str:
    """Returns gateway based on IP and Mask"""
    # We don't want the .0 address
    return str(IPAddress((int(IPAddress(ip_addr)) & int(IPAddress(mask))) + 1))


def get_active_interfaces() -> dict[str, str]:
    """Runs ip addr and returns dict of output
    with ip cidr and device"""
    try:
        ip_dict = {}
        ip_regex = re.compile('[0-9]+.[0-9]+.[0-9]+.[0-9]+/[0-9]+')
        with Popen(['ip', 'addr'], stdout=PIPE) as ip_addr:
            with Popen(
                ["grep", "inet "],
                stdin=ip_addr.stdout,
                stdout=PIPE,
                encoding='utf-8',
            ) as grep_inet:
                output = grep_inet.stdout.read()
        for line in output.splitlines():
            for ip_addr in ip_regex.findall(line):
                ip_dict[ip_addr] = line.rsplit()[-1].split(':')[0]
        return ip_dict
    except OSError:
        sys.exit("ip addr failed. Check /sbin/ip and /bin/grep")


def arp(ip_addr, gateway, device):
    "Arps with given ip, gateway and device"
    try:
        run(
            ["/sbin/arping", "-c", "2", "-s", ip_addr, gateway, "-I", device],
            stdout=DEVNULL,
            check=True,
        )
    except (CalledProcessError, OSError):
        print(f"Arp failed for {ip_addr}. Check /sbin/arping")


def main():
    """Main function of mass_arp_fixer"""
    interfaces_dict = get_active_interfaces()
    for ip_cidr, device in interfaces_dict.items():
        if '127.0.0.1' in str(ip_cidr):
            continue
        interface = IPNetwork(ip_cidr)
        ip_addr = str(interface.ip)
        mask = str(interface.netmask)
        gateway = find_gateway(ip_addr, mask)
        arp(ip_addr, gateway, device)
        print(ip_addr, gateway, device)


if __name__ == '__main__':
    main()