#!C:\\python26\\python.exe
# -*- coding:UTF-8 -*-
import os,sys
import socket
def cn(s):
''' 中文字符处理 '''
if isinstance(s, unicode):
return s.encode('gb2312')
else:
return s.decode('utf-8').encode('gb2312')
def GetInfo(adp):
''' [AdapterName, MacAddr, IPAddr, NetMask, Gateway] '''
NAME_INFO=[];IP_INFO=[];MAC_INFO=[];MSK_INFO=[];GW_INFO=[]
dict_ip={};dict_mac={};dict_msk={};dict_gw={}
cmd = "ipconfig /all"
txt = os.popen(cmd).readlines()
for line in txt:
if 'Ethernet adapter' in line:
NAME_INFO.append(line.split('adapter')[1].split(':')[0].strip())
if 'Physical Address' in line:
MAC_INFO.append(line.split(':')[1].strip())
if 'IP Address' in line:
IP_INFO.append(line.split(':')[1].strip())
if 'Subnet Mask' in line:
MSK_INFO.append(line.split(':')[1].strip())
if 'Default Gateway' in line:
GW_INFO.append(line.split(':')[1].strip())
for i in range(0,len(NAME_INFO)):
dict_ip[NAME_INFO[i]] = IP_INFO[i]
dict_mac[NAME_INFO[i]] = MAC_INFO[i]
dict_msk[NAME_INFO[i]] = MSK_INFO[i]
dict_gw[NAME_INFO[i]] = GW_INFO[i]
if adp not in dict_ip.keys():
print 'Error: No adapter name "%s" in this computer!' % (adp)
print 'You can select in: %s' % (NAME_INFO)
sys.exit()
else:
return [adp,dict_mac[adp],dict_ip[adp],dict_msk[adp],dict_gw[adp]]
if __name__ == '__main__':
NetWork_Adapter = cn('本地连接')
HOST_INFO = GetInfo(NetWork_Adapter)
print cn('Mac地址 ............................. ') + HOST_INFO[1]
print cn('IP地址 .............................. ') + HOST_INFO[2]
print cn('子网掩码 ............................ ') + HOST_INFO[3]
print cn('网关地址 ............................ ') + HOST_INFO[4]
|