示例#1
0
def do_webhook_shell(webhook_id, histroy_id, data):
    webhook = WebHook.query.get(webhook_id)
    history = History.query.get(histroy_id)
    # server information
    ip = webhook.server.ip
    port = webhook.server.port
    account = webhook.server.account
    pkey = webhook.server.pkey

    # do what
    shell = webhook.shell

    # start to process, add history into database
    status = '2'
    history.updateStatus(status)
    webhook.updateStatus(status)
    try:
        success, log = SshUtil.do_ssh_cmd(ip, port, account, pkey, shell,
                                          JsonUtil.object_2_json(data))
        status = '3'  # error
        if success:
            status = '4'  # success
    except Exception, e:
        success, log = False, 'Server SSH error: ' + str(e)
        status = '5'  # except
示例#2
0
def api_server_new(ip, port, account, pkey, name=None, id=None):
    # login user
    user_id = RequestUtil.get_login_user().get('id', '')
    server_id = id
    name = name if name else ip

    try:
        success, log = SshUtil.do_ssh_cmd(
            ip, port, account, pkey, 'ls -lh', timeout=5)
        if success:
            if server_id:
                # update webhook
                # you can only update the webhook which you create.
                server = Server.query.filter_by(
                    id=server_id, user_id=user_id).first()
                if not server:
                    return ResponseUtil.standard_response(
                        0, 'Server is not exist!')
                server.ip = ip
                server.port = port
                server.account = account
                server.pkey = pkey
                server.name = name
            else:
                server = Server(ip=ip, port=port, account=account, pkey=pkey,
                                user_id=user_id, name=name)

            server.save()

            return ResponseUtil.standard_response(
                1, server.dict(with_pkey=True))
    except Exception as e:
        print(e)
    return ResponseUtil.standard_response(0, 'Server SSH connect error!')
示例#3
0
def api_server_new():
    # login user
    user_id = RequestUtil.get_login_user().get('id', '')

    ip = RequestUtil.get_parameter('ip', '')
    name = RequestUtil.get_parameter('name', ip)
    name = name and name or ip
    port = RequestUtil.get_parameter('port', 22)
    account = RequestUtil.get_parameter('account', '')
    pkey = RequestUtil.get_parameter('pkey', '')

    if not all((ip, name, port, account, pkey)):
        return ResponseUtil.standard_response(0, 'Form data can not be blank!')

    try:
        success, log = SshUtil.do_ssh_cmd(ip,
                                          port,
                                          account,
                                          pkey,
                                          'ls -lh',
                                          timeout=5)
        if success:
            server = Server(ip=ip,
                            port=port,
                            account=account,
                            pkey=pkey,
                            user_id=user_id,
                            name=name)

            server.save()

            return ResponseUtil.standard_response(1, server.dict())
    except Exception, e:
        print e
示例#4
0
def test_ssh():
    kwargs = dict(ip='127.0.0.1',
                  port='22',
                  account='root',
                  pkey=RSA_PRIVATE_KEY,
                  shell='echo hello\n echo hello')
    with patch_ssh('', ''):
        success, log = ssh.do_ssh_cmd(**kwargs)
        assert success
    with patch_ssh('', 'message'):
        success, log = ssh.do_ssh_cmd(**kwargs)
        assert not success
        assert 'message' in log
    msg = ('fatal: Not a git repository '
           '(or any parent up to mount point /tmp)\n'
           'Stopping at filesystem boundary '
           '(GIT_DISCOVERY_ACROSS_FILESYSTEM not set).')
    with patch_ssh(msg, ''):
        success, log = ssh.do_ssh_cmd(**kwargs)
        assert not success
        assert msg in log
示例#5
0
def api_server_new():
    # login user
    user_id = RequestUtil.get_login_user().get('id', '')

    ip = RequestUtil.get_parameter('ip', '')
    name = RequestUtil.get_parameter('name', ip)
    name = name and name or ip
    port = RequestUtil.get_parameter('port', 22)
    account = RequestUtil.get_parameter('account', '')
    pkey = RequestUtil.get_parameter('pkey', '')

    if not all((ip, name, port, account, pkey)):
        return ResponseUtil.standard_response(0, 'Form data can not be blank!')

    try:
        success, log = SshUtil.do_ssh_cmd(ip,
                                          port,
                                          account,
                                          pkey,
                                          'ls -lh',
                                          timeout=5)
        if success:
            server_id = RequestUtil.get_parameter('id', '')
            if server_id:
                # update webhook
                # you can only update the webhook which you create.
                server = Server.query.filter_by(id=server_id,
                                                user_id=user_id).first()
                if not server:
                    return ResponseUtil.standard_response(
                        0, 'Server is not exist!')
                server.ip = ip
                server.port = port
                server.account = account
                server.pkey = pkey
                server.name = name
            else:
                server = Server(ip=ip,
                                port=port,
                                account=account,
                                pkey=pkey,
                                user_id=user_id,
                                name=name)

            server.save()

            return ResponseUtil.standard_response(1,
                                                  server.dict(with_pkey=True))
    except Exception as e:
        print(e)
    return ResponseUtil.standard_response(0, 'Server SSH connect error!')
示例#6
0
def do_webhook_shell(webhook_id, history_id, data):
    webhook = WebHook.query.get(webhook_id)
    history = History.query.get(history_id)
    # server information
    ip = webhook.server.ip
    port = webhook.server.port
    account = webhook.server.account
    pkey = webhook.server.pkey

    # do what
    shell = webhook.shell

    # start to process, add history into database
    status = '2'
    history.push_user = '******' % (
        HookDataParse.get_push_name(data)
        or 'WebHook', HookDataParse.get_push_email(data) or 'Web GUI')
    history.updateStatus(status)
    webhook.updateStatus(status)
    try:
        success, log = SshUtil.do_ssh_cmd(ip, port, account, pkey, shell,
                                          JsonUtil.object_2_json(data))
        status = '3'  # error
        if success:
            status = '4'  # success
    except Exception as e:
        success, log = False, 'Server SSH error: ' + str(e)
        status = '5'  # except

    # ProgrammingError: You must not use 8-bit bytestrings unless you use a
    # text_factory
    log = unicode(log)  # noqa

    history.status = status
    history.update_time = datetime.datetime.now()
    history.shell_log = log
    history.save()

    webhook.updateStatus(status)