Skip to content

Commit 6a4d033

Browse files
committed
Finished Mac OS X porting
1 parent ff01286 commit 6a4d033

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

netcmd.py

+31-17
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ def find_alive_hosts( self ):
8787

8888
# broadcast arping ftw
8989
ans,unans = srp( Ether( dst = "ff:ff:ff:ff:ff:ff" ) / ARP( pdst = self.network ),
90-
verbose = False,
91-
filter = "arp and arp[7] = 2",
92-
timeout = 2,
90+
verbose = False,
91+
filter = "arp and arp[7] = 2",
92+
timeout = 2,
9393
iface_hint = self.network )
9494

9595
for snd,rcv in ans:
@@ -120,21 +120,32 @@ def __init__( self, interface, gateway = None, network = None, kill = False, all
120120
raise Exception( "Only root can run this script." )
121121

122122
self.__preload_mac_table()
123-
124-
print "@ Searching for the network gateway address ..."
125123

124+
print "@ Searching for the network gateway address ..."
125+
126126
# for route in conf.route.routes:
127127
for net, msk, gw, iface, addr in conf.route.routes:
128128
# found a route for given interface
129129
if iface == self.interface:
130130
network = ltoa( net )
131-
# compute network representation if not yet done
132-
if network.split('.')[0] == addr.split('.')[0]:
133-
bits = self.__bit_count( msk )
134-
self.network = "%s/%d" % ( network, bits )
135-
# search for a valid network gateway
136-
if self.gateway is None and gw != '0.0.0.0':
137-
self.gateway = gw
131+
132+
# Mac OS X
133+
if sys.platform == 'darwin':
134+
if self.gateway is None:
135+
if gw != '0.0.0.0':
136+
self.gateway = gw
137+
elif self.network is None and network != '0.0.0.0':
138+
bits = self.__bit_count( msk )
139+
self.network = "%s/%d" % ( network, bits )
140+
# Linux
141+
else:
142+
# compute network representation if not yet done
143+
if network.split('.')[0] == addr.split('.')[0]:
144+
bits = self.__bit_count( msk )
145+
self.network = "%s/%d" % ( network, bits )
146+
# search for a valid network gateway
147+
if self.gateway is None and gw != '0.0.0.0':
148+
self.gateway = gw
138149

139150
if self.gateway is not None and self.network is not None:
140151
print "@ Gateway is %s on network %s ." % ( self.gateway, self.network )
@@ -150,12 +161,13 @@ def __init__( self, interface, gateway = None, network = None, kill = False, all
150161
self.targets = self.endpoints
151162
else:
152163
while choice is None:
153-
for i, item in enumerate( self.endpoints ):
154-
( mac, ip ) = item
155-
vendor = self.__find_mac_vendor( mac )
156-
print " [%d] %s %s %s" % ( i, mac, ip, "( %s )" % vendor if vendor else '' )
157-
choice = raw_input( "@ Choose [0-%d] (* to select all, r to refresh): " % (len(self.endpoints) - 1) )
158164
try:
165+
for i, item in enumerate( self.endpoints ):
166+
( mac, ip ) = item
167+
vendor = self.__find_mac_vendor( mac )
168+
print " [%d] %s %s %s" % ( i, mac, ip, "( %s )" % vendor if vendor else '' )
169+
170+
choice = raw_input( "@ Choose [0-%d] (* to select all, r to refresh): " % (len(self.endpoints) - 1) )
159171
choice = choice.strip()
160172
if choice == '*':
161173
self.targets = self.endpoints
@@ -164,6 +176,8 @@ def __init__( self, interface, gateway = None, network = None, kill = False, all
164176
self.find_alive_hosts()
165177
else:
166178
self.targets.append( self.endpoints[ int(choice) ] )
179+
except KeyboardInterrupt:
180+
quit()
167181
except Exception as e:
168182
print "@ Invalid choice!"
169183
choice = None

0 commit comments

Comments
 (0)