0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

IPアドレスからMACアドレスを取得する。MACアドレス一覧表を作成する。

Posted at

D:\Users\user\Videos ここにmactableというフォルダ作る
mactableの中にmac.py作る

mac.py  ↓

from scapy.all import ARP, Ether, srp
import ipaddress
import csv
from datetime import datetime

def scan_network(network):
    # 結果の辞書
    results = {}
    
    # 指定のネットワークの全IPを取得
    for ip in ipaddress.IPv4Network(network):
        # ARPリクエストパケットを作成
        arp_request = ARP(pdst=str(ip))
        ether = Ether(dst="ff:ff:ff:ff:ff:ff")
        packet = ether / arp_request
        
        # パケットを送信して応答を待つ
        result = srp(packet, timeout=1, verbose=0)[0]
        
        if result:
            # 応答があればMACアドレスを取得
            mac = result[0][1].hwsrc
            results[str(ip)] = mac
        else:
            # 応答がなければ"Notfound"
            results[str(ip)] = "Notfound"
    
    return results

# スキャン対象のネットワーク
network = "192.168.0.0/24"
mac_addresses = scan_network(network)

# 現在の日時を取得してファイル名を作成
current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
csv_filename = f"mac_scan_{current_time}.csv"

# CSVファイルに結果を書き出し
with open(csv_filename, 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(['IP Address', 'MAC Address'])  # ヘッダー行を書き込み
    for ip, mac in mac_addresses.items():
        writer.writerow([ip, mac])

print(f"結果は {csv_filename} に保存されました。")

ターミナル行く

d:
cd D:\Users\user\Videos\mactable

python -m venv myenv 初回のみ
myenv\Scripts\activate

pip install scapy
python 3 mac.py

csvに書き出されます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?