linux獲取系統 ip地址


獲取Linux上ip地址

linux命令

ifconfig

python程序

import socket

def get_host()
	hostname = socket.gethostname()
    ip = socket.gethostbyname(hostname)
	
	return ip

python程序指定網口

def get_host()
	ifname = "eth0"  # 指定網口
	
    import fcntl, struct, socket
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
   
    ip = socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', bytes(ifname,encoding="utf8")))[20:24])
    
    return ip

注意:

  • struct.pack(str,bytes) ,后面為bytes類型


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM