Skip to content

Instantly share code, notes, and snippets.

@rayansostenes
Last active August 29, 2015 14:25
Show Gist options
  • Save rayansostenes/c791fdf54bfb9a90b0c8 to your computer and use it in GitHub Desktop.
Save rayansostenes/c791fdf54bfb9a90b0c8 to your computer and use it in GitHub Desktop.
Retorna o IP de uma interface de rede
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import netifaces
def get_if_ipaddr(iface):
"""
Retorna o IP de uma interface de rede
Args:
iface (str): Nome da interface. ex: 'eth0'
Returns:
str: Retorna uma string com o IP no formato '192.168.1.1'
bool: Retorna ``False`` se a interface não tem um
IP ou se não existe no sistema
"""
if iface not in netifaces.interfaces():
return False
addrs = netifaces.ifaddresses(iface)
if netifaces.AF_INET not in addrs:
return False
return addrs[netifaces.AF_INET][0]
if __name__ == '__main__':
print get_if_ipaddr('eth0')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment