This Python script processes a log file containing IP addresses, retrieves detailed information about each IP address, and logs the information in a formatted manner to an output file. It continuously monitors the log file for new IP addresses and updates the output file accordingly.
- Extracts IP addresses from a log file.
- Fetches hostname and additional information (city, region, country, organization) for each IP address.
- Logs the detailed information in a human-readable format.
- Monitors the log file continuously and processes new IP addresses as they appear.
- Python 3.x
requestslibrary
- Clone the repository or download the script.
- Install the required Python library using pip:
pip install requests
- Ensure that you have a log file (
logs/ip_addresses.log) that the script can read from. - Run the script:
python ip_info.py
- The script will process the log file and append detailed information about each IP address to the output file (
logs/ip_info.log).
get_hostname(ip_address): Retrieves the hostname for a given IP address.get_ipinfo(ip_address): Fetches detailed information about the IP address from theipinfo.ioservice.get_ip_details(ip_address): Combines hostname and IP information into a dictionary.format_details(details): Formats the details dictionary into a human-readable string.is_valid_ip(ip): Validates if a given string is a valid IP address.process_threats_log(log_path, output_path): Main function that monitors the log file, processes new IP addresses, and writes the details to the output file.
The script continuously monitors the log file (logs/ip_addresses.log) every 60 seconds for new IP addresses. If the log file does not exist, it waits until the file is created. When new IP addresses are found, it retrieves and logs their details.
The output in the logs/ip_info.log file will look like this:
IP Address : 8.8.8.8 Hostname : dns.google City : Mountain View Region : California Country : US Organization: Google LLC
- To change the log file paths, modify the
log_pathandoutput_pathvariables in the__main__block. - The sleep duration between log checks can be adjusted by changing the value in
time.sleep(60)in theprocess_threats_logfunction.
This project is licensed under the MIT License. See the LICENSE file for details.
Feel free to contribute to this project by submitting issues or pull requests. Your feedback is highly appreciated!