1 | #!/usr/bin/perl
|
---|
2 |
|
---|
3 | #
|
---|
4 | # This little perl program attempts to connect to a running VirtualBox
|
---|
5 | # webservice and calls various methods on it.
|
---|
6 | #
|
---|
7 | # To get this to run:
|
---|
8 | #
|
---|
9 | # 0) If not yet among perl's modules, install SOAP::Lite. Users of debian
|
---|
10 | # based systems might try 'sudo apt-get install libsoap-lite-perl'.
|
---|
11 | #
|
---|
12 | # 1) In this directory, run
|
---|
13 | # stubmaker file:///path/to/sdk/bindings/webservice/vboxwebService.wsdl
|
---|
14 | # Note: the command is named stubmaker.pl on some systems.
|
---|
15 | # stubmaker should be installed on your system if you have SOAP::Lite and
|
---|
16 | # will, after a little while of thinking, create a vboxService.pm
|
---|
17 | # file in the current directory, which the "use" statement below
|
---|
18 | # then includes.
|
---|
19 | #
|
---|
20 | # (SOAP::Lite supports parsing the WSDL file on every run of
|
---|
21 | # the script, but it takes up to a minute to do so, hence the external
|
---|
22 | # variant via stubmaker.pl here.)
|
---|
23 | #
|
---|
24 | # 2) Start vboxwebsrv.
|
---|
25 | #
|
---|
26 | # 3) Run this script.
|
---|
27 | #
|
---|
28 | #
|
---|
29 | # Copyright (C) 2006-2009 Sun Microsystems, Inc.
|
---|
30 | #
|
---|
31 | # All rights reserved
|
---|
32 | #
|
---|
33 |
|
---|
34 | use strict;
|
---|
35 | use SOAP::Lite;
|
---|
36 | use vboxService;
|
---|
37 | use Data::Dumper;
|
---|
38 |
|
---|
39 | my $cmd = 'clienttest';
|
---|
40 | my $optMode;
|
---|
41 | my $vmname;
|
---|
42 |
|
---|
43 | while (my $this = shift(@ARGV))
|
---|
44 | {
|
---|
45 | if (($this =~ /^-h/) || ($this =~ /^--help/))
|
---|
46 | {
|
---|
47 | print "$cmd: test the VirtualBox web service.\n".
|
---|
48 | "Usage:\n".
|
---|
49 | " $cmd <mode>\n".
|
---|
50 | "with <mode> being one of 'version', 'list', 'start'; default is 'list'.\n".
|
---|
51 | " $cmd version: print version of VirtualBox web service.\n".
|
---|
52 | " $cmd list: list installed virtual machines.\n".
|
---|
53 | " $cmd startvm <vm>: start the virtual machine named <vm>.\n";
|
---|
54 | exit 0;
|
---|
55 | }
|
---|
56 | elsif ( ($this eq 'version')
|
---|
57 | || ($this eq 'list')
|
---|
58 | )
|
---|
59 | {
|
---|
60 | $optMode = $this;
|
---|
61 | }
|
---|
62 | elsif ($this eq 'startvm')
|
---|
63 | {
|
---|
64 | $optMode = $this;
|
---|
65 |
|
---|
66 | if (!($vmname = shift(@ARGV)))
|
---|
67 | {
|
---|
68 | die "[$cmd] Missing parameter: You must specify the name of the VM to start.\nStopped";
|
---|
69 | }
|
---|
70 | }
|
---|
71 | else
|
---|
72 | {
|
---|
73 | die "[$cmd] Unknown option \"$this\"; stopped";
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | $optMode = "list"
|
---|
78 | if (!$optMode);
|
---|
79 |
|
---|
80 | my $vbox = vboxService->IWebsessionManager_logon("test", "test");
|
---|
81 |
|
---|
82 | if (!$vbox)
|
---|
83 | {
|
---|
84 | die "[$cmd] Logon to session manager with user \"test\" and password \"test\" failed.\nStopped";
|
---|
85 | }
|
---|
86 |
|
---|
87 | if ($optMode eq "version")
|
---|
88 | {
|
---|
89 | my $v = vboxService->IVirtualBox_getVersion($vbox);
|
---|
90 | print "[$cmd] Version number of running VirtualBox web service: $v\n";
|
---|
91 | }
|
---|
92 | elsif ($optMode eq "list")
|
---|
93 | {
|
---|
94 | print "[$cmd] Listing machines:\n";
|
---|
95 | my $result = vboxService->IVirtualBox_getMachines($vbox);
|
---|
96 | foreach my $idMachine (@{$result->{'array'}})
|
---|
97 | {
|
---|
98 | my $if = vboxService->IManagedObjectRef_getInterfaceName($idMachine);
|
---|
99 | my $name = vboxService->IMachine_getName($idMachine);
|
---|
100 |
|
---|
101 | print "machine $if $idMachine: $name\n";
|
---|
102 | }
|
---|
103 | }
|
---|
104 | elsif ($optMode eq "startvm")
|
---|
105 | {
|
---|
106 | # assume it's a UUID
|
---|
107 | my $machine = vboxService->IVirtualBox_getMachine($vbox, $vmname);
|
---|
108 | if (!$machine)
|
---|
109 | {
|
---|
110 | # no: then try a name
|
---|
111 | $machine = vboxService->IVirtualBox_findMachine($vbox, $vmname);
|
---|
112 | }
|
---|
113 |
|
---|
114 | die "[$cmd] Cannot find VM \"$vmname\"; stopped"
|
---|
115 | if (!$machine);
|
---|
116 |
|
---|
117 | my $session = vboxService->IWebsessionManager_getSessionObject($vbox);
|
---|
118 | die "[$cmd] Cannot get session object; stopped"
|
---|
119 | if (!$session);
|
---|
120 |
|
---|
121 | my $uuid = vboxService->IMachine_getId($machine);
|
---|
122 | die "[$cmd] Cannot get uuid for machine; stopped"
|
---|
123 | if (!$uuid);
|
---|
124 |
|
---|
125 | print "[$cmd] UUID: $uuid\n";
|
---|
126 |
|
---|
127 | my $progress = vboxService->IVirtualBox_openRemoteSession($vbox,
|
---|
128 | $session,
|
---|
129 | $uuid,
|
---|
130 | "vrdp",
|
---|
131 | "");
|
---|
132 | die "[$cmd] Cannot open remote session; stopped"
|
---|
133 | if (!$progress);
|
---|
134 |
|
---|
135 | print("[$cmd] Waiting for the remote session to open...\n");
|
---|
136 | vboxService->IProgress_waitForCompletion($progress, -1);
|
---|
137 |
|
---|
138 | my $fCompleted;
|
---|
139 | $fCompleted = vboxService->IProgress_getCompleted($progress);
|
---|
140 | print("[$cmd] Completed: $fCompleted\n");
|
---|
141 |
|
---|
142 | my $resultCode;
|
---|
143 | $resultCode = vboxService->IProgress_getResultCode($progress);
|
---|
144 |
|
---|
145 | print("[$cmd] Result: $resultCode\n");
|
---|
146 |
|
---|
147 | vboxService->ISession_close($session);
|
---|
148 |
|
---|
149 | vboxService->IWebsessionManager_logoff($vbox);
|
---|
150 | }
|
---|