1. 程式人生 > >python獲取網絡卡IP地址

python獲取網絡卡IP地址

#!/usr/bin/env python 
# -*- 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,  # SIOCGIFADDR 
        struct.pack('256s', ifname[:15]) 
    )[20:24]) 
 
print "eth0 = "+ get_ip_address('eth0') 
print "lo = " + get_ip_address('lo')


如果返回沒有的網絡卡會出錯

Traceback (most recent call last):  File "getmac.py", line 17, in <module>    print "eth1 = "+ get_ip_address('eth1')   File "getmac.py", line 13, in get_ip_address    struct.pack('256s', ifname[:15]) IOError: [Errno 99] Cannot assign requested address

第二種方法:

利用命令:

ip=`ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'`
mac=`ifconfig | grep HWaddr | awk -F" " '{print $5}'`

import  os

tmptxt=" ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'"

IPTMP=os.popen(tmptxt).read()

IP=IPTMP.replace("\n","")
SIOCGIFHWADDR 和 0x8927是一個意思 /usr/include/linux/sockios.h 裡有定義

相關推薦

python獲取IP地址

#!/usr/bin/env python # -*- coding: utf-8 -*- import socket import fcntl import struct def get_ip_address(ifname): s = sock

C#之獲取IP地址

有時候不想讀取配置檔案來進行網路監聽,預設把本級所有IP地址監聽一遍,這個時候就需要獲取本級所有IP地址。 如下: string name = Dns.GetHostName(); IPAddress[] ipadrlist = Dns.Get

shell 獲取IP地址掩碼 預設閘道器 廣播地址 MAC地址

# IP地址 ifconfig eth0 | grep "inet addr:" | awk -F " " '{print $2}' | awk -F ":" '{print $2}' # 廣播地址 ifconfig eth0 | grep "inet addr:" |

LINUX下獲取IP地址和MAC地址,子掩碼程式參考

/* mode time:20120727 LINUX下獲取IP地址和MAC地址.程式相關結構體在程式後面。 列印網絡卡的ip地址 子網掩碼 廣播地址 mac地址 環境: [[email protected] temp]# uname -a Linux b

linux獲取活動ip地址

1、根據ioctl機制列印當前所有網絡卡 程式碼: #include <sys/ioctl.h> #include <net/if.h> #include <netinet/in.h> #include <arpa/inet.h> #include <

delphi 獲取IP地址列表和Mac地址

1、宣告windows系統的sendarp函式 function sendarp(ipaddr: ulong; temp: dword; ulmacaddr: pointer; ulmacaddrleng: pointer): Dword; StdCall;External

linux c/c++按規則獲取ip

linux c/c++按規則獲取網絡卡ip 輸出專案到雲或者輸出給外部客戶,會遇到伺服器多網絡卡多ip的情形,如果有多個應用都需要這個主機ip,而且多應用需要獲取相同的ip,此時可以約定一種規則來獲取相同的ip,比如: 獲得所有網絡卡名,然後對網絡卡名按從小到大排序,查詢最小

ip addr add和ifconfig的區別看linuxip地址的結構

                今天一個老外在郵件列表上問了一個問題,就是ip addr add和ifconfig的區別,我給他進行了解答,可能因為英語不好吧,解答的很簡單,因此我還是要在這裡詳細說明一下。其實它們之間沒有什麼區別,只 是表述方式不同罷了。如果你非常理解網路協議的原理以及網路的分層架構那麼我想

C#獲取Mac地址

需要using System.Management; /// <summary> /// Get LocalHost MAC Address /// </summary> /// <returns></returns> pub

linux下C語言獲取MAC地址

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/ioctl.h>#include <sys/socket.h&g

linux配置IP地址命令詳細介紹及一些常用網路配置命令

Linux命令列下配置IP地址不像圖形介面下那麼方 便,完全需要我們手動配置,下面就給大家介紹幾種配置的方法: 即時生效(重啟後失效): ifconfig eth0 192.168.1.102 netmask 255.255.255.0  //新增IP地址 rout

Shell獲取Mac地址(grep、正則表示式)

#!/bin/bash #brief attain the Mac addr of netcard eth0. #author lee #time 18.08.10 macaddr=`sudo

winPcap獲取網路地址和子掩碼

下面是獲取網路地址(不是IP地址)和子網掩碼的示例,沒時間接著往下做例子了,因為接下來需要在LINUX下面使用libPcap,當然我會貼出程式碼,會linux程式設計的大牛一般都會,所以準確的說是貼給自己的,喜歡玩資料包的朋友自己看官方例子就行 #include<pc

Linux下通過shell獲取ip地址和mac地址

ip=`ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'` mac=`ifconfig | grep HWaddr | awk -F" " '{print $5}'` 轉自:http://blog.csdn.

Linux下多程序程式設計小例——獲取IP地址

Linux下多程序程式設計的核心是呼叫fork()系統呼叫用來建立一個新的程序:pid_t   fork(void);  由fork()建立的新程序被稱為子程序。fork()函式被呼叫一次,但有兩次返回。 返回值=0:  子程序              返回值>0: 

linux C語言獲取對應IP地址

int get_gw_ip(char *eth, char *ipaddr){ int sock_fd; struct  sockaddr_in my_addr; struct ifreq ifr;  /**//* Get socket file descriptor */ 

C++ 中通過GetAdaptersInfo獲取配置和Ip地址資訊

#include <WinSock2.h> #include <Iphlpapi.h> #include <iostream> using namespace std; #pragma comment(lib,"Iphlpapi.lib") //需要新增Iphlpapi

獲取的硬體地址ip 地址

這是獲取網絡卡的硬體地址的程式碼,記得把下面的這幾個標頭檔案加上把。 #include <sys/socket.h> // Per msqr #include <sys/sysctl.h> #include <net/if.h> #

socket 通過 ioctl獲取 設定 IP掩碼 本地廣播地址

參看 http://blog.chinaunix.net/uid-20692625-id-3172833.html 類別 Request 說明 資料型別 套  接  口 SIOCATMARK  SIOCSPGRP  SIOCGPGRP 是否位於帶外標

c# 多 由【網路介面卡名】獲取資訊,IP

c# 多網絡卡 由【網路介面卡名】獲取網絡卡資訊,IP 多網絡卡電腦中,網路介面卡的名字 多樣化! 專案中需要,根據網路介面卡 名字 獲取 單個網絡卡的IP: using System.Net.NetworkInformation;