당신이 잘 지내기를 바랍니다.MAC 주소 (IP 주소 :
나를 도와 주거나 올바른 방향으로 나를 가리킬 수 있는지 궁금합니다. 저는 현재 네트워크 관리를 중심으로하는 프로젝트에 참여하고 있습니다. 가능한 한 심각한 시간 제약으로 인해 opensource 코드를 사용하고 있습니다. 내가 가지고있는 문제는 프로젝트의 일부가 네트워크에 연결된 모든 장치의 MAC 주소를 캡처 할 수 있어야한다는 것입니다.
저는 지난 4 년 동안 다른 분야의 소프트웨어 엔지니어링 분야에서 일하면서 네트워크 지향 프로그래밍에 대한 지식이 제한적입니다. 내가 취한 접근법은 nmap을 기반으로 ip 주소와 필요한 정보를 얻는 것입니다. MAC 주소는 nmap out에 포함되지 않았고, 읽었던 것에서는 조금 흐릿 해 보인다. (내가 틀렸을 수도있다).
따라서 2 단계 접근 방식으로이 작업을 시도했습니다. 우선 nmap에서 IP 주소를 포함한 데이터를 얻습니다. 내 다음 단계와 내가 어려움을 겪고있는 비트는 작동하는 IP 주소 (내 파이썬 프로그램 내에서)를 ping하는 것입니다. 하지만 IP 주소에서 MAC 주소를 얻으려면 어떻게해야합니까? 처음에는 IP에 핑 (ping)을하고 ARP에서 MAC을 가져 왔지만 IP 주소가 동일한 서브넷에있는 경우에만 작동한다고 생각합니다. 배포시 문제를 복합화하려면 네트워크에 기록해야하는 최대 5000 대의 컴퓨터가있을 수 있습니다. 내 파이썬 핑 접근법을 보여주기 위해 코드는 다음과 같습니다 :
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)
제가 찾은 것은 다소 모호한 것 같습니다. 당신이 나를 도울 수있는 아이디어 나 연구의 길을 알고 있다면 나는 greatful 것
건배
크리스
틀린 것으로 입증되고 싶지만 다른 서브넷에서 MAC 주소를 얻을 수 있을지는 의문입니다. – NPE
위 코드를 실행했지만 오류가 발생했습니다 ... 'Traceback (최근 호출 마지막) : 파일 "Get_MacAddress_from_ip.py", 줄 26, mac = re.search (r "([a-fA-F0 -9] {2} [: | \ -]?) {6} ", s).groups() [0] AttributeError : 'NoneType'객체에 'groups'속성이 없습니다. ' –
Fahadkalis