def packetCaptureGetCurrentPackets(self, getUpToPacketNumber=20, capturePacketsToFile=True): """ Description Packet Capturing in wireshark style details. By default, it saved 7209 packet counts. It takes a long time to save all the packet header details into a file. This API will default saving 20 packet counts. You could increase the packet count. Parameters getUpToPacketNumber: None|The last packet number to get. Always starts at 1. If you state 10, then this function will get 1-10 packets. capturePacketsToFile: True|False """ import subprocess, time if capturePacketsToFile: timestamp = int(time.time()) if self.enableDataPlane: packetCaptureFilenameData = 'packetCaptureForData_'+str(timestamp) subprocess.call(['touch', packetCaptureFilenameData]) if self.enableControlPlane: packetCaptureFilenameControl = 'packetCaptureForControl_'+str(timestamp) subprocess.call(['touch', packetCaptureFilenameControl]) if self.enableDataPlane == False and self.enableControlPlane == False: raise IxNetRestApiException('\nPacketCapture Error: You must enable one of the options: enableDataPlane|enableControlPlane') vport = self.portMgmtObj.getVports([self.captureRxPort])[0] response = self.ixnObj.get(self.ixnObj.httpHeader+vport+'/capture') totalDataCapturedPackets = response.json()['dataPacketCounter'] totalControlCapturedPackets = response.json()['controlPacketCounter'] if type(totalDataCapturedPackets) != int: totalDataCapturedPackets = 0 else: if getUpToPacketNumber != None: totalDataCapturedPackets = getUpToPacketNumber if type(totalControlCapturedPackets) != int: totalControlCapturedPackets = 0 else: if getUpToPacketNumber != None: totalControlCapturedPackets = getUpToPacketNumber for eachTypeOfCaptures, totalCapturedPackets in zip(('data', 'control'), (totalDataCapturedPackets, totalControlCapturedPackets)): self.ixnObj.logInfo('Getting captured packets for capture type: {0}'.format(eachTypeOfCaptures)) if capturePacketsToFile and int(totalCapturedPackets) != 0: timestamp = int(time.time()) if self.enableDataPlane: packetCaptureFilenameData = 'packetCaptureForData_'+str(timestamp) subprocess.call(['touch', packetCaptureFilenameData]) if self.enableControlPlane: packetCaptureFilenameControl = 'packetCaptureForControl_'+str(timestamp) subprocess.call(['touch', packetCaptureFilenameControl]) for packetIndex in range(1, int(totalCapturedPackets)): self.ixnObj.logInfo('Getting captured packet index number: {}/{}'.format(packetIndex, getUpToPacketNumber)) if self.enableDataPlane and eachTypeOfCaptures == 'data': data = {'arg1': vport+'/capture/currentPacket', 'arg2': packetIndex} response = self.ixnObj.post(self.ixnObj.sessionUrl+'/vport/capture/currentPacket/operations/getpacketfromdatacapture', data=data, silentMode=False) self.ixnObj.waitForComplete(response, self.ixnObj.sessionUrl+'/vport/capture/currentPacket/operations/getpacketfromdatacapture/'+response.json()['id']) if self.enableControlPlane and eachTypeOfCaptures == 'control': data = {'arg1': vport+'/capture/currentPacket', 'arg2': packetIndex} response = self.ixnObj.post(self.ixnObj.sessionUrl+'/vport/capture/currentPacket/operations/getpacketfromcontrolcapture', data=data, silentMode=False) self.ixnObj.waitForComplete(response, self.ixnObj.sessionUrl+'/vport/capture/currentPacket/operations/getpacketfromcontrolcapture/'+response.json()['id']) response = self.ixnObj.get(self.ixnObj.httpHeader+vport+'/capture/currentPacket/stack', silentMode=False) for eachStack in response.json(): displayName = eachStack['displayName'] stackIdObject = eachStack['links'][0]['href'] self.ixnObj.logInfo('\nStack: %s' % displayName) if capturePacketsToFile: if eachTypeOfCaptures == 'data': with open(packetCaptureFilenameData, 'a') as packetCaptureFile: packetCaptureFile.write('\nStack: %s\n' % displayName) if eachTypeOfCaptures == 'control': with open(packetCaptureFilenameControl, 'a') as packetCaptureFile: packetCaptureFile.write('\nStack: %s\n' % displayName) response = self.ixnObj.get(self.ixnObj.httpHeader+stackIdObject+'/field', silentMode=False) for eachField in response.json(): fieldId = eachField['id'] fieldName = eachField['displayName'] fieldValue = eachField['fieldValue'] self.ixnObj.logInfo('\t{0}: {1}: {2}'.format(fieldId, fieldName, fieldValue)) if capturePacketsToFile: if eachTypeOfCaptures == 'data': with open(packetCaptureFilenameData, 'a') as packetCaptureFile: packetCaptureFile.write('\t{0}: {1}: {2}\n'.format(fieldId, fieldName, fieldValue)) if eachTypeOfCaptures == 'control': with open(packetCaptureFilenameControl, 'a') as packetCaptureFile: packetCaptureFile.write('\t{0}: {1}: {2}\n'.format(fieldId, fieldName, fieldValue))