# Buggy code
def get_if_raw_addr(ifname):
    """Returns the IPv4 address configured on 'ifname', packed with inet_pton."""  # noqa: E501

    ifname = network_name(ifname)

    # Get ifconfig output
    subproc = subprocess.Popen(
        [conf.prog.ifconfig, ifname],
        close_fds=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    stdout, stderr = subproc.communicate()
    if subproc.returncode:
        warning("Failed to execute ifconfig: (%s)", plain_str(stderr))
        return b"\0\0\0\0"
    # Get IPv4 addresses

    addresses = [
        line.strip() for line in plain_str(stdout).splitlines() if "inet " in line
    ]

    if not addresses:
        warning("No IPv4 address found on %s !", ifname)
        return b"\0\0\0\0"

    # Pack the first address
    address = addresses[0].split(" ")[1]
    if "/" in address:  # NetBSD 8.0
        address = address.split("/")[0]
    return socket.inet_pton(socket.AF_INET, address)


# Traceback (OSError)
% scapy
scapy.__version__
'2.4.4.dev265'
conf.use_pcap
False
pkt = sr1(IP(dst="192.168.43.5")/UDP(sport=137, dport=137), timeout=2, verbose=0)
Traceback (most recent call last):
File "/Users/user/.pyenv/versions/3.8.1/lib/python3.8/site-packages/scapy/arch/bpf/supersocket.py", line 73, in __init__
fcntl.ioctl(self.ins, BIOCSETIF, struct.pack("16s16x", self.iface.encode()))  # noqa: E501
OSError: [Errno 6] Device not configured

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/user/.pyenv/versions/3.8.1/lib/python3.8/site-packages/scapy/sendrecv.py", line 541, in sr1
s = conf.L3socket(promisc=promisc, filter=filter,
File "/Users/user/.pyenv/versions/3.8.1/lib/python3.8/site-packages/scapy/arch/bpf/supersocket.py", line 241, in __init__
super(L2bpfListenSocket, self).__init__(*args, **kwargs)
File "/Users/user/.pyenv/versions/3.8.1/lib/python3.8/site-packages/scapy/arch/bpf/supersocket.py", line 75, in __init__
raise Scapy_Exception("BIOCSETIF failed on %s" % self.iface)
scapy.error.Scapy_Exception: BIOCSETIF failed on bridge1
conf.use_pcap = True
WARNING: No libpcap provider available ! pcap won't be used
pkt = sr1(IP(dst="192.168.43.5")/UDP(sport=137, dport=137), timeout=2, verbose=0)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/user/.pyenv/versions/3.8.1/lib/python3.8/site-packages/scapy/sendrecv.py", line 541, in sr1
s = conf.L3socket(promisc=promisc, filter=filter,
File "/Users/user/.pyenv/versions/3.8.1/lib/python3.8/site-packages/scapy/supersocket.py", line 273, in __init__
self.outs = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)  # noqa: E501
File "/Users/user/.pyenv/versions/3.8.1/lib/python3.8/socket.py", line 231, in __init__
_socket.socket.__init__(self, family, type, proto, fileno)
PermissionError: [Errno 1] Operation not permitted

# Fixed code
def get_if_raw_addr(ifname):
    """Returns the IPv4 address configured on 'ifname', packed with inet_pton."""  # noqa: E501

    ifname = network_name(ifname)

    # Get ifconfig output
    subproc = subprocess.Popen(
        [conf.prog.ifconfig, ifname],
        close_fds=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    stdout, stderr = subproc.communicate()
    if subproc.returncode:
        warning("Failed to execute ifconfig: (%s)", plain_str(stderr).strip())
        return b"\0\0\0\0"

    # Get IPv4 addresses
    addresses = [
        line.strip() for line in plain_str(stdout).splitlines() if "inet " in line
    ]

    if not addresses:
        warning("No IPv4 address found on %s !", ifname)
        return b"\0\0\0\0"

    # Pack the first address
    address = addresses[0].split(" ")[1]
    if "/" in address:  # NetBSD 8.0
        address = address.split("/")[0]
    return socket.inet_pton(socket.AF_INET, address)