示例#1
0
def transfer_file():
    global track_num
    # pool.apply_async()
    servers_id_list = request.form.getlist('servers')
    remote_user_id = request.form.get('remote_user')
    trans_type = request.form.get('trans_type')
    remote_path = request.form.get('remote_path')
    servers_id_list = list(set(servers_id_list))

    if trans_type == 'getfile':
        files = request.form.get('files')
        event_type = 'GetFile'
        log_type = 'BatchGetFile'
        total_num = len(servers_id_list)
    else:
        files = request.form.getlist('files')
        event_type = 'SendFile'
        log_type = 'BatchSendFile'
        total_num = len(servers_id_list) * len(files)

    msg = {"success": [], "fail": []}
    remote_user = RemoteUser.query.get(remote_user_id)
    mark = track_num + 1
    track_num += 1
    server_remote_user = []
    for server_id in servers_id_list:
        s = ServerRemoteUser.query.filter_by(server=Server.query.get(server_id),
                                             remote_user=remote_user).first()
        if s:
            server_remote_user.append(s)
        else:
            result = False
            no_auth_server = Server.query.get(server_id)
            if trans_type == 'getfile':
                log = 'get {0} from {1} '.format(files, no_auth_server.ip)
                event_log = log + 'no auth user'
                msg["fail"].append(log)
                write_ops_tem(g.user, no_auth_server, '', event_type, event_log, result, mark)
            else:
                for f in files:
                    log = 'send {0} to {1} '.format(f, no_auth_server.ip)
                    event_log = log + 'no auth user'
                    msg['fail'].append(log)
                    write_ops_tem(g.user, no_auth_server, '', event_type, event_log, result, mark)

    uid = g.user.id
    user = User.query.get(uid)
    results = []
    pool = Pool(PROCESS_NUMBER)
    start_time = datetime.now()
    for s in server_remote_user:
        server = s.server
        results.append(pool.apply_async(ssh_file, args=(
            server, user, remote_user.remote_username, s.auth_password,
            files, remote_path, s.auth_type.name, trans_type, mark)))

    pool.close()
    pool.join()

    for r in results:
        message = r.get()
        print(message)
        msg['fail'].extend(message['fail'])
        msg['success'].extend(message['success'])
    finish_time = datetime.now()
    write_ops(g.user, remote_user, "", total_num,
              len(msg['success']), len(msg['fail']), start_time, finish_time, log_type, mark)
    return jsonify(msg)
示例#2
0
def execute_cmd():
    global track_num
    # pool.apply_async()
    servers_id_list = request.form.getlist('servers')
    remote_user_id = request.form.get('remote_user')
    command = request.form.get('command')
    servers_id_list = list(set(servers_id_list))

    # get auth and no_auth
    remote_user = RemoteUser.query.get(remote_user_id)
    server_remote_user = []
    event_type = 'CommandExcution'
    event_log = 'no auth in server'
    msg = {"success": [], "fail": []}
    mark = track_num + 1
    track_num += 1
    result = False
    for server_id in servers_id_list:
        s = ServerRemoteUser.query.filter_by(server=Server.query.get(server_id),
                                             remote_user=remote_user).first()
        if s:
            server_remote_user.append(s)
        else:
            no_auth_server = Server.query.get(server_id)
            msg['fail'].append(no_auth_server.ip)
            write_ops_tem(g.user, no_auth_server, command, event_type, event_log, result, mark)

    # multiprocessing to execute command
    results = []
    pool = Pool(PROCESS_NUMBER)
    uid = g.user.id
    user = User.query.get(uid)
    print(mark)
    for s in server_remote_user:
        server = s.server
        results.append(pool.apply_async(ssh_cmd, args=(
            server, user, remote_user.remote_username, s.auth_password,
            command, s.auth_type.name, mark)))
    pool.close()
    pool.join()

    # get the result
    total_num = len(servers_id_list)
    success_num = 0
    fail_num = total_num - len(server_remote_user)
    start_time = datetime.now()
    for r in results:
        message = r.get()
        print(message)
        if message[3]:
            success_num += 1
            msg['success'].append(message[0])
        else:
            fail_num += 1
            msg['fail'].append(message[0])

    finish_time = datetime.now()
    log_type = 'BatchRunCommand'
    write_ops(g.user, remote_user, command, total_num,
              success_num, fail_num, start_time, finish_time, log_type, mark)

    return jsonify(msg)