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 | # This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
32 | # available from http://www.virtualbox.org. This file is free software;
|
---|
33 | # you can redistribute it and/or modify it under the terms of the GNU
|
---|
34 | # General Public License (GPL) as published by the Free Software
|
---|
35 | # Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
36 | # VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
37 | # hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
38 | #
|
---|
39 | # Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
40 | # Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
41 | # additional information or have any questions.
|
---|
42 | #
|
---|
43 |
|
---|
44 | use strict;
|
---|
45 | use SOAP::Lite;
|
---|
46 | use vboxService;
|
---|
47 | use Data::Dumper;
|
---|
48 |
|
---|
49 | my $cmd = 'clienttest';
|
---|
50 | my $optMode;
|
---|
51 | my $vmname;
|
---|
52 |
|
---|
53 | while (my $this = shift(@ARGV))
|
---|
54 | {
|
---|
55 | if (($this =~ /^-h/) || ($this =~ /^--help/))
|
---|
56 | {
|
---|
57 | print "$cmd: test the VirtualBox web service.\n".
|
---|
58 | "Usage:\n".
|
---|
59 | " $cmd <mode>\n".
|
---|
60 | "with <mode> being one of 'version', 'list', 'start'; default is 'list'.\n".
|
---|
61 | " $cmd version: print version of VirtualBox web service.\n".
|
---|
62 | " $cmd list: list installed virtual machines.\n".
|
---|
63 | " $cmd startvm <vm>: start the virtual machine named <vm>.\n";
|
---|
64 | exit 0;
|
---|
65 | }
|
---|
66 | elsif ( ($this eq 'version')
|
---|
67 | || ($this eq 'list')
|
---|
68 | )
|
---|
69 | {
|
---|
70 | $optMode = $this;
|
---|
71 | }
|
---|
72 | elsif ($this eq 'startvm')
|
---|
73 | {
|
---|
74 | $optMode = $this;
|
---|
75 |
|
---|
76 | if (!($vmname = shift(@ARGV)))
|
---|
77 | {
|
---|
78 | die "[$cmd] Missing parameter: You must specify the name of the VM to start.\nStopped";
|
---|
79 | }
|
---|
80 | }
|
---|
81 | else
|
---|
82 | {
|
---|
83 | die "[$cmd] Unknown option \"$this\"; stopped";
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | $optMode = "list"
|
---|
88 | if (!$optMode);
|
---|
89 |
|
---|
90 | my $vbox = vboxService->IWebsessionManager_logon("test", "test");
|
---|
91 |
|
---|
92 | if (!$vbox)
|
---|
93 | {
|
---|
94 | die "[$cmd] Logon to session manager with user \"test\" and password \"test\" failed.\nStopped";
|
---|
95 | }
|
---|
96 |
|
---|
97 | if ($optMode eq "version")
|
---|
98 | {
|
---|
99 | my $v = vboxService->IVirtualBox_getVersion($vbox);
|
---|
100 | print "[$cmd] Version number of running VirtualBox web service: $v\n";
|
---|
101 | }
|
---|
102 | elsif ($optMode eq "list")
|
---|
103 | {
|
---|
104 | print "[$cmd] Listing machines:\n";
|
---|
105 | my $result = vboxService->IVirtualBox_getMachines($vbox);
|
---|
106 | foreach my $idMachine (@{$result->{'array'}})
|
---|
107 | {
|
---|
108 | my $if = vboxService->IManagedObjectRef_getInterfaceName($idMachine);
|
---|
109 | my $name = vboxService->IMachine_getName($idMachine);
|
---|
110 |
|
---|
111 | print "machine $if $idMachine: $name\n";
|
---|
112 | }
|
---|
113 | }
|
---|
114 | elsif ($optMode eq "startvm")
|
---|
115 | {
|
---|
116 | # assume it's a UUID
|
---|
117 | my $machine = vboxService->IVirtualBox_getMachine($vbox, $vmname);
|
---|
118 | if (!$machine)
|
---|
119 | {
|
---|
120 | # no: then try a name
|
---|
121 | $machine = vboxService->IVirtualBox_findMachine($vbox, $vmname);
|
---|
122 | }
|
---|
123 |
|
---|
124 | die "[$cmd] Cannot find VM \"$vmname\"; stopped"
|
---|
125 | if (!$machine);
|
---|
126 |
|
---|
127 | my $session = vboxService->IWebsessionManager_getSessionObject($vbox);
|
---|
128 | die "[$cmd] Cannot get session object; stopped"
|
---|
129 | if (!$session);
|
---|
130 |
|
---|
131 | my $uuid = vboxService->IMachine_getId($machine);
|
---|
132 | die "[$cmd] Cannot get uuid for machine; stopped"
|
---|
133 | if (!$uuid);
|
---|
134 |
|
---|
135 | print "[$cmd] UUID: $uuid\n";
|
---|
136 |
|
---|
137 | my $progress = vboxService->IVirtualBox_openRemoteSession($vbox,
|
---|
138 | $session,
|
---|
139 | $uuid,
|
---|
140 | "vrdp",
|
---|
141 | "");
|
---|
142 | die "[$cmd] Cannot open remote session; stopped"
|
---|
143 | if (!$progress);
|
---|
144 |
|
---|
145 | print("[$cmd] Waiting for the remote session to open...\n");
|
---|
146 | vboxService->IProgress_waitForCompletion($progress, -1);
|
---|
147 |
|
---|
148 | my $fCompleted;
|
---|
149 | $fCompleted = vboxService->IProgress_getCompleted($progress);
|
---|
150 | print("[$cmd] Completed: $fCompleted\n");
|
---|
151 |
|
---|
152 | my $resultCode;
|
---|
153 | $resultCode = vboxService->IProgress_getResultCode($progress);
|
---|
154 |
|
---|
155 | print("[$cmd] Result: $resultCode\n");
|
---|
156 |
|
---|
157 | vboxService->ISession_close($session);
|
---|
158 |
|
---|
159 | vboxService->IWebsessionManager_logoff($vbox);
|
---|
160 | }
|
---|