Sample Scripts

Sample Scripts

Perl example:


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 Satellite channel on http://rhn.redhat.com.

#!/usr/bin/perl
use Frontier::Client;

my $HOST = 'satellite.example.com';
my $user = 'username';
my $pass = 'password';

my $client = new Frontier::Client(url => "http://$HOST/rpc/api");
my $session = $client->call('auth.login',$user, $pass);

my $systems = $client->call('system.listUserSystems', $session);
foreach my $system (@$systems) {
   print $system->{'name'}."\n";
}
$client->call('auth.logout', $session);


Python example:


Below is an example of the user.listUsers call being used. Only the name of each group is printed.

#!/usr/bin/python
import xmlrpclib

SATELLITE_URL = "http://satellite.example.com/rpc/api"
SATELLITE_LOGIN = "username"
SATELLITE_PASSWORD = "password"

client = xmlrpclib.Server(SATELLITE_URL, verbose=0)

key = client.auth.login(SATELLITE_LOGIN, SATELLITE_PASSWORD)
list = client.user.list_users(key)
for user in list:
   print user.get('login')

client.auth.logout(key)

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.

#!/usr/bin/python
from datetime import datetime
import time
import xmlrpclib

SATELLITE_URL = "http://satellite.example.com/rpc/api"
SATELLITE_LOGIN = "username"
SATELLITE_PASSWORD = "password"

client = xmlrpclib.Server(SATELLITE_URL, verbose=0)

key = client.auth.login(SATELLITE_LOGIN, SATELLITE_PASSWORD)
package_list = client.packages.findByNvrea(key, 'rhnlib', '2.5.22', '9.el6', '', 'noarch')
today = datetime.today()
earliest_occurrence = xmlrpclib.DateTime(today)
client.system.schedulePackageInstall(key, 1000000001, package_list[0]['id'], earliest_occurrence)

client.auth.logout(key)


Ruby example:


Below is an example of the channel.listAllChannels API call. List of channel labels is printed.

#!/usr/bin/ruby
require "xmlrpc/client"

@SATELLITE_URL = "http://satellite.example.com/rpc/api"
@SATELLITE_LOGIN = "username"
@SATELLITE_PASSWORD = "password"

@client = XMLRPC::Client.new2(@SATELLITE_URL)

@key = @client.call('auth.login', @SATELLITE_LOGIN, @SATELLITE_PASSWORD)
channels = @client.call('channel.listAllChannels', @key)
for channel in channels do
   p channel["label"]
end

@client.call('auth.logout', @key)


For more examples visit the Spacewalk community page.