VirtualBox

source: vbox/trunk/src/VBox/Main/webservice/samples/perl/clienttest.pl@ 18886

Last change on this file since 18886 was 18694, checked in by vboxsync, 16 years ago

SDK licensing

  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
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. Please refer to the SDK
6# programming reference (SDKRef.pdf) for how to use this sample.
7#
8# Copyright (C) 2008-2009 Sun Microsystems, Inc.
9#
10# The following license applies to this file only:
11#
12# Permission is hereby granted, free of charge, to any person
13# obtaining a copy of this software and associated documentation
14# files (the "Software"), to deal in the Software without
15# restriction, including without limitation the rights to use,
16# copy, modify, merge, publish, distribute, sublicense, and/or
17# sell copies of the Software, and to permit persons to whom the
18# Software is furnished to do so, subject to the following conditions:
19#
20# The above copyright notice and this permission notice shall be
21# included in all copies or substantial portions of the Software.
22#
23# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
25# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
27# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
28# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30# OTHER DEALINGS IN THE SOFTWARE.
31#
32
33use strict;
34use SOAP::Lite;
35use vboxService; # generated by stubmaker, see SDKRef.pdf
36use Data::Dumper;
37
38my $cmd = 'clienttest';
39my $optMode;
40my $vmname;
41
42while (my $this = shift(@ARGV))
43{
44 if (($this =~ /^-h/) || ($this =~ /^--help/))
45 {
46 print "$cmd: test the VirtualBox web service.\n".
47 "Usage:\n".
48 " $cmd <mode>\n".
49 "with <mode> being one of 'version', 'list', 'start'; default is 'list'.\n".
50 " $cmd version: print version of VirtualBox web service.\n".
51 " $cmd list: list installed virtual machines.\n".
52 " $cmd startvm <vm>: start the virtual machine named <vm>.\n";
53 exit 0;
54 }
55 elsif ( ($this eq 'version')
56 || ($this eq 'list')
57 )
58 {
59 $optMode = $this;
60 }
61 elsif ($this eq 'startvm')
62 {
63 $optMode = $this;
64
65 if (!($vmname = shift(@ARGV)))
66 {
67 die "[$cmd] Missing parameter: You must specify the name of the VM to start.\nStopped";
68 }
69 }
70 else
71 {
72 die "[$cmd] Unknown option \"$this\"; stopped";
73 }
74}
75
76$optMode = "list"
77 if (!$optMode);
78
79my $vbox = vboxService->IWebsessionManager_logon("test", "test");
80
81if (!$vbox)
82{
83 die "[$cmd] Logon to session manager with user \"test\" and password \"test\" failed.\nStopped";
84}
85
86if ($optMode eq "version")
87{
88 my $v = vboxService->IVirtualBox_getVersion($vbox);
89 print "[$cmd] Version number of running VirtualBox web service: $v\n";
90}
91elsif ($optMode eq "list")
92{
93 print "[$cmd] Listing machines:\n";
94 my @result = vboxService->IVirtualBox_getMachines2($vbox);
95 foreach my $idMachine (@result)
96 {
97 my $if = vboxService->IManagedObjectRef_getInterfaceName($idMachine);
98 my $name = vboxService->IMachine_getName($idMachine);
99
100 print "machine $if $idMachine: $name\n";
101 }
102}
103elsif ($optMode eq "startvm")
104{
105 # assume it's a UUID
106 my $machine = vboxService->IVirtualBox_getMachine($vbox, $vmname);
107 if (!$machine)
108 {
109 # no: then try a name
110 $machine = vboxService->IVirtualBox_findMachine($vbox, $vmname);
111 }
112
113 die "[$cmd] Cannot find VM \"$vmname\"; stopped"
114 if (!$machine);
115
116 my $session = vboxService->IWebsessionManager_getSessionObject($vbox);
117 die "[$cmd] Cannot get session object; stopped"
118 if (!$session);
119
120 my $uuid = vboxService->IMachine_getId($machine);
121 die "[$cmd] Cannot get uuid for machine; stopped"
122 if (!$uuid);
123
124 print "[$cmd] UUID: $uuid\n";
125
126 my $progress = vboxService->IVirtualBox_openRemoteSession($vbox,
127 $session,
128 $uuid,
129 "vrdp",
130 "");
131 die "[$cmd] Cannot open remote session; stopped"
132 if (!$progress);
133
134 print("[$cmd] Waiting for the remote session to open...\n");
135 vboxService->IProgress_waitForCompletion($progress, -1);
136
137 my $fCompleted;
138 $fCompleted = vboxService->IProgress_getCompleted($progress);
139 print("[$cmd] Completed: $fCompleted\n");
140
141 my $resultCode;
142 $resultCode = vboxService->IProgress_getResultCode($progress);
143
144 print("[$cmd] Result: $resultCode\n");
145
146 vboxService->ISession_close($session);
147
148 vboxService->IWebsessionManager_logoff($vbox);
149}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette