]> git.proxmox.com Git - pve-apiclient.git/blame_incremental - examples/example4.pl
examples: add CLI like example for easier re-use with non-local host
[pve-apiclient.git] / examples / example4.pl
... / ...
CommitLineData
1#!/usr/bin/perl
2
3# NOTE: you need to run this on a PVE host, or modify the source to
4# provide username/password/hostname from somewhere else.
5
6use strict;
7use warnings;
8
9use JSON;
10
11use PVE::APIClient::LWP;
12
13sub usage {
14 print STDERR "usage: $0 <host> [<user>]\n";
15 print STDERR "\n";
16 print STDERR "Pass password in PMX_CLIENT_PASSWORD env. variable\n";
17 print STDERR "User is either CLI argument, PMX_CLIENT_USER env. variable or 'root\@pam'\n";
18 print STDERR "Pass PMX_CLIENT_FINGERPRINT env. variable for self-signed certificates.";
19 exit(1);
20}
21
22my $host = shift || usage();
23my $user = shift || $ENV{'PMX_CLIENT_USER'} || 'root@pam';
24my $pass = $ENV{'PMX_CLIENT_PASSWORD'} || usage();
25
26my $fps = {};
27
28if (my $fp = $ENV{'PMX_CLIENT_FINGERPRINT'}) {
29 $fps->{$fp} = 1;
30}
31
32my $conn = PVE::APIClient::LWP->new(
33 username => $user,
34 password => $pass,
35 host => $host,
36 cached_fingerprints => $fps,
37);
38
39my $res = $conn->get("api2/json/version", {});
40print to_json($res, { pretty => 1, canonical => 1, utf8 => 1});