I have a problem with my python code that is about a Netflow collector agent. I run this code in virtual machine and must collects data from netflow agent that is a router which emulate in Gns3. My topology is correct and my devices can ping each other. But this application doesn't work.
from netflow import collector
import time
# Define the port to listen on for NetFlow data records
port = 2055
host = '192.168.10.2'
# Define the default time interval (in seconds)
default_time_interval = 1800 # 30 minutes
# Get the time interval from the user (in seconds)
time_interval = input("Enter the time interval (in seconds), or press Enter to use the default value of 30 minutes: ")
# Use the default time interval if the user does not enter a value
if time_interval == '':
time_interval = default_time_interval
else:
time_interval = int(time_interval)
# Create a FlowCollector object to listen for NetFlow data records
collector = collector.get_export_packets(host,port)
# Initialize the total bytes received and sent to 0
total_bytes_received = 0
total_bytes_sent = 0
# Initialize a dictionary to store the IP addresses and their corresponding byte counts
ip_bytes_dict = {}
# Initialize the start time for the current time interval
start_time = time.time()
while True:
# Check if the current time interval has elapsed
if time.time() - start_time > time_interval:
# Display the total bytes received and sent for the previous time interval
print(f'Total bytes received: {total_bytes_received}, Total bytes sent: {total_bytes_sent}')
# Find the IP addresses that have received traffic in the previous time interval
ip_list = [ip for ip, bytes in ip_bytes_dict.items() if bytes > 0]
# Display the IP addresses that have received traffic in the previous time interval
print(f'IP addresses that have received traffic: {ip_list}')
# Reset the total bytes received and sent to 0
total_bytes_received = 0
total_bytes_sent = 0
# Reset the byte counts for each IP address in the dictionary to 0
for ip in ip_bytes_dict:
ip_bytes_dict[ip] = 0
# Set the start time for the next time interval
start_time = time.time()
# Receive NetFlow data records from the agent
for data_record in collector:
# Extract the source and destination IP addresses and bytes transferred
src_ip = data_record.src_ip
dst_ip = data_record.dst_ip
bytes = data_record.bytes
# Check if the source IP address is within the local network
if src_ip.startswith('192.168.10.'):
# This is incoming traffic
total_bytes_received += bytes
# Update the byte count for the source IP address in the dictionary
if src_ip in ip_bytes_dict:
ip_bytes_dict[src_ip] += bytes
else: ip_bytes_dict[src_ip] = bytes
# Check if the destination IP address is within the local network
if dst_ip.startswith('192.168.10.'):
# This is outgoing traffic
total_bytes_sent += bytes
# Update the byte count for the destination IP address in the dictionary
if dst_ip in ip_bytes_dict:
ip_bytes_dict[dst_ip] += bytes
else:
ip_bytes_dict[dst_ip] = bytes
# Prompt the user to select an IP address
selected_ip = input("Enter the IP address to view its traffic, or press Enter to continue: ")
# Check if the user entered a valid IP address that received traffic in the previous time interval
if selected_ip in ip_bytes_dict and ip_bytes_dict[selected_ip] > 0:
# Calculate the amount of traffic received and sent to the selected IP address in the previous time interval
received_bytes = 0
sent_bytes = 0
for ip, bytes in ip_bytes_dict.items():
if ip == selected_ip:
received_bytes += bytes
elif ip.startswith('192.168.10.'):
sent_bytes += bytes
# Display the amount of traffic received and sent to the selected IP address
print(f'Traffic received from {selected_ip}: {received_bytes} bytes')
print(f'Traffic sent to {selected_ip}: {sent_bytes} bytes')
else:
# If the selected IP address did not receive traffic in the previous time interval or is not in the dictionary, display an error message
print(f'Error: {selected_ip} did not receive traffic in the previous time interval.')
I get these errors:
`Traceback (most recent call last):
File "C:\Users\98915\Desktop\netflow collector.py.txt", line 57, in <module>
for data_record in collector:
File "C:\Users\98915\AppData\Local\Programs\Python\Python38\lib\site-packages\netflow\collector.py", line 182, in get_export_packets
listener = ThreadedNetFlowListener(host, port)
File "C:\Users\98915\AppData\Local\Programs\Python\Python38\lib\site-packages\netflow\collector.py", line 95, in __init__
self.server = QueuingUDPListener((host, port), self.input)
File "C:\Users\98915\AppData\Local\Programs\Python\Python38\lib\site-packages\netflow\collector.py", line 60, in __init__
super().__init__(interface, QueuingRequestHandler)
File "C:\Users\98915\AppData\Local\Programs\Python\Python38\lib\socketserver.py", line 452, in __init__
self.server_bind()
File "C:\Users\98915\AppData\Local\Programs\Python\Python38\lib\socketserver.py", line 466, in server_bind
self.socket.bind(self.server_address)
OSError: [WinError 10049] The requested address is not valid in its context`
Thanks.
I want to resolve the errors.