@@ -83,15 +83,89 @@ def ip_address(value):
83
83
return ipaddress .ip_interface (value ).ip
84
84
85
85
86
- # XXX use this
87
86
def ip_interface (value ):
88
87
"""
89
- Returns ip_interface of passed value .
88
+ Returns ip_interface of passed IP interface .
90
89
"""
91
90
return ipaddress .ip_interface (value )
92
91
92
+ def ip_prefixlen (addr ):
93
+ """
94
+ Returns the prefix length for passed IP interface.
95
+ """
96
+ return ipaddress .ip_interface (addr ).prefixlen
97
+
98
+ def ip_netmask (addr ):
99
+ """
100
+ Returns a subnet mask for passed IP interface.
101
+ """
102
+ return ipaddress .ip_interface (addr ).netmask
103
+
104
+ def ip_hostmask (addr ):
105
+ """
106
+ Returns a wilcard or hostmask for passed IP interface.
107
+
108
+ Example:
109
+ ip_netmask('10.10.10.5/24')
110
+ > 0.0.0.255
111
+ """
112
+ return ipaddress .ip_interface (addr ).hostmask
113
+
114
+ def ip_network (addr ):
115
+ """
116
+ Returns a network address for a combination of IP address and subnet mask
117
+
118
+ Example:
119
+ ip_network('10.10.10.5/24')
120
+ > 10.10.10.0
121
+ """
122
+ return ipaddress .ip_network (addr ).network
123
+
124
+
125
+ def ip_broadcast (addr ):
126
+ """
127
+ Returns a broadcast address for a combination of IP address and subnet mask
128
+
129
+ Example:
130
+ ip_network('10.10.10.5/24')
131
+ > 10.10.10.255
132
+ """
133
+ return ipaddress .ip_network (addr ).broadcast
134
+
135
+ def ip_network_hosts_size (addr ):
136
+ """
137
+ Returns the size of the subnet for a combination of IP address and subnet mask
138
+
139
+ Example:
140
+ ip_network_hosts_size('10.10.10.5/24')
141
+ > 253
142
+ """
143
+ return ipaddress .ip_network (addr ).size
144
+
145
+
146
+ def ip_network_first (addr ):
147
+ """
148
+ Returns the first usable address in network address for a combination of IP address and subnet mask
149
+
150
+ Example:
151
+ ip_network('10.10.10.5/24')
152
+ > 10.10.10.1
153
+ """
154
+ net = ipaddress .ip_network (addr )
155
+ return ipaddress .ip_address (net [1 ]).__str__ ()
156
+
157
+
158
+ def ip_network_last (addr ):
159
+ """
160
+ Returns the last usable address in network address for a combination of IP address and subnet mask
161
+
162
+ Example:
163
+ ip_network('10.10.10.5/24')
164
+ > 10.10.10.254
165
+ """
166
+ return ipaddress .ip_address (ipaddress .ip_network (addr )[- 2 ]).__str__ ()
167
+
93
168
94
- # XXX when was interface added?
95
169
def ip_version (value ):
96
170
"""
97
171
Returns version of passed IP address.
0 commit comments