<s> import sys <EOL> from socket import gethostbyaddr <EOL> from optparse import OptionParser <EOL> from twisted . internet import task <EOL> from twisted . internet import defer <EOL> from twisted . internet import reactor <EOL> from twisted . internet . protocol import Protocol <EOL> from twisted . internet . protocol import ClientFactory <EOL> from twisted . python . filepath import FilePath <EOL> try : <EOL> from netcidr import CIDR , Networks <EOL> except ImportError : <EOL> print ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> sys . exit ( ) <EOL> from pymon . utils import getSimplePlural <EOL> usage = '''<STR_LIT>''' <EOL> optp = OptionParser ( usage = usage ) <EOL> optp . set_defaults ( ports = '<STR_LIT>' , batchSize = <NUM_LIT:100> , timeout = <NUM_LIT:0.1> , verbose = False , <EOL> spew = False , debug = False ) <EOL> optp . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' ) <EOL> optp . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' ) <EOL> optp . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> optp . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> optp . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> ( opts , args ) = optp . parse_args ( ) <EOL> if len ( args ) < <NUM_LIT:2> : <EOL> optp . error ( '<STR_LIT>' ) <EOL> blocks = args [ <NUM_LIT:0> ] . split ( '<STR_LIT:U+002C>' ) <EOL> ports = args [ <NUM_LIT:1> ] . split ( '<STR_LIT:U+002C>' ) <EOL> if len ( ports ) != <NUM_LIT:2> : <EOL> optp . error ( '<STR_LIT>' ) <EOL> try : <EOL> ports = [ int ( x ) for x in ports ] <EOL> except ValueError : <EOL> optp . error ( '<STR_LIT>' ) <EOL> TCPPingServiceConfigTemplate = '''<STR_LIT>''' <EOL> def printError ( err ) : <EOL> print '<STR_LIT>' <EOL> if opts . debug : <EOL> print err <EOL> else : <EOL> print '<STR_LIT>' % err . getErrorMessage ( ) <EOL> print '<STR_LIT:\n>' <EOL> class ScanProtocol ( Protocol ) : <EOL> def connectionMade ( self ) : <EOL> self . factory . deferred . callback ( "<STR_LIT:success>" ) <EOL> self . transport . loseConnection ( ) <EOL> class ScanFactory ( ClientFactory ) : <EOL> protocol = ScanProtocol <EOL> def __init__ ( self ) : <EOL> self . deferred = defer . Deferred ( ) <EOL> def clientConnectionFailed ( self , connector , reason ) : <EOL> self . deferred . errback ( reason ) <EOL> def clientConnectionLost ( self , connector , reason ) : <EOL> pass <EOL> class Scanner ( object ) : <EOL> '''<STR_LIT:U+0020>''' <EOL> def __init__ ( self , hosts , portRange , batchSize = <NUM_LIT:50> , timeout = <NUM_LIT:1> ) : <EOL> self . hosts = hosts <EOL> portRange [ <NUM_LIT:1> ] += <NUM_LIT:1> <EOL> self . portRange = portRange <EOL> self . batchSize = int ( batchSize ) <EOL> self . timeout = float ( timeout ) <EOL> self . data = { <EOL> '<STR_LIT:success>' : { } , <EOL> '<STR_LIT>' : { } , <EOL> } <EOL> def run ( self ) : <EOL> d = self . doScans ( ) <EOL> d . addCallback ( self . doReport ) <EOL> d . addCallback ( self . writeConfigs ) <EOL> d . addErrback ( printError ) <EOL> reactor . run ( ) <EOL> def doScans ( self ) : <EOL> '''<STR_LIT>''' <EOL> coop = task . Cooperator ( ) <EOL> def scanHosts ( ) : <EOL> for host in self . hosts : <EOL> for port in xrange ( * self . portRange ) : <EOL> yield self . doFactory ( host , port ) <EOL> scans = scanHosts ( ) <EOL> self . scans = defer . DeferredList ( <EOL> [ coop . coiterate ( scans ) for i in xrange ( self . batchSize ) ] ) <EOL> return self . scans <EOL> def doFactory ( self , host , port ) : <EOL> factory = ScanFactory ( ) <EOL> if opts . spew : <EOL> print "<STR_LIT>" % ( host , port ) <EOL> reactor . connectTCP ( host , port , factory , self . timeout ) <EOL> d = factory . deferred <EOL> d . addCallback ( self . recordConnection , host , port ) <EOL> d . addErrback ( self . recordFailure , host , port ) <EOL> return d <EOL> def recordConnection ( self , result , host , port ) : <EOL> if opts . verbose or opts . spew : <EOL> print "<STR_LIT>" % ( host , port ) <EOL> hostData = self . data [ '<STR_LIT:success>' ] . setdefault ( host , [ ] ) <EOL> hostData . append ( port ) <EOL> def recordFailure ( self , failure , host , port ) : <EOL> hostData = self . data [ '<STR_LIT>' ] . setdefault ( host , [ ] ) <EOL> data = ( port , failure . getErrorMessage ( ) ) <EOL> hostData . append ( data ) <EOL> def doReport ( self , ignore ) : <EOL> results = self . getSuccesses ( ) . items ( ) <EOL> if results : <EOL> print "<STR_LIT>" <EOL> else : <EOL> print "<STR_LIT>" <EOL> for host , ports in results : <EOL> print "<STR_LIT>" % host <EOL> for port in ports : <EOL> print "<STR_LIT>" % port <EOL> errors = { } <EOL> results = self . getFailures ( ) . items ( ) <EOL> for host , portAndError in results : <EOL> for port , error in portAndError : <EOL> errors . setdefault ( error , <NUM_LIT:0> ) <EOL> errors [ error ] += <NUM_LIT:1> <EOL> if results and ( opts . verbose or opts . spew ) : <EOL> print "<STR_LIT>" <EOL> for error , count in errors . items ( ) : <EOL> print "<STR_LIT>" % ( error , count ) <EOL> def writeConfigs ( self , result ) : <EOL> reactor . stop ( ) <EOL> basePath = FilePath ( '<STR_LIT>' ) <EOL> if not basePath . exists ( ) : <EOL> basePath . makedirs ( ) <EOL> results = self . getSuccesses ( ) . items ( ) <EOL> if results : <EOL> print "<STR_LIT>" <EOL> for host , ports in results : <EOL> hostname = gethostbyaddr ( host ) [ <NUM_LIT:0> ] <EOL> cfgFile = basePath . child ( '<STR_LIT>' % hostname ) <EOL> if cfgFile . exists ( ) : <EOL> continue <EOL> print "<STR_LIT>" % cfgFile . path <EOL> cfgFile . touch ( ) <EOL> fh = cfgFile . open ( '<STR_LIT:w>' ) <EOL> ports = '<STR_LIT:U+002C>' . join ( [ str ( x ) for x in ports ] ) <EOL> cfg = TCPPingServiceConfigTemplate % ( host , ports ) <EOL> fh . write ( cfg ) <EOL> fh . close ( ) <EOL> def getSuccesses ( self ) : <EOL> return self . data [ '<STR_LIT:success>' ] <EOL> def getFailures ( self ) : <EOL> return self . data [ '<STR_LIT>' ] <EOL> def main ( ) : <EOL> nets = Networks ( [ CIDR ( x ) for x in blocks ] ) <EOL> total = nets . getHostCount ( ) <EOL> netword = getSimplePlural ( '<STR_LIT>' , len ( nets ) ) <EOL> hostword = getSimplePlural ( '<STR_LIT:host>' , total ) <EOL> countMsg = '<STR_LIT>' % ( total , hostword ) <EOL> print '<STR_LIT>' % ( netword , countMsg ) <EOL> for net in nets : <EOL> print '<STR_LIT>' % net <EOL> scanner = Scanner ( nets . iterIPs ( ) , ports , opts . batchSize , opts . timeout ) <EOL> scanner . run ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
