Skip to content

Instantly share code, notes, and snippets.

@rmamba
Last active July 11, 2016 10:45
Show Gist options
  • Save rmamba/1960a9e8011334b0c4ac692d3565eb12 to your computer and use it in GitHub Desktop.
Save rmamba/1960a9e8011334b0c4ac692d3565eb12 to your computer and use it in GitHub Desktop.
return IP address of network interface
#(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