cowmesh-network-test/cowmesh_iperf_test.py

107 lines
2.7 KiB
Python

"""
script to do a basic holistic test of the cowmesh network,
testing internet connection speed using iperf,
between all nodes in the mesh
"""
import os
import re
import time
import asyncio, asyncssh, sys
import paramiko
import subprocess
import json
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
SECRETS_PATH = os.path.join(PROJECT_PATH, "secrets.json")
with open(SECRETS_PATH, 'r') as f:
SECRETS = json.loads(f.read())
nodes = [
"jaaga",
# "new-gazebo",
"redcottage",
"kotemanetp",
"guard"
]
host_to_ip = {
"jaaga": "10.56.121.19",
"redcottage": "10.56.58.194",
"new-gazebo": "10.56.113.2",
"guard": "10.56.121.73",
"kotemanetp": "10.56.40.113"
}
results = {}
def test_between_two_nodes(node_a, node_b):
print("running test from {} to {}".format(node_a, node_b))
u_name = 'root'
pswd = SECRETS["ROUTER_PASSWORD"]
myconn = paramiko.SSHClient()
myconn.set_missing_host_key_policy(paramiko.AutoAddPolicy())
session = myconn.connect(node_a, username =u_name, password=pswd)
ip = host_to_ip[node_b]
remote_cmd = 'iperf -c {ip} -p 5001'.format(ip=ip)
(stdin, stdout, stderr) = myconn.exec_command(remote_cmd)
output = str(stdout.read())
print("output: {}".format(output))
print("errors: {}".format(stderr.read()))
myconn.close()
match = re.search("(\S+) Mbits", output)
if match:
to_return = match.group(1)
else:
to_return = None
return to_return
async def start_iperf_servers():
for node in nodes:
print("starting iperf server on {}".format(node))
u_name = 'root'
pswd = SECRETS["ROUTER_PASSWORD"]
myconn = paramiko.SSHClient()
myconn.set_missing_host_key_policy(paramiko.AutoAddPolicy())
session = myconn.connect(node, username=u_name, password=pswd)
remote_cmd = 'iperf -s &'
(stdin, stdout, stderr) = myconn.exec_command(remote_cmd)
print("{}".format(stdout.read()))
print("{}".format(type(myconn)))
print("Options available to deal with the connectios are many like\n{}".format(dir(myconn)))
myconn.close()
async def run_test():
await start_iperf_servers()
for node_a in nodes:
for node_b in nodes:
if node_a == node_b:
print("skip self")
continue
r = test_between_two_nodes(node_a, node_b)
result_key = "{} -> {}".format(node_a, node_b)
results[result_key] = r
try:
asyncio.get_event_loop().run_until_complete(run_test())
print("** final results **")
for test_name, result in results.items():
print("{}: {} mbps".format(test_name, result))
except (OSError, asyncssh.Error) as exc:
sys.exit('SSH connection failed: ' + str(exc))