|
| 1 | +# import modules |
| 2 | +import struct |
| 3 | +import time |
| 4 | +import binascii |
| 5 | +import sys |
| 6 | +import os |
| 7 | +import json |
| 8 | +import requests |
| 9 | +import ipaddress |
| 10 | +import logging |
| 11 | +import threading |
| 12 | +import time |
| 13 | +from requests.exceptions import HTTPError |
| 14 | + |
| 15 | +# Global Header Values |
| 16 | +PCAP_MAGICAL_NUMBER = 0 |
| 17 | +PCAP_MJ_VERN_NUMBER = 0 |
| 18 | +PCAP_MI_VERN_NUMBER = 0 |
| 19 | +PCAP_LOCAL_CORECTIN = 0 |
| 20 | +PCAP_ACCUR_TIMSTAMP = 0 |
| 21 | +PCAP_MAX_LENGTH_CAP = 0 |
| 22 | +PCAP_DATA_LINK_TYPE = 0 |
| 23 | +service_name = '' |
| 24 | +service_num = 0 |
| 25 | +Nservices = 1 |
| 26 | +f = 0 |
| 27 | +stop = False |
| 28 | + |
| 29 | + |
| 30 | +def connect(address): |
| 31 | + url = 'http://'+address+':9000/polycube/v1/cubes/' |
| 32 | + try: |
| 33 | + response = requests.get(url, timeout=8) |
| 34 | + response.raise_for_status() |
| 35 | + except HTTPError as http_err: |
| 36 | + print(f'HTTP error occurred: {http_err}') |
| 37 | + exit() |
| 38 | + except Exception as err: |
| 39 | + print(f'Other error occurred: {err}') |
| 40 | + exit() |
| 41 | + else: |
| 42 | + if response.text == "{}": |
| 43 | + print('\t=== No packetcapture services up ===') |
| 44 | + exit() |
| 45 | + print('\t===Packetcapture Client===\n\n') |
| 46 | + return json.loads(response.text) |
| 47 | + |
| 48 | + |
| 49 | +def printnames(todos): |
| 50 | + global Nservices |
| 51 | + print('services:\n') |
| 52 | + for pktcap in todos['packetcapture']: |
| 53 | + print(str(Nservices) + ' -> ' + pktcap['name']) |
| 54 | + Nservices+=1 |
| 55 | + Nservices-=1 |
| 56 | + print() |
| 57 | + |
| 58 | + |
| 59 | +def selectService(todos, address): |
| 60 | + global service_num, Nservices, service_name |
| 61 | + try: |
| 62 | + service_num = int(input('Please select the service number: ')) |
| 63 | + if service_num > Nservices or service_num < 1: |
| 64 | + exit() |
| 65 | + service_name = todos['packetcapture'][service_num-1]['name'] |
| 66 | + url = 'http://'+address+':9000/polycube/v1/packetcapture/'+service_name+'/' |
| 67 | + try: |
| 68 | + response = requests.get(url, timeout=8) |
| 69 | + response.raise_for_status() |
| 70 | + except HTTPError as http_err: |
| 71 | + print(f'HTTP error occurred: {http_err}') |
| 72 | + exit() |
| 73 | + except Exception as err: |
| 74 | + print(f'Other error occurred: {err}') |
| 75 | + exit() |
| 76 | + todos2 = json.loads(response.text) |
| 77 | + if todos2['networkmode'] is False: |
| 78 | + print('-----------------------------------') |
| 79 | + networkmode_response = input('\n\t=== The service is not in network mode ===\nDo you want to set up this service in networkmode (y / n)? ') |
| 80 | + if networkmode_response != 'y': |
| 81 | + exit() |
| 82 | + else: |
| 83 | + url = 'http://'+address+':9000/polycube/v1/'+service_name+'/networkmode/' |
| 84 | + requests.patch(url, 'true') |
| 85 | + except: |
| 86 | + print("Closing...") |
| 87 | + |
| 88 | + |
| 89 | +def showusage(): |
| 90 | + print("client.py <IPv4 address> <file destination name>") |
| 91 | + print("\t-h/--help: show the usage menu") |
| 92 | + |
| 93 | + |
| 94 | +def checkIp(param): |
| 95 | + if param == 'localhost': |
| 96 | + return True |
| 97 | + try: |
| 98 | + ret = ipaddress.ip_address(param) |
| 99 | + if isinstance(ret,ipaddress.IPv4Address) is False: |
| 100 | + print("Supports only IPv4") |
| 101 | + exit() |
| 102 | + except: |
| 103 | + print("Invalid IP address") |
| 104 | + exit() |
| 105 | + |
| 106 | + |
| 107 | +def initGlobalHeader(address): |
| 108 | + global service_name, PCAP_MAGICAL_NUMBER, PCAP_MJ_VERN_NUMBER, PCAP_MI_VERN_NUMBER, PCAP_LOCAL_CORECTIN, PCAP_ACCUR_TIMSTAMP, PCAP_MAX_LENGTH_CAP, PCAP_DATA_LINK_TYPE |
| 109 | + url = 'http://'+address+':9000/polycube/v1/packetcapture/'+service_name+'/globalheader/' |
| 110 | + try: |
| 111 | + response = requests.get(url, timeout=8) |
| 112 | + response.raise_for_status() |
| 113 | + except HTTPError as http_err: |
| 114 | + print(f'HTTP error occurred: {http_err}') |
| 115 | + exit() |
| 116 | + except Exception as err: |
| 117 | + print(f'Other error occurred: {err}') |
| 118 | + exit() |
| 119 | + todos3 = json.loads(response.text) |
| 120 | + PCAP_MAGICAL_NUMBER = todos3['magic'] |
| 121 | + PCAP_MJ_VERN_NUMBER = todos3['version_major'] |
| 122 | + PCAP_MI_VERN_NUMBER = todos3['version_minor'] |
| 123 | + PCAP_LOCAL_CORECTIN = todos3['thiszone'] |
| 124 | + PCAP_ACCUR_TIMSTAMP = todos3['sigfigs'] |
| 125 | + PCAP_MAX_LENGTH_CAP = todos3['snaplen'] |
| 126 | + PCAP_DATA_LINK_TYPE = todos3['linktype'] |
| 127 | + |
| 128 | + |
| 129 | +def writeGlobalHeader(): |
| 130 | + global f |
| 131 | + f = open(sys.argv[2] + ".pcap", "ab+") |
| 132 | + f.write(struct.pack('@ I H H i I I I ', PCAP_MAGICAL_NUMBER, PCAP_MJ_VERN_NUMBER, PCAP_MI_VERN_NUMBER, PCAP_LOCAL_CORECTIN, PCAP_ACCUR_TIMSTAMP, PCAP_MAX_LENGTH_CAP, PCAP_DATA_LINK_TYPE)) |
| 133 | + |
| 134 | + |
| 135 | +def getAndWritePacket(address): |
| 136 | + global f |
| 137 | + url = 'http://'+address+':9000/polycube/v1/packetcapture/'+service_name+'/packet/' |
| 138 | + try: |
| 139 | + response = requests.get(url, timeout=8) |
| 140 | + response.raise_for_status() |
| 141 | + except HTTPError as http_err: |
| 142 | + print(f'HTTP error occurred: {http_err}') |
| 143 | + exit() |
| 144 | + except Exception as err: |
| 145 | + print(f'Other error occurred: {err}') |
| 146 | + exit() |
| 147 | + todos4 = json.loads(response.text) |
| 148 | + if todos4['rawdata'] != "": |
| 149 | + f.write(struct.pack('@ I I I I ', todos4['timestamp-seconds'], todos4['timestamp-microseconds'], todos4['capturelen'], todos4['packetlen'])) |
| 150 | + asciidata = todos4['rawdata'].encode("latin-1","strict") |
| 151 | + f.write(asciidata) |
| 152 | + else: |
| 153 | + time.sleep(0.1) |
| 154 | + |
| 155 | + |
| 156 | +def listening(): |
| 157 | + global f, stop |
| 158 | + while(1): |
| 159 | + if stop: |
| 160 | + f.close() |
| 161 | + break |
| 162 | + getAndWritePacket(sys.argv[1]) |
| 163 | + |
| 164 | + |
| 165 | +def main(): |
| 166 | + global stop |
| 167 | + |
| 168 | + #Checking arguments |
| 169 | + if len(sys.argv) != 3 or sys.argv[1] == "-h" or sys.argv[1] == "--help": |
| 170 | + showusage() |
| 171 | + exit() |
| 172 | + |
| 173 | + checkIp(sys.argv[1]) |
| 174 | + todos = connect(sys.argv[1]) |
| 175 | + printnames(todos) |
| 176 | + selectService(todos, sys.argv[1]) |
| 177 | + initGlobalHeader(sys.argv[1]) |
| 178 | + writeGlobalHeader() |
| 179 | + listener = threading.Thread(target=listening) |
| 180 | + listener.start() |
| 181 | + while(1): |
| 182 | + usr_cmd = input('write "stop" to end the capture: ') |
| 183 | + if usr_cmd == 'stop': |
| 184 | + stop = True |
| 185 | + break |
| 186 | + listener.join() |
| 187 | + |
| 188 | + |
| 189 | +if __name__ == "__main__": |
| 190 | + main() |
0 commit comments