2121from scapy .data import ARPHDR_ETHER , ARPHDR_LOOPBACK , ARPHDR_METRICOM , \
2222 DLT_ETHERNET_MPACKET , DLT_LINUX_IRDA , DLT_LINUX_SLL , DLT_LINUX_SLL2 , \
2323 DLT_LOOP , DLT_NULL , ETHER_ANY , ETHER_BROADCAST , ETHER_TYPES , ETH_P_ARP , ETH_P_MACSEC
24- from scapy .error import warning , ScapyNoDstMacException , log_runtime
24+ from scapy .error import (
25+ ScapyNoDstMacException ,
26+ log_runtime ,
27+ warning ,
28+ )
2529from scapy .fields import (
2630 BCDFloatField ,
2731 BitField ,
@@ -938,7 +942,7 @@ def _tups(ip, mac):
938942 # ip can be a Net/list/etc and will be iterated upon while sending
939943 return [(ip , "ff:ff:ff:ff:ff:ff" )]
940944 return [(x .query .pdst , x .answer .hwsrc )
941- for x in arping (ip , verbose = 0 )[0 ]]
945+ for x in arping (ip , verbose = 0 , iface = iface )[0 ]]
942946 elif isinstance (mac , list ):
943947 return [(ip , x ) for x in mac ]
944948 else :
@@ -960,6 +964,7 @@ def _tups(ip, mac):
960964 (x
961965 for ipa , maca in tup1
962966 for ipb , _ in tup2
967+ if ipb != ipa
963968 for x in
964969 Ether (dst = maca , src = target_mac ) /
965970 ARP (op = "who-has" , psrc = ipb , pdst = ipa ,
@@ -968,6 +973,7 @@ def _tups(ip, mac):
968973 (x
969974 for ipb , macb in tup2
970975 for ipa , _ in tup1
976+ if ipb != ipa
971977 for x in
972978 Ether (dst = macb , src = target_mac ) /
973979 ARP (op = "who-has" , psrc = ipa , pdst = ipb ,
@@ -987,6 +993,7 @@ def _tups(ip, mac):
987993 (x
988994 for ipa , maca in tup1
989995 for ipb , macb in tup2
996+ if ipb != ipa
990997 for x in
991998 Ether (dst = "ff:ff:ff:ff:ff:ff" , src = macb ) /
992999 ARP (op = "who-has" , psrc = ipb , pdst = ipa ,
@@ -995,6 +1002,7 @@ def _tups(ip, mac):
9951002 (x
9961003 for ipb , macb in tup2
9971004 for ipa , maca in tup1
1005+ if ipb != ipa
9981006 for x in
9991007 Ether (dst = "ff:ff:ff:ff:ff:ff" , src = maca ) /
10001008 ARP (op = "who-has" , psrc = ipa , pdst = ipb ,
@@ -1055,19 +1063,33 @@ def arping(net: str,
10551063 hwaddr = None
10561064 if "iface" in kargs :
10571065 hwaddr = get_if_hwaddr (kargs ["iface" ])
1058- r = conf .route .route (str (net ), verbose = False )
1066+ if isinstance (net , list ):
1067+ hint = net [0 ]
1068+ else :
1069+ hint = str (net )
1070+ psrc = conf .route .route (hint , verbose = False )[1 ]
1071+ if psrc == "0.0.0.0" :
1072+ if "iface" in kargs :
1073+ psrc = get_if_addr (kargs ["iface" ])
1074+ else :
1075+ warning (
1076+ "No route found for IPv4 destination %s. "
1077+ "Using conf.iface. Please provide an 'iface' !" % hint )
1078+ psrc = get_if_addr (conf .iface )
1079+ hwaddr = get_if_hwaddr (conf .iface )
1080+ kargs ["iface" ] = conf .iface
10591081
10601082 ans , unans = srp (
10611083 Ether (dst = "ff:ff:ff:ff:ff:ff" , src = hwaddr ) / ARP (
10621084 pdst = net ,
1063- psrc = r [ 1 ] ,
1085+ psrc = psrc ,
10641086 hwsrc = hwaddr
10651087 ),
10661088 verbose = verbose ,
10671089 filter = "arp and arp[7] = 2" ,
10681090 timeout = timeout ,
10691091 threaded = threaded ,
1070- iface_hint = net ,
1092+ iface_hint = hint ,
10711093 ** kargs ,
10721094 )
10731095 ans = ARPingResult (ans .res )
0 commit comments