netsec-lab/ex3/rep-19.py

43 lines
2.1 KiB
Python

import pandas as pd
'''
IMPORTANT: Remove leading spaces from columns in csv file or pandas won't read
it correctly.
'''
df_proto = pd.read_csv('csv/team13_protocol.csv', index_col=0).fillna(0)
df_all = pd.read_csv('csv/team13_monthly.csv', index_col=0).fillna(0)
# Calculate percentage of packets with 1 (ICMP), 6 (TCP) or 17 (UDP) protocols
packets_percentage_proto = ((df_proto['6 · # Packets'].sum() +
df_proto['17 · # Packets'].sum() +
df_proto['1 · # Packets'].sum()) /
df_all['#packets'].sum()) * 100
# Calculate ratio of packets from all other protocols
packets_percentage_others = 100 - packets_percentage_proto
print('Percentage of packets in protocols not belonging to ICMP, TCP, UDP: ',
round(packets_percentage_others, 2))
# Calculate percentage of unique source IPs with 1 (ICMP), 6 (TCP) or 17 (UDP) protocols
uIPs_percentage_proto = ((df_proto['6 · # Unique Source IPs'].sum() +
df_proto['17 · # Unique Source IPs'].sum() +
df_proto['1 · # Unique Source IPs'].sum()) /
df_all['#unique_IP_sources'].sum()) * 100
# Calculate ratio of unique source IPs from all other protocols
uIPs_percentage_others = 100 - uIPs_percentage_proto
print('Percentage of unique source IPs in protocols not belonging to ICMP, TCP, UDP: ',
round(uIPs_percentage_others, 1))
# Calculate percentage of unique destination IPs with 1 (ICMP), 6 (TCP) or 17 (UDP) protocols
uIPd_percentage_proto = ((df_proto['6 · # Unique Destination IPs'].sum() +
df_proto['17 · # Unique Destination IPs'].sum() +
df_proto['1 · # Unique Destination IPs'].sum()) /
df_all['#unique_IP_destinations'].sum()) * 100
# Calculate ratio of unique destination IPs from all other protocols
uIPd_percentage_others = 100 - uIPd_percentage_proto
print('Percentage of unique destination IPs in protocols not belonging to ICMP, TCP, UDP: ',
round(uIPd_percentage_others, 1))