@@ -0,0 +1,2723 @@
+#!/usr/bin/env python%0A# This Source Code Form is subject to the terms of the Mozilla Public License,%0A# v. 2.0. If a copy of the MPL was not distributed with this file, You can%0A# obtain one at http://mozilla.org/MPL/2.0/.%0A%0Aimport base64%0Aimport json%0Aimport logging%0Aimport os%0Aimport requests%0A%0Aimport stoneridge%0A%0A%0Aclass StoneRidgePcap(object):%0A    def __init__(self, stop):%0A        self.stop = stop%0A        self.macaddr = stoneridge.get_config('machine', 'macaddr')%0A        self.netconfig = stoneridge.get_config('run', 'netconfig')%0A        self.host = stoneridge.get_config('tcpdump', self.netconfig)%0A        self.outdir = stoneridge.get_config('run', 'out')%0A        self.stdout = os.path.join(self.outdir, 'tcpdump.out')%0A        self.pcap = os.path.join(self.outdir, 'traffic.pcap')%0A%0A        logging.debug('stop: %25s' %25 (stop,))%0A        logging.debug('macaddr: %25s' %25 (self.macaddr,))%0A        logging.debug('netconfig: %25s' %25 (self.netconfig,))%0A        logging.debug('host: %25s' %25 (self.host,))%0A        logging.debug('output directory: %25s' %25 (self.outdir,))%0A        logging.debug('tcpdump output: %25s' %25 (self.stdout,))%0A        logging.debug('packet capture: %25s' %25 (self.pcap,))%0A%0A    def start_pcap(self):%0A        url = 'http://%25s/start/%25s' %25 (self.host, self.macaddr)%0A        response = requests.post(url)%0A        res = json.loads(response)%0A        if res%5B'status'%5D != 'ok':%0A            logging.error('Error starting pcap: %25s' %25 (res%5B'message'%5D,))%0A        else:%0A            logging.debug('Started pcap')%0A%0A    def stop_pcap(self):%0A        url = 'http://%25s/stop/%25s' %25 (self.host, self.macaddr)%0A        response = requests.post(url)%0A        res = json.loads(response)%0A        if res%5B'status'%5D != 'ok':%0A            logging.error('Error stopping pcap: %25s' %25 (res%5B'message'%5D,))%0A            return%0A%0A        url = 'http://%25s/retrieve/%25s' %25 (self.host, self.macaddr)%0A        response = requests.post(url)%0A        res = json.loads(response)%0A        if res%5B'status'%5D != 'ok':%0A            logging.error('Error retrieving pcap: %25s' %25 (res%5B'message'%5D,))%0A            return%0A%0A        stdout = base64.b64decode(res%5B'stdout'%5D)%0A        pcap = base64.b64decode(res%5B'pcap'%5D)%0A%0A        with file(self.stdout, 'wb') as f:%0A            f.write(stdout)%0A%0A        with file(self.pcap, 'wb') as f:%0A            f.write(pcap)%0A%0A    def run(self):%0A        if self.stop:%0A            self.stop_pcap()%0A        else:%0A            self.start_pcap()%0A%0A%0A@stoneridge.main%0Adef main():%0A    parser = stoneridge.TestRunArgumentParser()%0A    group = parser.add_mutually_exclusive_group(required=True)%0A    group.add_argument('--start', dest='start')%0A    group.add_argument('--stop', dest='stop')%0A    args = parser.parse_args()%0A%0A    pcap = StoneRidgePcap(args.stop)%0A    pcap.run()%0A

Add client-side pcap program