Overview
Use the iperf or iperf3 command to automatically and concurrently verify the network bandwidth between different hosts based on the IP address or IP range segment entered by the user.
Script of iperf tools
#!/usr/bin/python
import os
import sys
import time
import argparse
import ipaddress
import subprocess
"""
This is a tool for Support and QA
after configure network, before create cluster, we may need to test network bandwidth.
if password have changed after cluster created, use --nopass
"""
def sshpass_param(nopass=False):
if not nopass:
param = 'sshpass -p {}'.format('p@ssw0rd')
else:
param = ''
return param
def full_ssh_pass(nopass=False):
param = "{} ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=ERROR " \
.format(sshpass_param(nopass))
return param
def start_iperf_server(host, user="root", nopass=False, cmd='iperf'):
full_cmd = "{} {}@{} '{} -s'".format(full_ssh_pass(nopass), user, host, cmd)
with open(os.devnull, 'w') as devnull:
p = subprocess.Popen(full_cmd, stderr=devnull, stdout=devnull, shell=True)
return p
def stop_iperf_server(host, user="root", nopass=False, cmd='iperf'):
full_cmd = "{} {}@{} killall {} 2>/dev/null".format(full_ssh_pass(nopass), user, host, cmd)
p = subprocess.Popen(full_cmd, shell=True)
p.wait()
return p
def start_iperf_client(client, server, user="root", nopass=False, cmd='iperf'):
if cmd == 'iperf3':
full_cmd = "{} {}@{} iperf3 -c {} -P 10 -i 1 -t 3 |grep SUM |grep sender|awk '/sec/ {{print $6,$7}}'|tail -1" \
.format(full_ssh_pass(nopass), user, client, server)
else:
full_cmd = "{} {}@{} iperf -c {} -P 10 -i 1 -t 3 |grep SUM |awk '/sec/ {{print $7,$8}}'|tail -1" \
.format(full_ssh_pass(nopass), user, client, server)
p = subprocess.Popen(full_cmd, stdout=subprocess.PIPE, shell=True)
p.wait()
out, err = p.communicate()
return out
def start_ping(src_ip, dst_ip, count, user="root", nopass=False):
full_cmd = "{} {}@{} ping {} -c {} -R".format(full_ssh_pass(nopass), user, src_ip, dst_ip, count)
p = subprocess.Popen(full_cmd, shell=True)
p.wait()
return p
def normalize_ip_list(iplist):
result = []
for ip in iplist:
if '~' in ip:
current = ip.split('~')
start_ip = current[0]
comp = start_ip.split('.')
begin = int(comp[3])
end = int(current[1])
while begin <= end:
current = '.'.join([comp[0],comp[1], comp[2], str(begin)])
try:
network = ipaddress.IPv4Network(unicode(current))
result.append(current)
except Exception:
pass
begin += 1
else:
try:
network = ipaddress.ip_address(unicode(ip))
result.append(ip)
except Exception:
continue
return result
def get_ip_list():
parser = argparse.ArgumentParser(description='Iperf test each other')
parser.add_argument("iplist", nargs='+', help="Please specify the target IPs of nodes")
parser.add_argument("--nopass", dest="nopass",
action='store_true', default=False,
help="ssh without default password")
parser.add_argument("-p", "--ping", action='store_true', help="Ping test each other")
parser.add_argument("-c", "--count", default='3', help=" Stop after sending count ECHO_REQUEST packets")
args = parser.parse_args()
return normalize_ip_list(args.iplist), args.nopass, args.ping, args.count
def get_linux_distro():
info = platform.linux_distribution()
if 'Ubuntu' in info[0]:
return 'ubuntu'
elif 'CentOS' in info[0]:
return 'centos'
else:
raise RuntimeError('Unknown Distribution: {}'.format(info[0]))
def get_iperf_bin_by_distro():
distro = get_linux_distro()
if distro == "centos":
return 'iperf3'
return 'iperf'
if __name__ == "__main__":
ip_list, nopass, ping, count = get_ip_list()
if not ip_list:
sys.exit(1)
cmd = get_iperf_bin_by_distro()
for ip in ip_list:
stop_iperf_server(ip, nopass=nopass, cmd=cmd)
time.sleep(0.1)
if ping:
for sip in ip_list:
for cip in ip_list:
if cip != sip:
print "******** {} => {} ********".format(sip, cip)
start_ping(sip, cip, count)
print "\n\n"
sys.exit(0)
for sip in ip_list:
print '-------------{}--------------'.format(sip)
p = start_iperf_server(sip, nopass=nopass, cmd=cmd)
time.sleep(0.1)
for cip in ip_list:
if cip != sip:
bw = start_iperf_client(cip, sip, nopass=nopass, cmd=cmd)
print "{} => {}: {}".format(cip, sip, bw)
time.sleep(0.1)
p.terminate()
stop_iperf_server(sip, nopass=nopass, cmd=cmd)