Created
February 7, 2018 19:23
-
-
Save tzhenghao/e51ac0a1620d462b26c556932f802bb7 to your computer and use it in GitHub Desktop.
A naive implementation to query an IP address of a machine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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