examples: add CLI like example for easier re-use with non-local host

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2020-12-03 15:55:11 +01:00
parent 18857a21bc
commit 9518a97873

40
examples/example4.pl Executable file
View File

@ -0,0 +1,40 @@
#!/usr/bin/perl
# NOTE: you need to run this on a PVE host, or modify the source to
# provide username/password/hostname from somewhere else.
use strict;
use warnings;
use JSON;
use PVE::APIClient::LWP;
sub usage {
print STDERR "usage: $0 <host> [<user>]\n";
print STDERR "\n";
print STDERR "Pass password in PMX_CLIENT_PASSWORD env. variable\n";
print STDERR "User is either CLI argument, PMX_CLIENT_USER env. variable or 'root\@pam'\n";
print STDERR "Pass PMX_CLIENT_FINGERPRINT env. variable for self-signed certificates.";
exit(1);
}
my $host = shift || usage();
my $user = shift || $ENV{'PMX_CLIENT_USER'} || 'root@pam';
my $pass = $ENV{'PMX_CLIENT_PASSWORD'} || usage();
my $fps = {};
if (my $fp = $ENV{'PMX_CLIENT_FINGERPRINT'}) {
$fps->{$fp} = 1;
}
my $conn = PVE::APIClient::LWP->new(
username => $user,
password => $pass,
host => $host,
cached_fingerprints => $fps,
);
my $res = $conn->get("api2/json/version", {});
print to_json($res, { pretty => 1, canonical => 1, utf8 => 1});