<html>
<head>
  <meta http-equiv="cache-control" content="no-cache" />
<style type="text/css">
ul.apidoc {
   list-style-image: url('/img/parent_node.gif');
}
</style>

</head>
<body>
<h1><i class="fa fa-gears"></i>Sample Scripts</h1>
<h2>Sample Scripts</h2>
<h3>Perl example:</h3><BR>
This Perl example shows the system.listUserSystems call being used to get a list of systems a user has access to.  In the example below, the name of each system will be printed.
The Frontier::Client Perl module can be found in the "perl-Frontier-RPC" rpm contained in the newest SUSE Manager channel.
<p />

<code>
#!/usr/bin/perl<br/>
use Frontier::Client;<br />
<br />
my $HOST = 'manager.example.com';<br />
my $user = 'username';<br />
my $pass = 'password';<br />
<br />
my $client = new Frontier::Client(url => "http://$HOST/rpc/api");<br />
my $session = $client->call('auth.login',$user, $pass);<br />
<br />
my $systems = $client->call('system.listUserSystems', $session);<br />
foreach my $system (@$systems) {<br />
&nbsp;&nbsp;&nbsp;print $system->{'name'}."\n";<br />
}<br />
$client->call('auth.logout', $session);</br>
</code>
<hr />
<p />
<h3>Python example:</h3><br />
Below is an example of the user.listUsers call being used.  Only the login of each user is printed. 
<p />

<code>
#!/usr/bin/python<br />
import xmlrpclib<br />
<br />
MANAGER_URL = "http://manager.example.com/rpc/api"<br />
MANAGER_LOGIN = "username"<br />
MANAGER_PASSWORD = "password"<br />
<br />
client = xmlrpclib.Server(MANAGER_URL, verbose=0)<br />
<br />
key = client.auth.login(MANAGER_LOGIN, MANAGER_PASSWORD)<br />
list = client.user.list_users(key)<br />
for user in list:<br />
&nbsp;&nbsp;&nbsp;print user.get('login')<br />
<br />
client.auth.logout(key)<br />
</code>
<p />
The following code shows how to use date-time parameters. This code will schedule
immediate installation of package rhnlib-2.5.22.9.el6.noarch to system with
id 1000000001.
<p />

<code>
#!/usr/bin/python<br />
from datetime import datetime<br />
import time<br />
import xmlrpclib<br />
<br />
MANAGER_URL = "http://manager.example.com/rpc/api"<br />
MANAGER_LOGIN = "username"<br />
MANAGER_PASSWORD = "password"<br />
<br />
client = xmlrpclib.Server(MANAGER_URL, verbose=0)<br />
<br />
key = client.auth.login(MANAGER_LOGIN, MANAGER_PASSWORD)<br />
package_list = client.packages.findByNvrea(key, 'rhnlib', '2.5.22', '9.el6', '', 'noarch')<br />
today = datetime.today()<br />
earliest_occurrence = xmlrpclib.DateTime(today)<br />
client.system.schedulePackageInstall(key, 1000000001, package_list[0]['id'],
earliest_occurrence)<br />
<br />
client.auth.logout(key)<br />
</code>
<p />
<hr />

<p />
<h3>Ruby example:</h3><br />
Below is an example of the channel.listAllChannels API call. List of channel labels is printed.
<p />

<code>
#!/usr/bin/env ruby<br />
require "xmlrpc/client"<br />
<br />
@MANAGER_URL = "http://manager.example.com/rpc/api"<br />
@MANAGER_LOGIN = "username"<br />
@MANAGER_PASSWORD = "password"<br />
<br />
@client = XMLRPC::Client.new2(@MANAGER_URL)<br />
<br />
@key = @client.call('auth.login', @MANAGER_LOGIN, @MANAGER_PASSWORD)<br />
channels = @client.call('channel.listAllChannels', @key)<br />
for channel in channels do<br />
&nbsp;&nbsp;&nbsp;p channel["label"]<br />
end<br />
<br />
@client.call('auth.logout', @key)<br />
</code>
<hr />

For more examples visit the <a href="https://fedorahosted.org/spacewalk/">Spacewalk community page</a>.

<p />
</body><p />
</html><p />
