<s> """<STR_LIT>""" <EOL> import sys <EOL> import os <EOL> import traceback <EOL> libpath = os . path . dirname ( os . path . abspath ( __file__ ) ) <EOL> sys . path [ : <NUM_LIT:0> ] = [ os . path . join ( libpath , '<STR_LIT>' ) ] <EOL> import common <EOL> import environment <EOL> import xmltodict <EOL> from collections import OrderedDict <EOL> logger = common . logging . getLogger ( ) . getChild ( '<STR_LIT>' ) <EOL> try : <EOL> import splunk . Intersplunk <EOL> import splunk . entity as entity <EOL> except ImportError as e : <EOL> logger . error ( "<STR_LIT>" <EOL> "<STR_LIT>" % __file__ ) <EOL> sys . exit ( <NUM_LIT:3> ) <EOL> libpath = os . path . dirname ( os . path . abspath ( __file__ ) ) <EOL> sys . path [ : <NUM_LIT:0> ] = [ os . path . join ( libpath , '<STR_LIT>' ) ] <EOL> sys . path [ : <NUM_LIT:0> ] = [ os . path . join ( libpath , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> sys . path [ : <NUM_LIT:0> ] = [ os . path . join ( libpath , '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> try : <EOL> import pandevice . base <EOL> import pan . xapi <EOL> except ImportError : <EOL> print "<STR_LIT>" <EOL> exit ( <NUM_LIT:3> ) <EOL> from common import log <EOL> def usage ( ) : <EOL> common . exit_with_error ( "<STR_LIT>" ) <EOL> def parse_apps ( apps_xml ) : <EOL> obj = xmltodict . parse ( apps_xml ) <EOL> try : <EOL> apps = obj [ '<STR_LIT>' ] [ '<STR_LIT:result>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> except KeyError as e : <EOL> logger . error ( "<STR_LIT>" ) <EOL> raise e <EOL> csv_apps = [ ] <EOL> for app in apps : <EOL> a = OrderedDict ( ) <EOL> try : <EOL> a [ '<STR_LIT>' ] = app [ '<STR_LIT>' ] <EOL> a [ '<STR_LIT>' ] = app . get ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> a [ '<STR_LIT>' ] = app . get ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> a [ '<STR_LIT>' ] = app . get ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> a [ '<STR_LIT>' ] = app [ '<STR_LIT>' ] <EOL> a [ '<STR_LIT>' ] = app [ '<STR_LIT>' ] <EOL> a [ '<STR_LIT>' ] = app [ '<STR_LIT>' ] <EOL> a [ '<STR_LIT>' ] = app [ '<STR_LIT>' ] <EOL> a [ '<STR_LIT>' ] = app [ '<STR_LIT>' ] <EOL> a [ '<STR_LIT>' ] = app [ '<STR_LIT>' ] <EOL> a [ '<STR_LIT>' ] = app [ '<STR_LIT>' ] <EOL> if a [ '<STR_LIT>' ] != u"<STR_LIT:yes>" and a [ '<STR_LIT>' ] != u"<STR_LIT>" : <EOL> a [ '<STR_LIT>' ] = a [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> a [ '<STR_LIT>' ] = app [ '<STR_LIT>' ] <EOL> a [ '<STR_LIT>' ] = app [ '<STR_LIT>' ] <EOL> a [ '<STR_LIT>' ] = app . get ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> a [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> try : <EOL> default = app [ '<STR_LIT:default>' ] <EOL> if isinstance ( default , list ) : <EOL> for d in default : <EOL> a [ '<STR_LIT>' ] = d [ '<STR_LIT:port>' ] [ '<STR_LIT>' ] <EOL> break <EOL> else : <EOL> a [ '<STR_LIT>' ] = default [ '<STR_LIT:port>' ] [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> if not isinstance ( a [ '<STR_LIT>' ] , basestring ) : <EOL> a [ '<STR_LIT>' ] = "<STR_LIT:|>" . join ( a [ '<STR_LIT>' ] ) <EOL> except Exception as e : <EOL> logger . error ( "<STR_LIT>" % app [ '<STR_LIT>' ] ) <EOL> logger . error ( traceback . format_exc ( ) ) <EOL> common . exit_with_error ( str ( e ) ) <EOL> for key in a : <EOL> a [ key ] = str ( a [ key ] ) <EOL> csv_apps . append ( a ) <EOL> logger . info ( "<STR_LIT>" % len ( csv_apps ) ) <EOL> return csv_apps <EOL> def parse_threats ( threats_xml ) : <EOL> obj = xmltodict . parse ( threats_xml ) <EOL> try : <EOL> phone_home = obj [ '<STR_LIT>' ] [ '<STR_LIT:result>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> vulnerability = obj [ '<STR_LIT>' ] [ '<STR_LIT:result>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> threats = phone_home + vulnerability <EOL> except KeyError as e : <EOL> logger . error ( "<STR_LIT>" ) <EOL> raise e <EOL> csv_threats = [ ] <EOL> for threat in threats : <EOL> a = OrderedDict ( ) <EOL> try : <EOL> a [ '<STR_LIT>' ] = threat [ '<STR_LIT>' ] <EOL> a [ '<STR_LIT>' ] = threat [ '<STR_LIT>' ] <EOL> a [ '<STR_LIT>' ] = threat [ '<STR_LIT>' ] <EOL> a [ '<STR_LIT>' ] = threat [ '<STR_LIT>' ] <EOL> a [ '<STR_LIT>' ] = threat . get ( '<STR_LIT>' , None ) <EOL> if a [ '<STR_LIT>' ] is not None : <EOL> a [ '<STR_LIT>' ] = threat [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if not isinstance ( a [ '<STR_LIT>' ] , basestring ) : <EOL> a [ '<STR_LIT>' ] = "<STR_LIT:U+002CU+0020>" . join ( a [ '<STR_LIT>' ] ) <EOL> else : <EOL> a [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> except KeyError as e : <EOL> logger . error ( "<STR_LIT>" % threat [ '<STR_LIT>' ] ) <EOL> raise e <EOL> for key in a : <EOL> a [ key ] = str ( a [ key ] ) <EOL> csv_threats . append ( a ) <EOL> logger . info ( "<STR_LIT>" % len ( csv_threats ) ) <EOL> return csv_threats <EOL> def main ( ) : <EOL> args , kwargs = splunk . Intersplunk . getKeywordsAndOptions ( ) <EOL> debug = common . check_debug ( kwargs ) <EOL> if len ( args ) < <NUM_LIT:2> : <EOL> logger . error ( "<STR_LIT>" % len ( args ) ) <EOL> usage ( ) <EOL> if args [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> logger . info ( "<STR_LIT>" % args [ <NUM_LIT:0> ] ) <EOL> elif args [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> logger . info ( "<STR_LIT>" % args [ <NUM_LIT:0> ] ) <EOL> else : <EOL> usage ( ) <EOL> results , unused1 , settings = splunk . Intersplunk . getOrganizedResults ( ) <EOL> sessionKey = settings [ '<STR_LIT>' ] <EOL> log ( debug , "<STR_LIT>" ) <EOL> apikey = common . apikey ( sessionKey , args [ <NUM_LIT:0> ] , debug ) <EOL> device = pandevice . base . PanDevice ( args [ <NUM_LIT:0> ] , api_key = apikey ) <EOL> try : <EOL> if args [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> device . xapi . get ( "<STR_LIT>" ) <EOL> app_xml = device . xapi . xml_document <EOL> csv = parse_apps ( app_xml ) <EOL> else : <EOL> device . xapi . get ( "<STR_LIT>" ) <EOL> threat_xml = device . xapi . xml_document <EOL> csv = parse_threats ( threat_xml ) <EOL> except pan . xapi . PanXapiError as e : <EOL> common . exit_with_error ( str ( e ) ) <EOL> splunk . Intersplunk . outputResults ( csv ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
