I'm looking for a way to get output like "192.168.1.0/24".
I know I can get my IP address by
my_ip = socket.gethostbyname(socket.gethostname())
but that's not the thing I want. I should also mention that I'm using Linux
I'm looking for a way to get output like "192.168.1.0/24".
I know I can get my IP address by
my_ip = socket.gethostbyname(socket.gethostname())
but that's not the thing I want. I should also mention that I'm using Linux
Finally found a soulution
import socket
import fcntl
import struct
import ipaddress
iface = "enp0s3"
subnet_mask = socket.inet_ntoa(fcntl.ioctl(socket.socket(socket.AF_INET, socket.SOCK_DGRAM), 35099, struct.pack(b'256s', iface.encode()))[20:24])
my_ip = socket.gethostbyname(socket.gethostname())
network = ipaddress.IPv4Network(my_ip+"/"+subnet_mask, strict=False)
print(network)
prints 192.168.1.0/24