Skip to content

Commit a3859f1

Browse files
committed
Windows: code clean-up, fix iface repr, fix get_working_if()
1 parent 1fd1861 commit a3859f1

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

scapy/arch/windows/__init__.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,7 @@ def _update_pcapdata(self):
259259
raise PcapNameNotFoundError
260260

261261
def __repr__(self):
262-
return "<%s %s: %s %s %s description=%r>" % (
263-
self.__class__.__name__, self.name, self.guid, self.ip,
264-
self.mac, self.description,
265-
)
262+
return "<%s %s %s>" % (self.__class__.__name__, self.name, self.guid)
266263

267264
from UserDict import UserDict
268265

@@ -394,15 +391,15 @@ def read_routes_7():
394391
return routes
395392

396393
def read_routes():
397-
routes=[]
394+
routes = []
398395
release = platform.release()
399396
try:
400397
if release in ["post2008Server", "8"]:
401-
routes=read_routes_post2008()
398+
routes = read_routes_post2008()
402399
elif release == "XP":
403-
routes=read_routes_xp()
400+
routes = read_routes_xp()
404401
else:
405-
routes=read_routes_7()
402+
routes = read_routes_7()
406403
except Exception as e:
407404
log_loading.warning("Error building scapy routing table : %s"%str(e))
408405
else:
@@ -663,17 +660,12 @@ def sniff(count=0, store=1, offline=None, prn = None, lfilter=None, L2socket=Non
663660
scapy.sendrecv.sniff = sniff
664661

665662
def get_working_if():
666-
# XXX get the interface associated with default route instead
667663
try:
668-
if 'Ethernet' in IFACES and IFACES['Ethernet'].ip != '0.0.0.0':
669-
return 'Ethernet'
670-
elif 'Wi-Fi' in IFACES and IFACES['Wi-Fi'].ip != '0.0.0.0':
671-
return 'Wi-Fi'
672-
elif len(IFACES) > 0:
673-
return IFACES.itervalues().next()
674-
else:
675-
return LOOPBACK_NAME
676-
except:
664+
# return the interface associated with the route with smallest
665+
# mask (route by default if it exists)
666+
return min(read_routes(), key=lambda x: x[1])[2]
667+
except ValueError:
668+
# no route
677669
return LOOPBACK_NAME
678670

679671
conf.iface = get_working_if()

0 commit comments

Comments
 (0)