· 8 years ago · Jan 24, 2018, 03:50 PM
1#!/usr/bin/env python
2
3import os
4from datetime import datetime, date, timedelta
5import subprocess
6import pyjq
7import pandas as pd
8
9# Initializes the variables for the directories
10HomeDir = "Users/kiya/Downloads/"
11ScriptDir = HomeDir + "pan-python-0.12.0 2"
12ResultDir = HomeDir + "techscroll/"
13
14# Create the dates
15ToDay = datetime.now().strftime('%Y%m%d')
16# checkDATE = (date.today() - timedelta(1)).strfttime('%Y%m%d')
17ResultFile = "Test"
18CheckDATE = "Test"
19NOWDATE = "TEST"
20
21# Run the panafpi
22subprocess.check_output('python ' + ScriptDir + '/bin/panafapi.py -K secretkey --samples -j -r "{\"query\":{\"operator\":\"all\",\"children\":[{\"field\":\"alias.ip_address\",\"operator\":\"contains\",\"value\":\"' + ResultFile + '\"},{\"operator\":\"any\",\"children\":[{\"field\":\"sample.update_date\",\"operator\":\"is in the range\",\"value\":[\"' + CheckDATE + 'T00:00:00\",\"$' + NOWDATE + 'T23:59:59\"]},{\"field\":\"sample.create_date\",\"operator\":\"is in the range\",\"value\":[\"' + CheckDATE + 'T00:00:00\",\"' + NOWDATE + 'T23:59:59\"]},{\"operator\":\"any\",\"children\":[{\"field\":\"sample.malware\",\"operator\":\"is\",\"value\":1},{\"field\":\"sample.malware\",\"operator\":\"is\",\"value\":4}]}]}]},\"scope\":\"global\",\"size\":1,\"from\":0,\"sort\":{\"create_date\":{\"order\":\"desc\"}}}" > ' + ResultDir + 'srciplist-' + ToDay + '.json', shell=True)
23
24# Using pyjq to filter
25filteredResultData = pyjq.all('.hits[]._source | .create_date + "," + .sha256')
26
27# Save the JSON file to comma-separated file
28pd.to_csv(ResultDir + "/srciplist-" + ToDay + ".csv", sep=",")
29
30
31
32error:
33
34/bin/sh: Users/kiya/Downloads/techscroll/srciplist-20180125.json: No such file or directory
35Traceback (most recent call last):
36 File "/Users/mani/Downloads/tester (1).py", line 22, in <module>
37 subprocess.check_output('python ' + ScriptDir + '/bin/panafapi.py -K secretkey --samples -j -r "{\"query\":{\"operator\":\"all\",\"children\":[{\"field\":\"alias.ip_address\",\"operator\":\"contains\",\"value\":\"' + ResultFile + '\"},{\"operator\":\"any\",\"children\":[{\"field\":\"sample.update_date\",\"operator\":\"is in the range\",\"value\":[\"' + CheckDATE + 'T00:00:00\",\"$' + NOWDATE + 'T23:59:59\"]},{\"field\":\"sample.create_date\",\"operator\":\"is in the range\",\"value\":[\"' + CheckDATE + 'T00:00:00\",\"' + NOWDATE + 'T23:59:59\"]},{\"operator\":\"any\",\"children\":[{\"field\":\"sample.malware\",\"operator\":\"is\",\"value\":1},{\"field\":\"sample.malware\",\"operator\":\"is\",\"value\":4}]}]}]},\"scope\":\"global\",\"size\":1,\"from\":0,\"sort\":{\"create_date\":{\"order\":\"desc\"}}}" > ' + ResultDir + 'srciplist-' + ToDay + '.json', shell=True)
38 File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 336, in check_output
39 **kwargs).stdout
40 File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 418, in run
41 output=stdout, stderr=stderr)
42subprocess.CalledProcessError: Command 'python Users/kiya/Downloads/pan-python-0.12.0 2/bin/panafapi.py -K secret key --samples -j -r "{"query":{"operator":"all","children":[{"field":"alias.ip_address","operator":"contains","value":"Test"},{"operator":"any","children":[{"field":"sample.update_date","operator":"is in the range","value":["TestT00:00:00","$TESTT23:59:59"]},{"field":"sample.create_date","operator":"is in the range","value":["TestT00:00:00","TESTT23:59:59"]},{"operator":"any","children":[{"field":"sample.malware","operator":"is","value":1},{"field":"sample.malware","operator":"is","value":4}]}]}]},"scope":"global","size":1,"from":0,"sort":{"create_date":{"order":"desc"}}}" > Users/kiya/Downloads/techscroll/srciplist-20180125.json' returned non-zero exit status 1.