Python gets network card information (name, MAC, IP, gateway, etc.)

Source: Internet
Author: User

"Life is short, I use Python." The efficiency of Python is partly inseparable from its rich modules. Python has a number of third-party modules that can help us do something and reduce development time.

A module named "Netifaces" in the Python PyPI Library is a third-party module written in C. OK:

1. Get all the gateways for this machine

2. Get all the interface interface (NIC NICs) for this machine

3. Get details of the interface specified by the machine, including IP address, subnet mask, broadcast address, MAC address, etc.

Unfortunately, this module is too limited in functionality and will bring out some confusing information, such as the incorrect subnet mask on Windows systems.

PS: To get a public address, you can use a number of APIs, such as:

# use 3rd party web-sites to get your IP
# Please note that I don't 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

Run as follows:

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;border-bottom:0px;border-left:0px; " Border= "0" alt= "image" Src= "http://s3.51cto.com/wyfs02/M02/89/B8/wKiom1gatDmTQ_RmAAAofnjl5os468.png" height= "123" />

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;border-bottom:0px;border-left:0px; " Border= "0" alt= "image" Src= "http://s3.51cto.com/wyfs02/M02/89/B6/wKioL1gatDrwvIhqAABFd-bLWKE885.png" height= "388" />

code please go to GitHub: https://github.com/DingGuodong/LinuxBashShellScriptForOps/blob/master/projects/WindowsSystemOps/Network/ getnetworkstatus.py

The code is as follows:

#!/usr/bin/python# encoding: utf-8# -*- coding: utf8 -*-"" "Created by  pycharm.file:                linuxbashshellscriptforops:getnetworkstatus.pyuser:                GuodongCreate Date:         2016/11/2create time:        16:20show windows or  linux network nic status, such as mac address, gateway, ip  address, etc# python getnetworkstatus.pyrouting gateway:                10.6.28.254Routing NIC Name:               eth0Routing NIC MAC  address:       06:7f:12:00:00:15Routing IP Address:             10.6.28.28Routing IP Netmask:             255.255.255.0  "" "import osimport systry:     import netifacesexcept 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  Netifacesroutinggateway = 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:             passdisplay_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)

Finally: do not make the wheel again. Repeat manufacturing wheels for yourself, although the process of manufacturing is learning to consolidate the process, but the repetition of the production of the wheel is not good for others, life is too short, do not repeat the production of wheels, unless you make good enough.

Tag:python get the MAC address, Python Gets the gateway address, Python gets the IP address

--end--

This article is from "Communication, My Favorites" blog, please make sure to keep this source http://dgd2010.blog.51cto.com/1539422/1868851

Python gets network card information (name, MAC, IP, gateway, etc.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.