shell获取IP地址
/sbin/ifconfig|sed -n '/inet addr/s/^[^:]*:\([0-9.]\{7,15\}\) .*/\1/p' | grep -v 127.0.0.1
python获取IP地址
方法1:
def get_local_ip(ifname):
import socket, fcntl, struct
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
inet = fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15]))
ret = socket.inet_ntoa(inet[20:24])
return ret
print get_local_ip("eth0")
方法2:
import os
tmptxt=" /sbin/ifconfig eth0 |grep -w \"inet addr\" |cut -d: -f2 |cut -d\" \" -f1 "
IPTMP=os.popen(tmptxt).read()
IP=IPTMP.replace("\n","")
print IP