imll
imll

Reputation: 341

Obtain network IP from interface's IP and netmask

Using python3, how is it possible to get the network IP address from an interface IP and netmask?

Example:

Having:

eth0 IP: 192.168.1.11  # string
netmask: 255.255.255.0    # string

I need to obtain:

network IP: 192.168.1.0 # string

Upvotes: 1

Views: 252

Answers (1)

oppressionslayer
oppressionslayer

Reputation: 7224

You can use the Python ipaddress module:

import ipaddress
net = ipaddress.ip_network('192.168.1.11/255.255.255.0', strict=False)                                                                                                              

net.network_address                                                                                                                                                                 
# IPv4Address('192.168.1.0')

Upvotes: 2

Related Questions