Skip to content

Instantly share code, notes, and snippets.

@justinmassiot
Last active December 13, 2023 17:14
Show Gist options
  • Save justinmassiot/6bd4e61c8b499b4d8036cf84b6fcbc75 to your computer and use it in GitHub Desktop.
Save justinmassiot/6bd4e61c8b499b4d8036cf84b6fcbc75 to your computer and use it in GitHub Desktop.
Obtain the IP address of the designated interface (Linux systems)
import socket
def get_ipaddress(network='wlan0'):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', network[:15])
)[20:24])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment