Last active
August 29, 2015 14:25
-
-
Save rayansostenes/c791fdf54bfb9a90b0c8 to your computer and use it in GitHub Desktop.
Retorna o IP de uma interface de rede
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
#!/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