私はあなたがすべて元気であることを願っています。
あなたが私を助けてくれるか、正しい方向に向けてくれるかどうか疑問に思っています. 私は現在、ネットワーク管理を中心としたプロジェクトに取り組んでいます。時間の制約が厳しいため、可能な限りオープンソース コードを使用しています。私が抱えている問題は、プロジェクトの一部で、ネットワークに接続されているすべてのデバイスの MAC アドレスを取得できる必要があることです。
過去 4 年間、ソフトウェア エンジニアリングの他の分野で働いていたため、ネットワーク指向プログラミングの知識は限られています。私が取ったアプローチは、必要な IP アドレスやその他の情報を取得するための基礎として nmap を使用することです。MAC アドレスは nmap の出力に含まれておらず、私が読んだところ、少し不安定なようです。(私は間違っている可能性があります)。
したがって、これを 2 段階のアプローチで実行しようとしました。まず、正常に動作する nmap から IP アドレスを含むデータを取得します。私の次のステップと私が苦労しているビットは、機能するIPアドレスを(私のpythonプログラム内から)pingすることです。しかし、どうすれば IP アドレスから MAC アドレスを取得できますか? 最初は、IP に ping を実行して ARP から MAC を取得することを考えていましたが、IP アドレスが同じサブネット上にある場合にのみ機能すると思います。展開の問題を複雑にするために、ネットワーク上にログを記録する必要がある最大 5000 台のコンピューターが存在する可能性があります。私のpython pingアプローチを示すために、これはコードです:
import pdb, os
import subprocess
import re
from subprocess import Popen, PIPE
# This will only work within the netmask of the machine the program is running on cross router MACs will be lost
ip ="192.168.0.4"
#PING to place target into system's ARP cache
process = subprocess.Popen(["ping", "-c","4", ip], stdout=subprocess.PIPE)
process.wait()
result = process.stdout.read()
print(result)
#MAC address from IP
pid = Popen(["arp", "-n", ip], stdout=PIPE)
s = pid.communicate()[0]
# [a-fA-F0-9] = find any character A-F, upper and lower case, as well as any number
# [a-fA-F0-9]{2} = find that twice in a row
# [a-fA-F0-9]{2}[:|\-] = followed by either a ?:? or a ?-? character (the backslash escapes the hyphen, since the # hyphen itself is a valid metacharacter for that type of expression; this tells the regex to look for the hyphen character, and ignore its role as an operator in this piece of the expression)
# [a-fA-F0-9]{2}[:|\-]? = make that final ?:? or ?-? character optional; since the last pair of characters won't be followed by anything, and we want them to be included, too; that's a chunk of 2 or 3 characters, so far
# ([a-fA-F0-9]{2}[:|\-]?){6} = find this type of chunk 6 times in a row
mac = re.search(r"([a-fA-F0-9]{2}[:|\-]?){6}", s).groups()[0] #LINUX VERSION ARP
mac = re.search(r"(([a-f\d]{1,2}\:){5}[a-f\d]{1,2})", s).groups()[0] #MAC VERSION ARP
print(mac)
いくつかの情報を探しましたが、見つけたものは少し曖昧なようです。私に役立つ可能性のあるアイデアや研究手段を知っていれば、私はとても助かります
乾杯
クリス