Skip to content

Instantly share code, notes, and snippets.

@tzhenghao
Created February 7, 2018 19:23
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 tzhenghao/e51ac0a1620d462b26c556932f802bb7 to your computer and use it in GitHub Desktop.
Save tzhenghao/e51ac0a1620d462b26c556932f802bb7 to your computer and use it in GitHub Desktop.
A naive implementation to query an IP address of a machine
from subprocess import Popen,PIPE
def get_ip_addresses():
c1 = Popen(["ip","addr"], stdout=PIPE)
addr_table = c1.communicate()[0]
addr_table = addr_table.split("\n")
index = 0
while index < len(addr_table):
if "eth0" in addr_table[index]:
break
index += 1
while index < len(addr_table):
if "inet " in addr_table[index] and "/" in addr_table[index]:
fields = addr_table[index].split("inet ")
return fields[1].split("/")[0]
index += 1
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment