python 获取linux IP地址

获取IP地址

#!/usr/bin/env python
# -*- coidng:utf-8 -*-
from subprocess import Popen,PIPE

def getIP():
    string = str()
    result_list = list()
    dic = dict()
    p =Popen(['ifconfig'],stdout=PIPE)
    content = p.stdout.read()
    lst = [item for item in content.split('\n') if item]
    for line in lst:
        if line[0].strip():
            result_list.append(string)
            string = line
        else:
            string += line
    result_list.append(string)
    result = [item.split('\n') for item in result_list if not item.startswith('lo') and item ]
    for line in result:
       dic[line[0].split()[0]]= line[0].split()[6].split(':')[1]
    return dic

 

posted @ 2018-02-02 16:01  lichenxi  阅读(292)  评论(0编辑  收藏  举报