<s> """<STR_LIT>""" <EOL> import sys <EOL> from collections import defaultdict <EOL> from bacpypes . debugging import Logging , function_debugging , ModuleLogger <EOL> from bacpypes . consolelogging import ConsoleLogHandler <EOL> from bacpypes . pdu import Address <EOL> from bacpypes . analysis import trace , strftimestamp , Tracer <EOL> from bacpypes . npdu import WhoIsRouterToNetwork <EOL> _debug = <NUM_LIT:0> <EOL> _log = ModuleLogger ( globals ( ) ) <EOL> filterSource = None <EOL> filterDestination = None <EOL> filterHost = None <EOL> requests = defaultdict ( int ) <EOL> networks = defaultdict ( list ) <EOL> @ function_debugging <EOL> def Match ( addr1 , addr2 ) : <EOL> """<STR_LIT>""" <EOL> if _debug : Match . _debug ( "<STR_LIT>" , addr1 , addr2 ) <EOL> if ( addr2 . addrType == Address . localBroadcastAddr ) : <EOL> return ( addr1 . addrType == Address . localStationAddr ) or ( addr1 . addrType == Address . localBroadcastAddr ) <EOL> elif ( addr2 . addrType == Address . localStationAddr ) : <EOL> return ( addr1 . addrType == Address . localStationAddr ) and ( addr1 . addrAddr == addr2 . addrAddr ) <EOL> elif ( addr2 . addrType == Address . remoteBroadcastAddr ) : <EOL> return ( ( addr1 . addrType == Address . remoteStationAddr ) or ( addr1 . addrType == Address . remoteBroadcastAddr ) ) and ( addr1 . addrNet == addr2 . addrNet ) <EOL> elif ( addr2 . addrType == Address . remoteStationAddr ) : <EOL> return ( addr1 . addrType == Address . remoteStationAddr ) and ( addr1 . addrNet == addr2 . addrNet ) and ( addr1 . addrAddr == addr2 . addrAddr ) <EOL> elif ( addr2 . addrType == Address . globalBroadcastAddr ) : <EOL> return ( addr1 . addrType == Address . globalBroadcastAddr ) <EOL> else : <EOL> raise RuntimeError , "<STR_LIT>" <EOL> class WhoIsRouterToNetworkSummary ( Tracer , Logging ) : <EOL> def __init__ ( self ) : <EOL> if _debug : IAmRouterToNetworkSummary . _debug ( "<STR_LIT>" ) <EOL> Tracer . __init__ ( self , self . Filter ) <EOL> def Filter ( self , pkt ) : <EOL> if _debug : WhoIsRouterToNetworkSummary . _debug ( "<STR_LIT>" , pkt ) <EOL> global requests , networks <EOL> if not isinstance ( pkt , WhoIsRouterToNetwork ) : <EOL> return <EOL> if filterSource : <EOL> if not Match ( pkt . pduSource , filterSource ) : <EOL> if _debug : WhoIsRouterToNetworkSummary . _debug ( "<STR_LIT>" ) <EOL> return <EOL> if filterDestination : <EOL> if not Match ( pkt . pduDestination , filterDestination ) : <EOL> if _debug : WhoIsRouterToNetworkSummary . _debug ( "<STR_LIT>" ) <EOL> return <EOL> if filterHost : <EOL> if ( not Match ( pkt . pduSource , filterHost ) ) and ( not Match ( pkt . pduDestination , filterHost ) ) : <EOL> if _debug : WhoIsRouterToNetworkSummary . _debug ( "<STR_LIT>" ) <EOL> return <EOL> requests [ pkt . pduSource ] += <NUM_LIT:1> <EOL> networks [ pkt . pduSource ] . append ( pkt . wirtnNetwork ) <EOL> try : <EOL> if ( '<STR_LIT>' in sys . argv ) : <EOL> indx = sys . argv . index ( '<STR_LIT>' ) <EOL> for i in range ( indx + <NUM_LIT:1> , len ( sys . argv ) ) : <EOL> ConsoleLogHandler ( sys . argv [ i ] ) <EOL> del sys . argv [ indx : ] <EOL> if _debug : _log . debug ( "<STR_LIT>" ) <EOL> if ( '<STR_LIT>' in sys . argv ) : <EOL> i = sys . argv . index ( '<STR_LIT>' ) <EOL> filterSource = Address ( sys . argv [ i + <NUM_LIT:1> ] ) <EOL> if _debug : _log . debug ( "<STR_LIT>" , filterSource ) <EOL> del sys . argv [ i : i + <NUM_LIT:2> ] <EOL> if ( '<STR_LIT>' in sys . argv ) : <EOL> i = sys . argv . index ( '<STR_LIT>' ) <EOL> filterDestination = Address ( sys . argv [ i + <NUM_LIT:1> ] ) <EOL> if _debug : _log . debug ( "<STR_LIT>" , filterDestination ) <EOL> del sys . argv [ i : i + <NUM_LIT:2> ] <EOL> if ( '<STR_LIT>' in sys . argv ) : <EOL> i = sys . argv . index ( '<STR_LIT>' ) <EOL> filterHost = Address ( sys . argv [ i + <NUM_LIT:1> ] ) <EOL> if _debug : _log . debug ( "<STR_LIT>" , filterHost ) <EOL> del sys . argv [ i : i + <NUM_LIT:2> ] <EOL> for fname in sys . argv [ <NUM_LIT:1> : ] : <EOL> trace ( fname , [ WhoIsRouterToNetworkSummary ] ) <EOL> items = requests . items ( ) <EOL> items . sort ( lambda x , y : cmp ( y [ <NUM_LIT:1> ] , x [ <NUM_LIT:1> ] ) ) <EOL> print "<STR_LIT>" % ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> for key , count in items : <EOL> print "<STR_LIT>" % ( key , count ) <EOL> net_count = defaultdict ( int ) <EOL> for net in networks [ key ] : <EOL> net_count [ net ] += <NUM_LIT:1> <EOL> net_count = net_count . items ( ) <EOL> net_count . sort ( lambda x , y : cmp ( y [ <NUM_LIT:1> ] , x [ <NUM_LIT:1> ] ) ) <EOL> for net , count in net_count : <EOL> print "<STR_LIT>" % ( net , count ) <EOL> except KeyboardInterrupt : <EOL> pass <EOL> except Exception , e : <EOL> _log . exception ( "<STR_LIT>" , e ) <EOL> finally : <EOL> if _debug : _log . debug ( "<STR_LIT>" ) </s>
