forked from RobbieHan/sandboxMP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
115 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import time | ||
import logging | ||
|
||
from celery import shared_task | ||
from celery_once import QueueOnce | ||
|
||
from utils.sandbox_utils import SandboxScan, LoginExecution | ||
from .models import DeviceScanInfo | ||
|
||
info_logger = logging.getLogger('sandbox_info') | ||
|
||
|
||
@shared_task(base=QueueOnce) | ||
def scan_execution(): | ||
scan = SandboxScan() | ||
execution = LoginExecution() | ||
scan_type = execution.get_scan_type() | ||
auth_type = execution.get_auth_type() | ||
start_time = time.time() | ||
if scan_type == 'basic_scan': | ||
hosts = scan.basic_scan() | ||
for host in hosts: | ||
DeviceScanInfo.objects.update_or_create( | ||
hostname=host, | ||
) | ||
else: | ||
hosts = scan.os_scan() | ||
login_hosts = [host for host in hosts if host['os'] in ['Linux', 'embedded']] | ||
nologin_hosts = [host for host in hosts if host not in login_hosts] | ||
for host in nologin_hosts: | ||
DeviceScanInfo.objects.update_or_create( | ||
hostname=host['host'], | ||
defaults={ | ||
'os_type': host['os'] | ||
} | ||
) | ||
for host in login_hosts: | ||
kwargs = { | ||
'hostname': host['host'], | ||
'username': execution.get_ssh_username(), | ||
'port': execution.get_ssh_port(), | ||
'password': execution.get_ssh_password(), | ||
'private_key': execution.get_ssh_private_key() | ||
} | ||
defaults = execution.login_execution(auth_type=auth_type, **kwargs) | ||
DeviceScanInfo.objects.update_or_create( | ||
hostname=host['host'], | ||
defaults=defaults | ||
) | ||
end_time = time.time() | ||
msg = 'Scan task has been completed, execution time: %(time)s, %(num)s hosts are up.' % { | ||
'time': end_time - start_time, | ||
'num': len(hosts) | ||
} | ||
info_logger.info(msg) | ||
return msg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from __future__ import absolute_import, unicode_literals | ||
|
||
from .celery import app as celery_app | ||
|
||
__all__ = ('celery_app') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from __future__ import absolute_import, unicode_literals | ||
import os | ||
from celery import Celery | ||
|
||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sandboxMP.settings') | ||
|
||
app = Celery('sandbox') | ||
|
||
app.config_from_object('django.conf:settings') | ||
|
||
app.autodiscover_tasks() | ||
|
||
BROKER_URL = 'redis://localhost:6379/0' | ||
|
||
CELERY_RESULT_BACKEND = 'redis://localhost:6379/1' | ||
|
||
CELERY_TIMEZONE = 'Asia/Shanghai' | ||
|
||
CELERY_ENABLE_UTC = False | ||
|
||
CELERYD_FORCE_EXECV = True | ||
|
||
CELERYD_CONCURRENCY = 5 | ||
|
||
CELERY_ACKS_LATE = True | ||
|
||
CELERYD_MAX_TASKS_PER_CHILD = 100 | ||
|
||
CELERYD_TASK_TIME_LIMIT = 60 * 5 | ||
|
||
|
||
app.conf.ONCE = { | ||
'backend': 'celery_once.backends.Redis', | ||
'settings': { | ||
'url': 'redis://localhost:6379/2', | ||
'default_timeout': 60 * 5 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters