import os
import socket
[docs]def checkPortIsOpen(ip, port):
"""Check if port is open in given address
Parameters
----------
ip : string
ip address
port : int
port number
Returns
------
outputs : bool
True if port is open else False
"""
print("Checking if port %s is open in ip %s" % (port, ip))
isOpen = False
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(3)
result = sock.connect_ex((ip, port))
if result == 0:
print("Port OPEN")
isOpen = True
else:
print("Port CLOSED")
sock.close()
return isOpen
except Exception as e:
print(str(e))
return False
[docs]def insert_packets(packets_per_second, src_ip, multicast_ip):
"""Starts iperf process in a different thread to inject packets
in the network
Parameters
----------
packets_per_second : int
Number of packet per seconds to be injected
src_ip : string
source ip address
multicast_ip : string
multicast ip address of the injected packets
"""
dirPath = os.path.dirname(os.path.realpath(__file__))
filePath = os.path.abspath(filePath)
iperf_path = dirPath + ".\\bin\\iperf.exe" # TODO: fix this
packet_length = 200
multicast_address = multicast_ip
ip_of_pc = src_ip
data_amount = "4M" #The data amount will be fixed, the variable to get the desired p/s will be the packet length
experimental_ratio = 0.79
time_seconds_to_inject = 100000000000 #practically infinite amount of time
packet_length = int((400000 / packets_per_second) / experimental_ratio)
print(packet_length)
final_command = iperf_path + " -u " + "-c " + multicast_address\
+ " -B " + str(ip_of_pc) + " -b " + str(data_amount)\
+ " -l " + str(packet_length) + " -t " + str(time_seconds_to_inject)
print(final_command)
#The structure of the command is: iperf.exe -u -c {mcast_address} -B {ip_pc} -b 4M -l {packet_size} -t {time in seconds}
os.system("start " + iperf_path + " -u " + "-c " + multicast_address + " -B " + str(ip_of_pc) + " -b " +
str(data_amount) + " -l " + str(packet_length) + " -t " + str(time_seconds_to_inject))
#data_amount / packet_length = 2600 (packets_per_second)
return
[docs]def stop_inserting():
"""Stops all iperf processes of injecting packets
"""
os.system("taskkill /F /IM iperf.exe")
return