#!/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|
linuxjishu/13830.html target=_blank class=infotextkey>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()