#!/usr/bin/python
import os
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])
def get_ip():
f=os.popen("ifconfig -s|grep -v Iface|grep -v lo|awk '{print $1}'")
interface=f.readlines()
f.close()
ip_dic={}
ip_list=[]
for ifname in interface:
ifname=ifname.strip()
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ipaddr=socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
ip_dic[ifname]=ipaddr
ip_list.append(ipaddr)
#return ip_list
return ip_dic
if __name__ == '__main__':
print get_ip_address('eth0')
print get_ip()
阅读(5932) | 评论(1) | 转发(0) |