Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e969281

Browse files
Main Module
1 parent b9fe59d commit e969281

File tree

1 file changed

+55
-19
lines changed

1 file changed

+55
-19
lines changed

‎PySubnet/pysubet.py‎

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,41 @@
3131
}
3232

3333
def getnetworkaddr(address=None, netmask=None, auto=False): # Get the network address
34-
if auto and ( address or netmask ):
34+
if auto and ( address or netmask ):
3535
raise ValueError("too many arguments once once auto is set to True")
3636
elif auto:
3737
import sys
3838
if sys.platform=='win32':
3939

4040
import subprocess
41-
41+
4242
netmaski = subprocess.check_output(["ipconfig"]).decode()
43-
43+
4444
try:
4545
netmask = netmaski.split("Subnet Mask . . . . . . . . . . . : ")[1].split("\r\n")[0]
4646
address = netmaski.split("IPv4 Address. . . . . . . . . . . : ")[1].split("\r\n")[0]
4747
except IndexError:
4848
raise SystemError("Unable to identify the subnet mask and the local IPv4")
4949
elif sys.platform=="linux":
50-
pass
50+
51+
import subprocess
52+
53+
ip_out = subprocess.Popen(['ip', '-o', '-f', 'inet', 'addr', 'show'], stdout=subprocess.PIPE)
54+
split = subprocess.Popen(['awk', '/scope global/ {print 4ドル}'], stdin=ip_out.stdout, stdout=subprocess.PIPE)
55+
addresses, stderr = split.communicate()
56+
57+
if not addresses:
58+
raise SystemError("Unable to identify the subnet mask and the local IPv4")
59+
60+
(address, netmask) = addresses.decode().split("/")
61+
62+
netmask = [mask for mask, valuec in Mask.items() if valuec==int(netmask)][0]
5163
else:
5264
raise SystemError("Unsupported platform : %s" % sys.platform)
5365
elif not address or not netmask:
5466
raise TypeError("getrangeaddr() missing 2 positional arguments: 'address' and 'netmask'")
5567

56-
import ipaddress, socket
68+
import ipaddress, socket
5769

5870
try:
5971
socket.inet_aton(address)
@@ -67,38 +79,50 @@ def getnetworkaddr(address=None, netmask=None, auto=False): # Get the network ad
6779
raise TypeError("Not a private address %s " % address)
6880

6981
address = [(maskoct & addroct) for maskoct, addroct in zip(
70-
map(int , netmask.split(".")),
82+
map(int , netmask.split(".")),
7183
map(int , address.split("."))
7284
)
7385
]
74-
86+
7587
return ".".join(map(str, address))
7688

7789

7890
def getbroadcast(address=None, netmask=None, auto=False): # Get the broadcast address
79-
if auto and ( address or netmask ):
91+
if auto and ( address or netmask ):
8092
raise ValueError("too many arguments once once auto is set to True")
8193
elif auto:
8294
import sys
8395
if sys.platform=='win32':
8496

8597
import subprocess
86-
98+
8799
netmaski = subprocess.check_output(["ipconfig"]).decode()
88-
100+
89101
try:
90102
netmask = netmaski.split("Subnet Mask . . . . . . . . . . . : ")[1].split("\r\n")[0]
91103
address = netmaski.split("IPv4 Address. . . . . . . . . . . : ")[1].split("\r\n")[0]
92104
except IndexError:
93105
raise SystemError("Unable to identify the subnet mask and the local IPv4")
94106
elif sys.platform=="linux":
95-
pass
107+
108+
import subprocess
109+
110+
ip_out = subprocess.Popen(['ip', '-o', '-f', 'inet', 'addr', 'show'], stdout=subprocess.PIPE)
111+
split = subprocess.Popen(['awk', '/scope global/ {print 4ドル}'], stdin=ip_out.stdout, stdout=subprocess.PIPE)
112+
addresses, stderr = split.communicate()
113+
114+
if not addresses:
115+
raise SystemError("Unable to identify the subnet mask and the local IPv4")
116+
117+
(address, netmask) = addresses.decode().split("/")
118+
119+
netmask = [mask for mask, valuec in Mask.items() if valuec==int(netmask)][0]
96120
else:
97121
raise SystemError("Unsupported platform : %s" % sys.platform)
98122
elif not address or not netmask:
99123
raise TypeError("getbroadcast() missing 2 positional arguments: 'address' and 'netmask'")
100124

101-
import ipaddress, socket
125+
import ipaddress, socket
102126

103127
try:
104128
socket.inet_aton(address)
@@ -119,35 +143,47 @@ def getbroadcast(address=None, netmask=None, auto=False): # Get the broadcast ad
119143
int(octet[16:24],2),
120144
int(octet[24:] ,2)
121145
]
122-
146+
123147
return ".".join(map(str, octet))
124148

125149

126150

127151
def getrangeaddr(address=None, netmask=None, auto=False): # Get the hosts range in the current network
128-
if auto and ( address or netmask ):
152+
if auto and ( address or netmask ):
129153
raise ValueError("too many arguments once once auto is set to True")
130154
elif auto:
131155
import sys
132156
if sys.platform=='win32':
133157

134158
import subprocess
135-
159+
136160
netmaski = subprocess.check_output(["ipconfig"]).decode()
137-
161+
138162
try:
139163
netmask = netmaski.split("Subnet Mask . . . . . . . . . . . : ")[1].split("\r\n")[0]
140164
address = netmaski.split("IPv4 Address. . . . . . . . . . . : ")[1].split("\r\n")[0]
141165
except IndexError:
142166
raise SystemError("Unable to identify the subnet mask and the local IPv4")
143167
elif sys.platform=="linux":
144-
pass
168+
169+
import subprocess
170+
171+
ip_out = subprocess.Popen(['ip', '-o', '-f', 'inet', 'addr', 'show'], stdout=subprocess.PIPE)
172+
split = subprocess.Popen(['awk', '/scope global/ {print 4ドル}'], stdin=ip_out.stdout, stdout=subprocess.PIPE)
173+
addresses, stderr = split.communicate()
174+
175+
if not addresses:
176+
raise SystemError("Unable to identify the subnet mask and the local IPv4")
177+
178+
(address, netmask) = addresses.decode().split("/")
179+
180+
netmask = [mask for mask, valuec in Mask.items() if valuec==int(netmask)][0]
145181
else:
146182
raise SystemError("Unsupported platform : %s" % sys.platform)
147183
elif not address or not netmask:
148184
raise TypeError("getrangeaddr() missing 2 positional arguments: 'address' and 'netmask'")
149185

150-
import ipaddress, socket
186+
import ipaddress, socket
151187

152188
try:
153189
socket.inet_aton(address)
@@ -169,7 +205,7 @@ def getrangeaddr(address=None, netmask=None, auto=False): # Get the hosts range
169205
int(octet[24:] ,2)-1
170206
]
171207
address = [(maskoct & addroct) for maskoct, addroct in zip( # Extracting the network address
172-
map(int , netmask.split(".")),
208+
map(int , netmask.split(".")),
173209
map(int , address.split("."))
174210
)
175211
]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /