From 9a5699cf273d93111b68b91eab7538586a3954db Mon Sep 17 00:00:00 2001 From: vapao Date: Fri, 29 May 2020 15:30:42 +0800 Subject: [PATCH] =?UTF-8?q?U=20=E4=BC=98=E5=8C=96=E4=B8=BB=E6=9C=BA?= =?UTF-8?q?=E6=89=B9=E9=87=8F=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spug_api/apps/host/views.py | 14 +++++++++++--- spug_web/src/pages/host/Import.js | 3 +++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/spug_api/apps/host/views.py b/spug_api/apps/host/views.py index 389e493..cf90cf1 100644 --- a/spug_api/apps/host/views.py +++ b/spug_api/apps/host/views.py @@ -13,6 +13,7 @@ from apps.account.models import Role from libs.ssh import SSH, AuthenticationException from libs import human_datetime, AttrDict from openpyxl import load_workbook +import socket class HostView(View): @@ -96,7 +97,7 @@ def post_import(request): password = request.POST.get('password') file = request.FILES['file'] ws = load_workbook(file, read_only=True)['Sheet1'] - summary = {'invalid': [], 'skip': [], 'fail': [], 'success': []} + summary = {'invalid': [], 'skip': [], 'fail': [], 'network': [], 'success': []} for i, row in enumerate(ws.rows): if i == 0: # 第1行是表头 略过 continue @@ -116,9 +117,16 @@ def post_import(request): deleted_by_id__isnull=True).exists(): summary['skip'].append(i) continue - if valid_ssh(data.hostname, data.port, data.username, data.pop('password') or password) is False: + try: + if valid_ssh(data.hostname, data.port, data.username, data.pop('password') or password) is False: + summary['fail'].append(i) + continue + except AuthenticationException: summary['fail'].append(i) continue + except socket.error: + summary['network'].append(i) + continue host = Host.objects.create(created_by=request.user, **data) if request.user.role: request.user.role.add_host_perm(host.id) @@ -135,7 +143,7 @@ def valid_ssh(hostname, port, username, password): AppSetting.set('private_key', private_key, 'ssh private key') AppSetting.set('public_key', public_key, 'ssh public key') if password: - cli = SSH(hostname, port, username, password=password) + cli = SSH(hostname, port, username, password=str(password)) code, out = cli.exec_command('mkdir -p -m 700 ~/.ssh && \ echo %r >> ~/.ssh/authorized_keys && \ chmod 600 ~/.ssh/authorized_keys' % public_key) diff --git a/spug_web/src/pages/host/Import.js b/spug_web/src/pages/host/Import.js index 0c5f718..b863408 100644 --- a/spug_web/src/pages/host/Import.js +++ b/spug_web/src/pages/host/Import.js @@ -34,6 +34,9 @@ class ComImport extends React.Component { {res['fail'].length > 0 && {res['fail'].length} } + {res['network'].length > 0 && + {res['network'].length} + } {res['skip'].length > 0 && {res['skip'].length} }