How can i track the current network speed per ip on a WLAN ? I am working on project to create a WLAN with raspberry pi which will be running nodejs express framework to play videos in browser of smartphones connected to it but i need to know the network speed between my raspberry pie server and each client ip so that i could change the video quality according to network speed.
use iptables to create a new chain and attach it to the output chain of iptables.Create rule for each ip address in arp table and use iptables -L custom_chain_name -vnx to view data used and then calculate the speed by measuring data used after certain interval of time.Look the below python code it will do everything for you.
def reset_iptables():
call("iptables -t filter -D OUTPUT -j ltgauge",shell=True)
call("iptables --flush ltgauge",shell=True)
call("iptables -X ltgauge",shell=True)
#this function fetches devices information from arp_table
def fetch_arptable():
with open(r"/proc/net/arp","r") as arp_table:
arp_table_data=arp_table.readlines()
bucket=[]
size=len(arp_table_data)
for i in range(1,size):
bucket.append(arp_table_data[i].split())
devices=[]
for item in bucket:
device_detail={"IP address":item[0],"HW address":item[3],"network_device":item[5]}
devices.append(device_detail)
return devices
def getConnectedDeviceMacIP(interface):
active_devices=[]
for device in ARPTABLE:
if device["network_device"]==interface:
active_devices.append([device["HW address"],device["IP address"]])
return active_devices
def createIptablerule():
for item in macIP_list:
status=call("iptables -C ltgauge -d "+item[1]+" -j RETURN > /dev/null",shell=True)
if status==1:
call("iptables -A ltgauge -d "+item[1]+" -j RETURN > /dev/null",shell=True)
print "rule created \n"
def fetch_data():
info={}
for item in macIP_list:
command=check_output("iptables -L ltgauge -vnx | grep "+item[1],shell=True)
edata=[]
edata=command.split()
info[item[1]]=edata[1]
return info`
from subprocess import call,check_output
import datetime
from time import sleep
interface="wlp2s0"
final_db={}
data={}
reset_iptables()
call("iptables -t filter -N ltgauge",shell=True)
call("iptables -t filter -A OUTPUT -j ltgauge",shell=True)
while(True):
call("tput reset",shell=True)
ARPTABLE=fetch_arptable()
macIP_list=getConnectedDeviceMacIP(interface)
createIptablerule()
data=fetch_data()
check=datetime.datetime.now()
for item in macIP_list:
mac=item[0]
ip=item[1]
byte=int(data[ip])
if item[0] in final_db:
total_data = final_db[mac]["total"]+byte
final_db[mac]["total"] = total_data
last_update_time = final_db[mac]["updation_time"]
current_time = datetime.datetime.now()
time_gap = (current_time-final_db[mac]["updation_time"]).total_seconds()
speed = byte/time_gap
final_db[mac]["speed"] = speed
final_db[mac]["updation_time"] = current_time
else:
new_device={"total":byte,"updation_time":datetime.datetime.now(),"speed":0}
final_db[mac]=new_device
print "\tmac \t current_IP\t Total \t speed"
for item in macIP_list:
print item[0]+" "+item[1]+"\t"+str(final_db[item[0]]["total"])+" \t "+str(final_db[item[0]]["speed"])
sleep(1)