Is there a function of ipaddress to get the subnet only from input like 192.168.1.129/25

Daniel Flick daniel.p.flick at gmail.com
Tue Oct 17 21:28:43 EDT 2017


On Tuesday, October 17, 2017 at 8:27:03 PM UTC-5, Daniel Flick wrote:
> On Tuesday, October 17, 2017 at 8:24:52 PM UTC-5, Daniel Flick wrote:
> > On Tuesday, October 17, 2017 at 5:01:13 PM UTC-5, Peter Otten wrote:
> > > Daniel Flick wrote:
> > > 
> > > > On Tuesday, October 17, 2017 at 4:25:02 PM UTC-5, Daniel Flick wrote:
> > > >> <SNIP>
> > > >> Peter, I am not following.  Are you saying that there is a function that
> > > >> returns the network only?  network_address was giving me the mask
> > > >> attached to the end but maybe I was doing something wrong.
> > > >> 
> > > >> For an input of LAN_IP=192.168.99.1/24
> > > >> ipaddress.IPv4Interface(LAN_IP).ip
> > > >> returns 192.168.99.0/24
> > > >> 
> > > >> I need the 192.168.99.0 part only.
> > > > 
> > > > OOPS!  I meant
> > > > For an input of LAN_IP=192.168.99.1/24
> > > > ipaddress.IPv4Interface(LAN_IP).network
> > > > returns 192.168.99.0/24
> > > 
> > > In the body of your post you had 192.168.1.128/25, so I mistook 
> > > 192.168.1.129/25 as a typo. However, once you have
> > > 
> > > >>> import ipaddress
> > > >>> ipaddress.ip_interface("192.168.99.1/24").network
> > > IPv4Network('192.168.99.0/24')
> > > 
> > > you can simply add the step from my first answer:
> > > 
> > > >>> ipaddress.ip_interface("192.168.99.1/24").network.network_address
> > > IPv4Address('192.168.99.0')
> > > 
> > > Unless I'm misunderstanding again...
> > 
> > That seems to be spot on.  I will try that.  I used the /25 to illustrate that that the network may not end in zero.  I had asked this question in the Mako forum and I had a few answers to drop the last octet and add zero which was not correct.  Thanks for your help!
> 
> SWEET!  That worked.  Thanks Peter!

In case anyone needs this as a reference:
>>> import ipaddress
>>> ipaddress.ip_interface("192.168.99.129/25").network
IPv4Network('192.168.99.128/25')
>>> ipaddress.ip_interface("192.168.99.129/25").network.network_address
IPv4Address('192.168.99.128')
>>> ipaddress.ip_interface("192.168.99.154/30").network.network_address
IPv4Address('192.168.99.152')



More information about the Python-list mailing list