First, we import socket and os libraries from python libraries.
import socket import os
then we find our ip address with the code below.
gw = os.popen("ip -4 route show default").read().split() s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect((gw[2], 0)) ipaddr = s.getsockname()[0] gateway = gw[2] host = socket.gethostname() print ("IP:", ipaddr, " GW:", gateway, " Host:", host)
Tagged In:
Python