Ejemplo n.º 1
0
def api_device(id):
    content_type = request.headers.get('Content-Type')

    device = db_functions.db_get_device(id)
    device_json = jsonify(device)

    if content_type == "application/json":
        return device_json
    else:
        return render_template("api/index.html",
                               output_result=json.dumps(device_json.json,
                                                        indent=4))
Ejemplo n.º 2
0
def devices_edit(id):
    device = db_functions.db_get_device(id)
    form = DeviceEditForm(obj=device)

    if request.method == "GET":
        return render_template("devices/edit.html", form=form)
    else:
        if form.validate_on_submit():
            data = {
                "user": form.user.data,
                "ip_addr": form.ip_addr.data,
                "friendly_name": form.friendly_name.data,
                "vendor": form.vendor.data
            }

            db_functions.db_update_device(device, **data)
            flash("Device has been successfully updated!")
        return redirect(url_for('devices.devices_home'))
Ejemplo n.º 3
0
def api_device_get_hostname(id):
    content_type = request.headers.get('Content-Type')

    device = db_functions.db_get_device(id)
    device_user = db_functions.db_get_user(device.user)

    netmiko_session = netmiko_functions.NetmikoAPI(
        **{
            "device_type": device.vendor,
            "ip": device.ip_addr,
            "username": device_user.username,
            "password": device_user.password
        })

    output = netmiko_session.get_hostname()
    netmiko_json = jsonify(output)

    if content_type == "application/json":
        return netmiko_json
    else:
        return render_template("api/index.html",
                               output_result=json.dumps(netmiko_json.json,
                                                        indent=4))
Ejemplo n.º 4
0
def api_device_send_command(id):
    content_type = request.headers.get('Content-Type')
    if not content_type == "application/json":
        return jsonify({
            "error": True,
            "details": "Only application/json is accepted..."
        })

    if request.method == 'POST':
        if not request.json:
            abort(400)

        content = request.get_json()

        if not content["command_string"]:
            return jsonify({
                "error": True,
                "details": "command_string not found in body"
            })

        device = db_functions.db_get_device(id)
        device_user = db_functions.db_get_user(device.user)

        netmiko_session = netmiko_functions.NetmikoAPI(
            **{
                "device_type": device.vendor,
                "ip": device.ip_addr,
                "username": device_user.username,
                "password": device_user.password
            })

        output = netmiko_session.send_command(**content)

        json_output = jsonify({"error": False, "send_command": output})

        return json_output
Ejemplo n.º 5
0
def devices_view(id):
    device = db_functions.db_get_device(id)
    return render_template("devices/view.html", device=device)