콘텐츠로 건너뛰기

(Python) IP 국가 조회 도구

  • 기준

방화벽 장비에서 IDS/IPS를 사용하면 IDS에 의해 수집된 IP가 무수히 많다. 수집된 IP 국가를 조회하고 싶으면 IPinfo.io, AbuseIPDB 사이트를 이용하는 방법이 있다.

만약 대량의 IP를 한 번에 조회하고 싶으면 IPinfo.io API를 이용하는 방법이 있다.

주의할 점은 IPinfo.io의 요청 한도는 등록되지 않은 사용자의 경우 하루 1,000건으로 제한되어 있다.

(Python) IP 국가 조회 도구 - Github

https://github.com/korea821/IP-Country-Matcher

IP 목록 txt 파일 준비

e.g.
147.45.112.222
147.45.112.222
5.181.190.248
24.45.184.30
24.45.184.30

requests 설치

pip install requests

(Python) IP 국가 조회 도구 코드

import requests

# IP 목록 파일 경로
input_file = "bad_ip.txt"
output_file = "bad_ip_with_country.txt"

# IP 조회 API URL
api_url = "https://ipinfo.io/{ip}/json"

# 결과를 저장할 리스트
results = []

# 파일에서 IP 읽기
with open(input_file, "r") as file:
    ip_addresses = file.readlines()

# IP 주소의 국가 정보 가져오기
for ip in ip_addresses:
    ip = ip.strip()  # 공백 제거
    if ip:
        try:
            response = requests.get(api_url.format(ip=ip))
            if response.status_code == 200:
                data = response.json()
                country = data.get("country", "Unknown")
                results.append(f"{ip} - {country}")
            else:
                results.append(f"{ip} - Error: {response.status_code}")
        except Exception as e:
            results.append(f"{ip} - Error: {str(e)}")

# 결과를 파일에 저장
with open(output_file, "w") as file:
    file.write("\n".join(results))

print(f"IP와 국가 매칭 결과가 {output_file}에 저장되었습니다.")

실행 결과 (bad_ip_with_country.txt)

147.45.112.222 - DE
147.45.112.222 - DE
5.181.190.248 - PL
24.45.184.30 - US
24.45.184.30 - US

Join the conversation

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다