日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python获取eth0_Python获取网卡信息(名称、MAC、IP、网关等)

發(fā)布時間:2024/9/30 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python获取eth0_Python获取网卡信息(名称、MAC、IP、网关等) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

“人生苦短,我用Python”。Python的高效有一部分是跟它豐富的模塊分不開的。Python有很多第三方模塊可以幫助我們完成一些事情,減少開發(fā)時間。

Python pypi庫中一個模塊名字叫“netifaces”,使用C語言寫的一個第三方模塊。可以:

1.獲取本機(jī)的所有網(wǎng)關(guān)

2.獲取本機(jī)所有的接口Interface(網(wǎng)卡NIC)

3.獲取本機(jī)指定接口的詳細(xì)信息,包括IP地址、子網(wǎng)掩碼、廣播地址、MAC地址等

不過遺憾的是這個模塊的功能太有限以及會帶出一些令人困惑的信息,例如Windows系統(tǒng)上的子網(wǎng)掩碼可能不正確等。

PS:要想獲取公網(wǎng)地址,可以使用很多種API,例如:

# Use 3rd party web-sites to get your IP

# Please note that I do not recommend following curl/wget method due to security reasons. You have been warned:

curl ifconfig.me

curl icanhazip.com

curl ipecho.net/plain

curl ifconfig.co

curl http://ip.chinaz.com/getip.aspx

運行截圖如下:

代碼如下:#!/usr/bin/python

# encoding: utf-8

# -*- coding: utf8 -*-

"""

Created by PyCharm.

File: LinuxBashShellScriptForOps:getNetworkStatus.py

User: Guodong

Create Date: 2016/11/2

Create Time: 16:20

show Windows or Linux network Nic status, such as MAC address, Gateway, IP address, etc

# python getNetworkStatus.py

Routing Gateway: 10.6.28.254

Routing NIC Name: eth0

Routing NIC MAC Address: 06:7f:12:00:00:15

Routing IP Address: 10.6.28.28

Routing IP Netmask: 255.255.255.0

"""

import os

import sys

try:

import netifaces

except ImportError:

try:

command_to_execute = "pip install netifaces || easy_install netifaces"

os.system(command_to_execute)

except OSError:

print "Can NOT install netifaces, Aborted!"

sys.exit(1)

import netifaces

routingGateway = netifaces.gateways()['default'][netifaces.AF_INET][0]

routingNicName = netifaces.gateways()['default'][netifaces.AF_INET][1]

for interface in netifaces.interfaces():

if interface == routingNicName:

# print netifaces.ifaddresses(interface)

routingNicMacAddr = netifaces.ifaddresses(interface)[netifaces.AF_LINK][0]['addr']

try:

routingIPAddr = netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['addr']

# TODO(Guodong Ding) Note: On Windows, netmask maybe give a wrong result in 'netifaces' module.

routingIPNetmask = netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['netmask']

except KeyError:

pass

display_format = '%-30s %-20s'

print display_format % ("Routing Gateway:", routingGateway)

print display_format % ("Routing NIC Name:", routingNicName)

print display_format % ("Routing NIC MAC Address:", routingNicMacAddr)

print display_format % ("Routing IP Address:", routingIPAddr)

print display_format % ("Routing IP Netmask:", routingIPNetmask)

最后:不要重復(fù)制造輪子。重復(fù)制造輪子對自己而言,雖然制造的過程是學(xué)習(xí)鞏固的過程,但重復(fù)制造輪子對別人沒有好處,人生苦短,別重復(fù)制造輪子,除非你制造的足夠好。

tag:python獲取MAC地址,python獲取網(wǎng)關(guān)地址,python獲取IP地址

--end--

總結(jié)

以上是生活随笔為你收集整理的python获取eth0_Python获取网卡信息(名称、MAC、IP、网关等)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。