From 9518a9787310d47142b7877f058769c4b9d5016c Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Thu, 3 Dec 2020 15:55:11 +0100 Subject: [PATCH] examples: add CLI like example for easier re-use with non-local host Signed-off-by: Thomas Lamprecht --- examples/example4.pl | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100755 examples/example4.pl diff --git a/examples/example4.pl b/examples/example4.pl new file mode 100755 index 0000000..f5fce5d --- /dev/null +++ b/examples/example4.pl @@ -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 []\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}); -- 2.39.2