2013년 12월 13일 금요일

[Python] Windows 2000/XP 에서 네트워크 정보 가져오기

import os
import string
def getnetworkinfo():
    ret = []

    fp = os.popen("ipconfig /all")
    data = fp.readlines()
    fp.close()

    adapter=[]
    i=-1
    for info in data:
        if info.find("Ethernet adapter")==0:
            i=i+1
            adapter.append([])
        if i>=0:
            adapter[i].append(info)

    adapter.reverse()
    for adinfo in adapter:
        desc=""
        ip=[]
        dns=[]
        subnet=[]
        gateway=[]
        mac=""

        adinfo.reverse()
        for info in adinfo:
            info = info.split(':')
            if len(info)<2:
                continue
            info = map(string.strip, info)
            if info[0].find("Description")==0:
                desc=info[1]
            elif info[0].find("IP Address")==0:
                ip.append(info[1])
            elif info[0].find("DNS Servers")==0:
                dns.append(info[1])
            elif info[0].find("Subnet Mask")==0:
                subnet.append(info[1])
            elif info[0].find("Default Gateway")==0:
                gateway.append(info[1])
            elif info[0].find("Physical Address")==0:
                mac=info[1]
        ret.append({
                'DESC':desc,
                'IP':ip,
                'MAC':mac,
                'SUBNET':subnet,
                'GATEWAY':gateway,
                'DNS':dns
                })
    return ret

if __name__=='__main__':
    import pprint
    pprint.pprint(getnetworkinfo())

댓글 없음:

댓글 쓰기