Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: is_listening.py
- # Version: 1.00
- # Author: Jeoi Reqi
- """
- This script retrieves information about listening ports and their associated processes on a local system.
- It utilizes the psutil library to gather network connection data and process information.
- Functions:
- - get_listening_ports(): Retrieves information about listening ports and their associated processes.
- Features:
- - Retrieves listening ports and their associated process information (PID, user, command, start time, memory usage, CPU usage).
- - Handles cases where process information cannot be retrieved due to permissions or non-existent processes.
- - Groups the retrieved data with a space between each group for better readability.
- - Prompts the user whether to save the data to a file named 'listening_ports.txt'.
- - Provides error handling for file writing operations.
- Requirements:
- - Python 3.x
- - psutil library (install via pip: pip install psutil)
- Usage:
- 1. Ensure Python 3.x is installed on your system.
- 2. Install the psutil library by running: pip install psutil
- 3. Save the script as 'is_listening.py'.
- 4. Execute the script using the command: python is_listening.py
- 5. Follow the prompts to view the listening ports and optionally save the data to a file.
- Notes:
- - This script retrieves information about listening ports and associated processes on the local system only.
- - It may require elevated privileges to gather process information depending on the system's security settings.
- - The 'listening_ports.txt' file will be created in the same directory as the script.
- - Ensure to review the contents of the 'listening_ports.txt' file, as it may contain sensitive information.
- """
- import psutil
- # Define a function to get the list of listening ports with additional process information
- def get_listening_ports():
- # Get all connections
- connections = psutil.net_connections(kind='inet')
- listening_ports = []
- grouped_data = []
- # Iterate over the connections
- for conn in connections:
- # Check if the connection is listening
- if conn.status == 'LISTEN':
- # Extract port number and process ID (PID)
- port = conn.laddr.port
- pid = conn.pid
- # Fetch process information
- try:
- process = psutil.Process(pid)
- user = process.username()
- command = process.cmdline()
- start_time = process.create_time()
- memory_percent = process.memory_percent()
- cpu_percent = process.cpu_percent()
- # Append the collected information to the list
- listening_ports.append(
- f"Port: {port}\nPID: {pid}\nUser: {user}\nCommand: {command}\nStart Time: {start_time}\nMemory Usage: {memory_percent}%\nCPU Usage: {cpu_percent}%\n")
- except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
- # If process information cannot be retrieved, append N/A
- listening_ports.append(
- f"Port: {port}\nPID: {pid}\nUser: N/A\nCommand: N/A\nStart Time: N/A\nMemory Usage: N/A\nCPU Usage: N/A\n")
- # Group the data with a space between each group
- for i in range(0, len(listening_ports), 2):
- grouped_data.append('\n'.join(listening_ports[i:i+2]))
- # Print the grouped data
- print('\n\n'.join(grouped_data))
- # Prompt the user whether to save the data to a file
- save_to_file = input("\nDo you want to save the data to 'listening_ports.txt'?\nOPTIONS:\n1: YES\n2: NO\nMake Your Selection (1 or 2): ").strip().lower()
- if save_to_file == '1':
- try:
- with open('listening_ports.txt', 'w') as file:
- file.write('\n\n'.join(grouped_data))
- print("Data saved to 'listening_ports.txt'.")
- except Exception as e:
- print(f"Failed to save data: {e}")
- elif save_to_file == '2':
- print("Data not saved.")
- else:
- print("Invalid input. Data not saved.")
- # Call the function to print the listening ports and prompt the user to save the data to a file
- get_listening_ports()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement