Manage RT from command line

To create a command line script to make someting in RT you have to call initialization methods:

  • CleanEnv – clean out user’s environment variables: PATH, ENV, SHELL, CDPATH, IFS.
  • LoadConfig – loads config. Basically do just use config;.
  • DBConnect – calls RT::Init() which sets connection to the database, creates two users – system user and nobody and initializes lgging system.

Each RT (except RT::Users and RT::CurrentUser) class needs to have a user object as parameter in its constructor:
my $ticket = new RT::Ticket($curr_user);
To get an user object use CLI method GetCurrentUser:
my $curr_user = GetCurrentUser();
It uses Perl function getpwuid(UID) which returns a record from /etc/passwd according to real user ID (UID). UID can be retrieved from special Perl variable $< . So, you’re lucky if you have the same user name in RT and on the computer where the script will be run. Otherwise you should load the user object directly:
my $curr_user = new RT::CurrentUser('some_user_name');

Let’s put all them together:

#!/usr/bin/perl -w
use strict;
use warnings;
use lib ("/opt/rt2/lib", "/opt/rt2/etc");
use RT::Tickets;
use RT::Interface::CLI  qw(CleanEnv LoadConfig 
                         DBConnect GetCurrentUser);

#Clean out environment
CleanEnv();

#Load etc/config.pm
LoadConfig();

#Connect to the database
DBConnect();

my $curr_user = GetCurrentUser();

#
# Do something
#

# Disconnet from the database
$RT::Handle->Disconnect();

One response to “Manage RT from command line”

  1. […] r | 
    About | 
    Lite

    « Manage RT from command line Howto get ticket body […]

Leave a Reply

Your email address will not be published. Required fields are marked *