ip a output screenshot我正在尝试创建一个扫描局域网的脚本,并使用Python获取所有机器的IP地址和mac地址。下面的脚本执行此操作,但是它会打印两次列表吗?如何实现这一目标,或者如何更改下面的脚本以打印列表一次(作为字典,其中ip地址是键,mac是值)?
from __future__ import absolute_import, division, print_function
import logging
import scapy.config
import scapy.layers.l2
import scapy.route
import socket
import math
import errno
logging.basicConfig(format='%(asctime)s %(levelname)-5s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=logging.DEBUG)
logger = logging.getLogger(__name__)
def long2net(arg):
if (arg <= 0 or arg >= 0xFFFFFFFF):
raise ValueError("illegal netmask value", hex(arg))
return 32 - int(round(math.log(0xFFFFFFFF - arg, 2)))
def to_CIDR_notation(bytes_network, bytes_netmask):
network = scapy.utils.ltoa(bytes_network)
netmask = long2net(bytes_netmask)
net = "%s/%s" % (network, netmask)
if netmask < 16:
logger.warn("%s is too big. skipping" % net)
return None
return net
def scan_and_print_neighbors(net, interface, timeout=1):
logger.info("arping %s on %s" % (net, interface))
try:
ans, unans = scapy.layers.l2.arping(net, iface=interface, timeout=timeout, verbose=True)
for s, r in ans.res:
line = r.sprintf("%Ether.src% %ARP.psrc%")
try:
hostname = socket.gethostbyaddr(r.psrc)
line += " " + hostname[0]
except socket.herror:
# failed to resolve
pass
logger.info(line)
except socket.error as e:
if e.errno == errno.EPERM: # Operation not permitted
logger.error("%s. Did you run as root?", e.strerror)
else:
raise
if __name__ == "__main__":
for network, netmask, _, interface, address in scapy.config.conf.route.routes:
# skip loopback network and default gw
if network == 0 or interface == 'lo' or address == '127.0.0.1' or address == '0.0.0.0':
continue
if netmask <= 0 or netmask == 0xFFFFFFFF:
continue
net = to_CIDR_notation(network, netmask)
if interface != scapy.config.conf.iface:
# see http://trac.secdev.org/scapy/ticket/537
logger.warn("skipping %s because scapy currently doesn't support arping on non-primary network interfaces", net)
continue
if net:
scan_and_print_neighbors(net, interface)
答案 0 :(得分:1)
您是否尝试过将verbose
设为False
?
ans, unans = scapy.layers.l2.arping(net, iface=interface, timeout=timeout, verbose=False)
除了在verbose=False
中设置scapy.layers.l2.arping(
之外,
导入conf
模块:
from scapy.all import conf
在conf.verb=0
行下面添加if __name__ == "__main__":
:
if __name__ == "__main__":
conf.verb=0
for network, netmask, _, interface, address in scapy.config.conf.route.routes:
# skip loopback network and default gw
if network == 0 or interface == 'lo' or address == '127.0.0.1' or address == '0.0.0.0':
continue
编辑:我认为您的脚本为/proc/net/route
中定义的尚未明确过滤掉的每条“路线”循环一次(即{{1我的猜测是,如果你在哪里执行continue
,你可能会在那里找到2条路线,它们在某种程度上具有相同的网络和接口值,但其他东西与网络掩码或网关不同。
无论如何,通过这种方法来解决这个问题的方法是在调用route -n
之后添加break
以退出for循环。
例如:
scan_and_print_neighbors(net, interface)