Last active
July 11, 2016 10:45
-
-
Save rmamba/1960a9e8011334b0c4ac692d3565eb12 to your computer and use it in GitHub Desktop.
return IP address of network interface
This file contains hidden or 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
#(sudo) pip install netifaces | |
import netifaces as ni | |
import sys | |
if name == '__main__': | |
interfaces = ni.interfaces() | |
if len(sys.argv) < 2: | |
print('IP.py interface [json]') | |
print('Interfaces: %s' % interfaces) | |
quit() | |
interface = sys.srgv[1] | |
if interface in interfaces: | |
address = ni.ifaddresses(interface)[2][0]['addr'] | |
if sys.argv[2] == 'json': | |
print('{"IP": "%s"}' % address) | |
else: | |
print(address) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment