コード例 #1
0
 def handle(self, *args, **kwargs):
     if sum([
         kwargs['network'],
         kwargs['data_center'],
         kwargs['environment'],
         kwargs['queue']
     ]) > 1:
         raise SystemExit(
             "You can't mix networks, environments, data centers and "
             "queues.",
         )
     if not args:
         raise SystemExit("Please specify the addresses to scan.")
     if kwargs['network']:
         try:
             for network in [
                 find_network(network_spec) for network_spec in args
             ]:
                 autoscan_network(network)
         except (Error, Network.DoesNotExist) as e:
             raise SystemExit(e)
     elif kwargs['environment']:
         try:
             for environment in [
                 Environment.objects.get(name=name) for name in args
             ]:
                 autoscan_environment(environment)
         except (Error, Environment.DoesNotExist) as e:
             raise SystemExit(e)
     elif kwargs['data_center']:
         try:
             for data_center in [
                 DataCenter.objects.get(name=name) for name in args
             ]:
                 for environment in data_center.environment_set.filter(
                     queue__isnull=False,
                 ):
                     autoscan_environment(environment)
         except (Error, DataCenter.DoesNotExist) as e:
             raise SystemExit(e)
     elif kwargs['queue']:
         try:
             for queue in [
                 DiscoveryQueue.objects.get(name=name) for name in args
             ]:
                 for environment in queue.environment_set.all():
                     autoscan_environment(environment)
         except (Error, DiscoveryQueue.DoesNotExist) as e:
             raise SystemExit(e)
     else:
         try:
             addresses = [str(ipaddr.IPAddress(ip)) for ip in args]
             for address in addresses:
                 autoscan_address(address)
         except (Error, ValueError) as e:
             raise SystemExit(e)
コード例 #2
0
ファイル: scan.py プロジェクト: 4i60r/ralph
 def handle(self, *args, **kwargs):
     options_sum = sum([
         kwargs['network'],
         kwargs['data_center'],
         kwargs['environment'],
         kwargs['queue'],
     ])
     if options_sum > 1:
         raise SystemExit(
             "You can't mix networks, environments, data centers and "
             "queues.",
         )
     if not args and options_sum == 0:
         raise SystemExit("Please specify the IP address to scan.")
     plugins = getattr(settings, 'SCAN_PLUGINS', {}).keys()
     verbose = kwargs['verbose']
     if kwargs["plugins"]:
         new_plugins = map(
             lambda s: 'ralph.scan.plugins.{}'.format(s),
             kwargs["plugins"].split(","),
         )
         plugins = filter(lambda plug: plug in new_plugins, plugins)
     if kwargs['network']:
         try:
             for network in [
                 find_network(network_spec) for network_spec in args
             ]:
                 queue_scan_network(network, plugins)
         except (Error, Network.DoesNotExist) as e:
             raise SystemExit(e)
     elif kwargs['environment']:
         try:
             for environment in [
                 Environment.objects.get(name=name) for name in args
             ]:
                 queue_scan_environment(environment, plugins)
         except (Error, Environment.DoesNotExist) as e:
             raise SystemExit(e)
     elif kwargs['data_center']:
         try:
             for data_center in [
                 DataCenter.objects.get(name=name) for name in args
             ]:
                 for environment in data_center.environment_set.filter(
                     queue__isnull=False,
                 ):
                     queue_scan_environment(environment, plugins)
         except (Error, DataCenter.DoesNotExist) as e:
             raise SystemExit(e)
     elif kwargs['queue']:
         try:
             for queue in [
                 DiscoveryQueue.objects.get(name=name) for name in args
             ]:
                 for environment in queue.environment_set.all():
                     queue_scan_environment(environment, plugins)
         except (Error, DiscoveryQueue.DoesNotExist) as e:
             raise SystemExit(e)
     else:
         try:
             ip_addresses = [unicode(ipaddr.IPAddress(ip)) for ip in args]
         except ValueError as e:
             raise SystemExit(e)
         else:
             last_message = 0
             for ip_address in ip_addresses:
                 job = queue_scan_address(ip_address, plugins)
                 while not job.is_finished:
                     job.refresh()
                     last_message = print_job_messages(
                         job, last_message, verbose)
                     if job.is_failed:
                         raise SystemExit(job.exc_info)
                     time.sleep(1)
                 print('\nAll done!')
                 for plugin_name, job_result in job.result.iteritems():
                     if job_result.get('status') == 'success':
                         if not verbose:
                             print('Success: ', plugin_name)
                         else:
                             print('-' * 40)
                             print('Successful plugin: ' + plugin_name)
                             print('Result data:')
                             pprint.pprint(job_result.get('device', ''))
コード例 #3
0
 def handle(self, *args, **kwargs):
     options_sum = sum([
         kwargs['network'],
         kwargs['data_center'],
         kwargs['environment'],
         kwargs['queue'],
     ])
     if options_sum > 1:
         raise SystemExit(
             "You can't mix networks, environments, data centers and "
             "queues.", )
     if not args and options_sum == 0:
         raise SystemExit("Please specify the IP address to scan.")
     plugins = getattr(settings, 'SCAN_PLUGINS', {}).keys()
     verbose = kwargs['verbose']
     if kwargs["plugins"]:
         new_plugins = map(
             lambda s: 'ralph.scan.plugins.{}'.format(s),
             kwargs["plugins"].split(","),
         )
         plugins = filter(lambda plug: plug in new_plugins, plugins)
     if kwargs['network']:
         try:
             for network in [
                     find_network(network_spec) for network_spec in args
             ]:
                 scan_network(network, plugins)
         except (Error, Network.DoesNotExist) as e:
             raise SystemExit(e)
     elif kwargs['environment']:
         try:
             for environment in [
                     Environment.objects.get(name=name) for name in args
             ]:
                 scan_environment(environment, plugins)
         except (Error, Environment.DoesNotExist) as e:
             raise SystemExit(e)
     elif kwargs['data_center']:
         try:
             for data_center in [
                     DataCenter.objects.get(name=name) for name in args
             ]:
                 for environment in data_center.environment_set.filter(
                         queue__isnull=False, ):
                     scan_environment(environment, plugins)
         except (Error, DataCenter.DoesNotExist) as e:
             raise SystemExit(e)
     elif kwargs['queue']:
         try:
             for queue in [
                     DiscoveryQueue.objects.get(name=name) for name in args
             ]:
                 for environment in queue.environment_set.all():
                     scan_environment(environment, plugins)
         except (Error, DiscoveryQueue.DoesNotExist) as e:
             raise SystemExit(e)
     else:
         try:
             ip_addresses = [unicode(ipaddr.IPAddress(ip)) for ip in args]
         except ValueError as e:
             raise SystemExit(e)
         else:
             last_message = 0
             for ip_address in ip_addresses:
                 job = scan_address(ip_address, plugins)
                 while not job.is_finished:
                     job.refresh()
                     last_message = print_job_messages(
                         job, last_message, verbose)
                     if job.is_failed:
                         raise SystemExit(job.exc_info)
                     time.sleep(1)
                 print('\nAll done!')
                 for plugin_name, job_result in job.result.iteritems():
                     if job_result.get('status') == 'success':
                         if not verbose:
                             print('Success: ', plugin_name)
                         else:
                             print('-' * 40)
                             print('Successful plugin: ' + plugin_name)
                             print('Result data:')
                             pprint.pprint(job_result.get('device', ''))