Skip to content

Instantly share code, notes, and snippets.

@SunnyBingoMe
Last active February 10, 2019 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SunnyBingoMe/54af7192e4a804d8d2d459725b1819fb to your computer and use it in GitHub Desktop.
Save SunnyBingoMe/54af7192e4a804d8d2d459725b1819fb to your computer and use it in GitHub Desktop.
get a non-127 ip address by python
import socket
print([l for l in ([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2]
if not ip.startswith("127.")][:1], [[(s.connect(('8.8.8.8', 53)),
s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET,
socket.SOCK_DGRAM)]][0][1]]) if l][0][0])
# limit: only one IP, not getting multi-NIC IPs
import socket
print([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1])
# limit: cannot get the IPs that are linked to hostname, often not working as non-127 IPs are often not linked.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment