python取系统主机名和IP地址以及过滤网卡信息等

发布时间:2021-01-11编辑:脚本学堂
使用python取系统主机名和IP地址以及过滤网卡信息等,供大家学习参考。

使用python取系统主机名和IP地址以及过滤网卡信息等,供大家学习参考。

Windows 主机名IP地址:

复制代码 代码如下:

#!C:python26python.exe

# -*- coding:UTF-8 -*-

import socket

# 主机名
name = socket.gethostname()
print name

# 主机名(带域的后缀)
myfullname = socket.getfqdn(socket.gethostname())
print myfullname

# IP 地址
ip_addr = socket.gethostbyname(name)
print ip_addr

# 主机名和IP地址(多网卡)
socket.gethostbyname_ex(socket.gethostname())

linux 主机名IP地址:

复制代码 代码如下:

#!C:python26python.exe
# -*- coding:UTF-8 -*-

import socket
import fcntl
import struct

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,
        struct.pack('256s', ifname[:15])
    )[20:24])

print "LoopBack Addr: " + get_ip_address('lo')
print "My IP Address: " + get_ip_address('eth0')
 

 
Windows 用命令过滤网卡信息:

复制代码 代码如下:

#!C:python26python.exe
# -*- coding:gbk -*-
import os,sys
import socket

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 = '本地连接'
    HOST_INFO = GetInfo(NetWork_Adapter)
    print 'mac地址 ............................. ' + HOST_INFO[1]
    print 'IP地址 .............................. ' + HOST_INFO[2]
    print '子网掩码 ............................ ' + HOST_INFO[3]
    print '网关地址 ............................ ' + HOST_INFO[4]

复制代码 代码如下:

#!C:python26python.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]