def _check_iperf_server(self, **kwargs): import time ip = kwargs['ServerIP'] self.clientobject = SSHConnection(IP=self.client, username=self.client_username, password=self.client_password, port=5001) self.clientobject.connect() self.cmd = 'iperf -c %s' % (ip) clientrun = 1 string = "/sec" Resp = "" ser_Resp = "" ## Checking for the Client and Server response for req-string. while string not in Resp and string not in ser_Resp: if clientrun: result = self.clientobject.execute_command(self.cmd) self.rc.execute_command( "\n") ## To update the server response. print result clientrun = 0 time.sleep(0.5) Resp = self.clientobject.rc.rc.before ## TBD:rc object is cascaded a bit more !! ser_Resp = self.rc.rc.rc.before self.rc.rc.rc.sendcontrol( 'c') ## Gently terminate the server connection del self.clientobject print Resp return result
def setUpClass(self): self.ovs_obj = ovs(IP=config.SWITCH_IP, username= config.SWITCH_USER, password= config.SWITCH_PASSWORD,root_password=config.SWITCH_ROOT_PSWD) if config.MININET: self.mn = SSHConnection(IP=config.SWITCH_IP, username= config.SWITCH_USER, password= config.SWITCH_PASSWORD) self.MN=self.mn.connect() self.mn.execute_command(cmd="sudo su", exp_out=":") self.mn.execute_command(cmd=config.SWITCH_ROOT_PSWD, exp_out="#")
def remote_ping(source_ip, source_user, source_password, destination_ip, echo_count=1): status = False ssh_obj = SSHConnection(IP=source_ip, username=source_user, password=source_password) conn_status = ssh_obj.connect() if conn_status: print destination_ip print("SSH connection success") cmd = "ping {0} -c{1}".format(destination_ip, echo_count) if ssh_obj.execute_command(cmd=cmd): exp_message = r'{0} packets transmitted, {1} received, 0% packet loss'.format(echo_count, echo_count) command_output = ssh_obj.resp #print command_output #print exp_message status = True if exp_message in command_output else False return status
def iperf_client(client_ip, username, password, server_ip, port): print("Connecting to Client: {0}".format(client_ip)) client_conn = SSHConnection(IP=client_ip, username=username, password=password) client_conn.connect() iperf_client_cmd = "iperf -c {0} -p {1}".format(server_ip, port) client_conn.execute_command(iperf_client_cmd) print("Traffic Sent from Client:{0} to Server:{1}".format(client_ip, server_ip)) client_conn.disconnect() return client_conn.resp
def monitor_server_traffic(server_ip, server_username, server_password, client_ip, client_username, client_password, port): print("Connecting to Server: {0}".format(server_ip)) server_conn = SSHConnection(IP=server_ip, username=server_username, password=server_password) server_conn.connect() # Run iperf server to listed on TCP port. Don't wait for command to complete execution iperf_svr_cmd = "iperf -s -p {0}".format(port) server_conn.execute_command(iperf_svr_cmd, wait=False) #server_conn.execute_command(iperf_svr_cmd) # Run iperf client command and pump traffic to server on TCP port transmitted_traffic = iperf_client(client_ip=client_ip, username=client_username, password=client_password, server_ip=server_ip, port=port) # Open a New server session to kill the iperf server running server_conn_2 = SSHConnection(IP=server_ip, username=server_username, password=server_password) server_conn_2.connect() server_conn_2.execute_command("lsof -i | grep 5001") expr = r'iperf \s*(\d*).' command_pid = re.search(expr, server_conn_2.resp).groups()[0].strip() print("Killing iperf server with PID :" + command_pid) server_conn_2.execute_command("kill -9 {0}".format(command_pid)) received_traffic = server_conn.rc.capture_output(wait=False) print("Server Output: \n{0}".format(received_traffic)) server_conn_2.disconnect() server_conn.disconnect() return transmitted_traffic, received_traffic
class test_OVS_FUNC(unittest.TestCase): @classmethod def setUpClass(self): self.ovs_obj = ovs(IP=config.SWITCH_IP, username= config.SWITCH_USER, password= config.SWITCH_PASSWORD,root_password=config.SWITCH_ROOT_PSWD) if config.MININET: self.mn = SSHConnection(IP=config.SWITCH_IP, username= config.SWITCH_USER, password= config.SWITCH_PASSWORD) self.MN=self.mn.connect() self.mn.execute_command(cmd="sudo su", exp_out=":") self.mn.execute_command(cmd=config.SWITCH_ROOT_PSWD, exp_out="#") unittest.skip("Test to be Skipped") def test_1_ovs_cntrlr_health_check(self): """ Check the requested bridge is configured and available in vsctl """ if config.MININET and self.MN: self.mn.execute_command("sudo mn -c") if self.mn.execute_command("sudo mn",exp_out="mininet>"): print "Bridge created with controller has created by MININET\n" return True else: print "Mininet is not able to prepare topology, chacl it...\n" return False if self.ovs_obj.create_validate_bridge(config.OVS_BRIDGE,[config.CONTROLLER_IP, 6653]): print "bridge %s and controller %s configured properly\n" %(config.OVS_BRIDGE, config.CONTROLLER_IP) return True else: print "Something wrong with OVS bridge & controller config\n" def test_2_ovs_ports_connectivity(self): """ Checks the topology availability i.e., links between hosts & OVS and OVS & controller :return: """ if config.MININET and self.MN: print "Ports association with bridge has done by MININET\n" return True if self.ovs_obj.addports_validate(config.OVS_BRIDGE, config.OVS_BRIDGE_PORTS): print "Ports %s are connected properly to the bridge %s\n" %(config.OVS_BRIDGE_PORTS, config.OVS_BRIDGE) return True else: print "Something wrong with logical connections with the ports %s and bridge %s\n"%(config.OVS_BRIDGE_PORTS, config.OVS_BRIDGE) return False def test_3_port_based_flows_test(self): """ This method is to test the OVS functionality in presence of traffic :return: """ self.ovs_obj.manage_flows(manage_type="delete", br_name=config.OVS_BRIDGE, inputs=None) flow_input = {OvsConf.flow_inputs['Pri']: OvsConf.priority[0], OvsConf.flow_inputs['iPort']: OvsConf.ports[0]} action = {OvsConf.flow_actions[0]: [OvsConf.ports[1]]} self.ovs_obj.manage_flows(manage_type="add", br_name=config.OVS_BRIDGE, inputs=flow_input, action=action) flow_input = {OvsConf.flow_inputs['Pri']: OvsConf.priority[0], OvsConf.flow_inputs['iPort']: OvsConf.ports[1]} action = {OvsConf.flow_actions[0]: [OvsConf.ports[0]]} self.ovs_obj.manage_flows(manage_type="add", br_name=config.OVS_BRIDGE, inputs=flow_input, action=action) if config.MININET and self.MN: temp = self.mn.execute_command("h1 ping h2 -c 5",exp_out = "0% packet loss") if temp: print self.mn.resp else: print self.mn.resp self.ovs_obj.manage_flows(manage_type="delete", br_name=config.OVS_BRIDGE, inputs=flow_input) if config.MININET and self.MN: temp = self.mn.execute_command("h1 ping h2 -c 5",exp_out = "0% packet loss") if temp: print self.mn.resp return False else: print self.mn.resp return True else: status = remote_ping(config.HOST1_IP,config.HOST1_USER,config.HOST1_PASSWORD,config.h2_port1,echo_count=10) if status: print "Ping between hosts H1 & H2 through OVS is Successfull\n" self.ovs_obj.manage_flows(manage_type="delete", br_name=config.OVS_BRIDGE, inputs=flow_input) status = remote_ping(config.HOST1_IP, config.HOST1_USER, config.HOST1_PASSWORD, config.h2_port1, echo_count=10) if not status: print "Ping between hosts H1 & H2 through OVS failed as flows are deleted\n" self.ovs_obj.manage_flows(manage_type="add", br_name=config.OVS_BRIDGE, inputs=flow_input, action=action) action = {OvsConf.flow_actions[2]: None} self.ovs_obj.manage_flows(manage_type="alter", br_name=config.OVS_BRIDGE, inputs=flow_input, action=action) #flow_input = {OvsConf.flow_inputs['Pri']:OvsConf.priority[0],OvsConf.flow_inputs['iPort']:OvsConf.ports[0],OvsConf.flow_inputs['Protocol']:"arp"} #action = {OvsConf.flow_actions[0]:[OvsConf.ports[1]]} #self.ovs_obj.manage_flows(manage_type="add",br_name=config.OVS_BRIDGE, inputs = flow_input, action = action) #action = {OvsConf.flow_actions[2]: None} #self.ovs_obj.manage_flows(manage_type="alter", br_name=config.OVS_BRIDGE, inputs=flow_input,action=action) #action = {OvsConf.flow_actions[0]:[OvsConf.ports[1],4]} #self.ovs_obj.manage_flows(manage_type="alter", br_name=config.OVS_BRIDGE, inputs=flow_input, action=action) #self.ovs_obj.manage_flows(manage_type="delete", br_name=config.OVS_BRIDGE, inputs=flow_input) """
def __init__(self, **kwargs): self.rc = SSHConnection(IP=kwargs["IP"], username=kwargs["username"], password=kwargs["password"]) self.rc.connect()
class ovs(object): _OVS_OFCTL_COMMAND = "ovs-ofctl" def __init__(self, **kwargs): self.rc = SSHConnection(IP=kwargs["IP"], username=kwargs["username"], password=kwargs["password"]) self.rc.connect() def addflows(self, **kwargs): self.parameters = "add-flow " self.interface, self.priority, self.protocol, self.protocol_type, self.input_port, self.action = ( kwargs["interface"], kwargs["priority"], kwargs['protocol'], kwargs["protocolType"], kwargs["input_port"], kwargs["action"]) if self.interface: self.parameters += "%s " % self.interface if self.priority: self.parameters += "priority=%d" % self.priority if self.protocol: self.parameters += ",%s" % self.protocol if self.protocol_type: self.parameters += " %s " % self.protocol_type if self.input_port: self.parameters += ",in_port=%d" % self.input_port if self.action: self.parameters += ",actions=%s" % self.action self.CMD = "%s %s" % (self._OVS_OFCTL_COMMAND, self.parameters) print("Flow Configured on %s : %s" % ("SWITCH", self.CMD)) return True if self.rc.execute_command(self.CMD) else False def dumpflows(self, **kwargs): self.parameters = " dump-flows " self.protocol, self.protocol_type, self.interface = ( kwargs["protocol"], kwargs["protocolType"], kwargs['interface']) if self.protocol: self.parameters += "-O %s " % self.protocol if self.interface: self.parameters += " %s " % self.interface if self.protocol_type: self.parameters += " %s " % self.protocol_type dlog(self.parameters) self.CMD = "sudo %s %s" % (self._OVS_OFCTL_COMMAND, self.parameters) #self.CMD = "%s %s" % (self._OVS_OFCTL_COMMAND, self.parameters) return True if self.rc.execute_command(self.CMD) else False # TBD: This needs to be moved to the system utils module def ifconfig(self, **kwargs): self.CMD = "ifconfig %s" self.parameters = " " if kwargs.get('InterfaceList') is True: self.parameters += "| grep Link| awk '{print $1}'" dlog(self.CMD % (self.parameters)) return [True, self.rc.resp] if self.rc.execute_command( self.CMD % (self.parameters)) else False def version(self): #self.CMD = "sudo %s %s"%(self._OVS_OFCTL_COMMAND, "--version") self.CMD = "%s %s" % (self._OVS_OFCTL_COMMAND, "--version") regexp = r'\(Open vSwitch\).(\d+\.\d+\.\d+)' status = self.rc.execute_command(self.CMD) #print(self.rc.resp) if status: match = re.search(regexp, self.rc.resp) if match: return match.groups()[0] def add_group_flows(self, flows_input): self.pattern = 'add-group -O OpenFlow13' bucket_frame = '' inputs = flows_input.copy() for key, value in inputs.items(): if key is 'bridge': self.pattern += " %s " % value elif key is 'group': self.pattern += "group_id=%s" % (value) elif key is 'type': self.pattern += ",%s=%s," % (key, value) elif key is 'buckets': for bucket in value: bucket = 'bucket=%s,' % bucket bucket_frame += bucket self.pattern += bucket_frame.rstrip(',') #print pattern self.CMD = "%s %s" % (self._OVS_OFCTL_COMMAND, self.pattern) print self.CMD print("Flow Configured on %s : %s" % ("SWITCH", self.CMD)) return True if self.rc.execute_command(self.CMD) else False def validate_flows(self, manage_type, br_name, input, ovs_protocol="-O OpenFlow13"): """ This method is to validate the flows created using ovs-ofctl dump-flows cli :return: True if it validates the flows properly else False """ search = "" if input.has_key("id"): del input["id"] if input.has_key("order"): del input["order"] self.CMD = self._OVS_OFCTL_COMMAND + " group-flows %s %s" % ( ovs_protocol, br_name) if self.rc.execute_command(self.CMD, "#"): group_flows = self.rc.resp print group_flows if len(input) == 0: search = None input_list = group_flows.splitlines() if len(input_list) == 1 and "OFPST_FLOW reply" in input_list[0]: search = None else: for key, value in input.items(): pattern = key + "=" + str(value) if key is "dl_type": if value is "2048": pattern = ',ip,' return_list = self.get_match_flow(pattern=pattern, input_list=input_list) if len(return_list) > 1: return_list = self.get_match_flow( pattern=pattern, input_list=return_list) else: search = None if len(return_list) == 0 else True input_list = return_list continue if manage_type is not "delete" and search is not None: return True elif manage_type is "delete" and search is None: return True def get_flows(self, **kwargs): self.parameters = " dump-flows " self.protocol, self.protocol_type, self.interface = ( kwargs["protocol"], kwargs["protocolType"], kwargs['interface']) if self.protocol: self.parameters += "-O %s " % self.protocol if self.interface: self.parameters += " %s " % self.interface if self.protocol_type: self.parameters += " %s " % self.protocol_type #dlog(self.parameters) self.CMD = "sudo %s %s" % (self._OVS_OFCTL_COMMAND, self.parameters) #self.CMD = "%s %s" % (self._OVS_OFCTL_COMMAND, self.parameters) flow_records = [] #print "\nself.CMD = ", self.CMD #print "\nself.rc.execute_command(self.CMD) = ", self.rc.execute_command(self.CMD) if self.rc.execute_command(self.CMD): #print "\nself.rc.resp = ", self.rc.resp for line in self.rc.resp.split("\r\n"): line = line.strip() if line.startswith("cookie"): flow_info = {} for flow_param in line.split(", "): param, value = flow_param.split("=", 1) flow_info[param] = value flow_records.append(flow_info) else: print "\nInvalid Command: ", self.CMD return flow_records def _check_iperf_server(self, **kwargs): import time ip = kwargs['ServerIP'] self.clientobject = SSHConnection(IP=self.client, username=self.client_username, password=self.client_password, port=5001) self.clientobject.connect() self.cmd = 'iperf -c %s' % (ip) clientrun = 1 string = "/sec" Resp = "" ser_Resp = "" ## Checking for the Client and Server response for req-string. while string not in Resp and string not in ser_Resp: if clientrun: result = self.clientobject.execute_command(self.cmd) self.rc.execute_command( "\n") ## To update the server response. print result clientrun = 0 time.sleep(0.5) Resp = self.clientobject.rc.rc.before ## TBD:rc object is cascaded a bit more !! ser_Resp = self.rc.rc.rc.before self.rc.rc.rc.sendcontrol( 'c') ## Gently terminate the server connection del self.clientobject print Resp return result def iperf_client(self, **kwargs): ##import socket import os self.serverIP = kwargs['serverip'] self.cmd = 'iperf %s' self.server_cmd = 'iperf -s' self.parameters = "" """ ##source = kwargs['source'] dlog('Checking for any iperf sessions running:') if os.system('killall -9 iperf') : dlog("Killing the existing instance") else: dlog("No instances found.") """ if kwargs.has_key('client'): self.client = kwargs['client'] if kwargs.has_key('client_username'): self.client_username = kwargs['client_username'] if kwargs.has_key('client_password'): self.client_password = kwargs['client_password'] if kwargs.has_key('timeout'): self.timeout = kwargs['timeout'] if kwargs.has_key('connectiontype'): self.connectiontype = kwargs['connectiontype'] self.parameters = "-u " ##self.parameters += " -c %s"%(self.client) self.rc.execute_command(self.server_cmd) ##self.rc.execute_command(self.cmd%(self.parameters)) return self._check_iperf_server(ServerIP=self.serverIP)