]> git.proxmox.com Git - pve-apiclient.git/commitdiff
examples: add CLI like example for easier re-use with non-local host
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Thu, 3 Dec 2020 14:55:11 +0000 (15:55 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Thu, 3 Dec 2020 14:55:11 +0000 (15:55 +0100)
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
examples/example4.pl [new file with mode: 0755]

diff --git a/examples/example4.pl b/examples/example4.pl
new file mode 100755 (executable)
index 0000000..f5fce5d
--- /dev/null
@@ -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});