]> git.proxmox.com Git - pve-apiclient.git/blob - examples/example4.pl
examples: add CLI like example for easier re-use with non-local host
[pve-apiclient.git] / examples / example4.pl
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
6 use strict;
7 use warnings;
8
9 use JSON;
10
11 use PVE::APIClient::LWP;
12
13 sub 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
22 my $host = shift || usage();
23 my $user = shift || $ENV{'PMX_CLIENT_USER'} || 'root@pam';
24 my $pass = $ENV{'PMX_CLIENT_PASSWORD'} || usage();
25
26 my $fps = {};
27
28 if (my $fp = $ENV{'PMX_CLIENT_FINGERPRINT'}) {
29 $fps->{$fp} = 1;
30 }
31
32 my $conn = PVE::APIClient::LWP->new(
33 username => $user,
34 password => $pass,
35 host => $host,
36 cached_fingerprints => $fps,
37 );
38
39 my $res = $conn->get("api2/json/version", {});
40 print to_json($res, { pretty => 1, canonical => 1, utf8 => 1});