Git Product home page Git Product logo

proxmox-class-api-login-pass's Introduction

Proxmox <-> PHP <-> API[username+password]

Request to proxmox api with username and password. No need for api token.

Directories and files

.
├── class
│   └── Proxmox.php
└── index.php

.index.php

<?php
include_once('./class/Proxmox.php');

$proxmox = new Proxmox;
$proxmox->setPM_IP($_GET['PM_IP']);
$proxmox->setPM_PORT($_GET['PM_PORT']);
$proxmox->setPM_USER($_GET['PM_USER']);
$proxmox->setPM_PASS($_GET['PM_PASS']);
$proxmox->PM_URL = $_GET['PM_URL'];

// login => request => logout
$proxmox->login();
echo ($proxmox->reqPrxmox());

.class/Proxmox.php

<?php
class Proxmox
{
    private $PM_IP;
    private $PM_PORT;
    private $PM_USER;
    private $PM_PASS;
    public $PM_URL;
    public $token;
    public $cookie;

    public function  setPM_IP($v)
    {
        $this->PM_IP = $v;
        return $this;
    }

    public function  setPM_PORT($v)
    {
        $this->PM_PORT = $v;
        return $this;
    }

    public function  setPM_USER($v)
    {
        $this->PM_USER = $v;
        return $this;
    }

    public function  setPM_PASS($v)
    {
        $this->PM_PASS = $v;
        return $this;
    }

    public function  getPM_ADDRESS()
    {
        $this->PM_ADDRESS = "https://$this->PM_IP:$this->PM_PORT/api2/json/";
        return $this->PM_ADDRESS;
    }

    public function getPM_FIELDS()
    {
        return "username=$this->PM_USER&password=$this->PM_PASS";
    }

    public function login()
    {
        global $proxmox;
        $ch = curl_init();
        curl_setopt_array($ch, [
            CURLOPT_URL => $proxmox->getPM_ADDRESS() . "access/ticket",
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HTTPHEADER => ["Content-Type: application/x-www-form-urlencoded"],
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_POSTFIELDS => $proxmox->getPM_FIELDS(),
        ]);
        $res = curl_exec($ch);
        curl_close($ch);
        $proxmox->token = json_decode($res)->data->CSRFPreventionToken;
        $proxmox->cookie = json_decode($res)->data->ticket;
    }

    public function reqPrxmox()
    {
        global $proxmox;
        $ch = curl_init();
        curl_setopt_array($ch, [
            CURLOPT_URL => $proxmox->getPM_ADDRESS() . $proxmox->PM_URL,
            CURLOPT_CUSTOMREQUEST => "GET",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HTTPHEADER => ["CSRFPreventionToken=$this->token"],
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_COOKIE => "PVEAuthCookie=$this->cookie"
        ]);
        $res = curl_exec($ch);
        curl_close($ch);
        return $res;
    }
}

How to use?

Access your web server and install git

apt install git

Access the directory "/var/www/"

cd /var/www/

Clone this project into the 'proxmox' folder

git clone https://github.com/Full-Monitoring/proxmox-class-api-login-pass.git proxmox
# open proxmox
cd proxmox/
# list files
ls -la

Request example using get method

To get the version

http://127.0.0.1/proxmox?PM_URL=version&PM_IP=172.33.255.2&PM_PORT=8006&PM_USER=roo@pam&PM_PASS=12345678

To get cluster

http://127.0.0.1/proxmox?PM_URL=cluster&PM_IP=172.33.255.2&PM_PORT=8006&PM_USER=roo@pam&PM_PASS=12345678

To get cluster/resources

http://127.0.0.1/proxmox?PM_URL=cluster/resources&PM_IP=172.33.255.2&PM_PORT=8006&PM_USER=roo@pam&PM_PASS=12345678

proxmox-class-api-login-pass's People

Contributors

saulotarsobc avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.