5

I am trying to create a script to batch mark a group of users as privileged in RT. I found a script on the RT wiki for adding users to a group and giving them the privileged status, then removed the bits of it to do with adding to a group. The perl script I have remaining is:

#!/usr/bin/perl
# Usage: ./rt_set_privileged.pl <username>

use strict;
use lib "/var/www/ticket.ourcompany.com/lib";
use RT;
use RT::User;
use RT::Interface::CLI;

RT::LoadConfig();
RT::Init();

# Create RT User Object
my $user = new RT::User($RT::SystemUser);

# Instantiate the user object with the user passed as parameter
my $usertoadd = $ARGV[0];
$user->Load( $usertoadd );

# Set the privileged flag (1=privileged, 0=unprivileged)
$user->SetPrivileged(1);

exit 1

I have the users in a file, one username per line. I don't know perl as of yet, so I tried to create a little bash script to loop through the file and run the perl script once per name. The Bash script as it looks right now:

#!/bin/bash

touch commands.sh
cat usernames.txt | while read LINE ; do
        N=$((N+1))
        echo /home/chris/RT/bin/rt_set_privileged.pl \"$LINE\" >> commands.sh
        /home/chris/RT/bin/rt_set_privileged.pl \"$LINE\"
        perl /home/chris/RT/bin/rt_set_privileged.pl \"$LINE\"
        perl -w /home/chris/RT/bin/rt_set_privileged.pl \"$LINE\"
        eval /home/chris/RT/bin/rt_set_privileged.pl \"$LINE\"
        perl "/home/chris/RT/bin/rt_set_privileged.pl $LINE"
done
echo "Processed $N users"

As you can see I have tried quite a few methods to get the command to run, but to no avail. The annoying thing is, I can take any of the commands from the commands.sh file afterwards and paste them straight into the terminal without a problem, this works fine. When they are run through the bash script though, I just get a bunch of these messages:

[Tue Sep  4 07:43:56 2012] [critical]: _AddMember called with a parameter that's not an integer. (/var/www/ticket.ourcompany.com/lib/RT/Group.pm:912)
[Tue Sep  4 07:43:58 2012] [warning]: Use of uninitialized value $principal in pattern match (m//) at /var/www/ticket.ourcompany.com/lib/RT/Group.pm line 970. (/var/www/ticket.ourcompany.com/lib/RT/Group.pm:968)
[Tue Sep  4 07:43:58 2012] [error]: Group::HasMember was called with an argument that isn't an RT::Principal or id. It's (undefined) (/var/www/ticket.ourcompany.com/lib/RT/Group.pm:973)
[Tue Sep  4 07:43:58 2012] [warning]: Use of uninitialized value $principal in pattern match (m//) at /var/www/ticket.ourcompany.com/lib/RT/Group.pm line 970. (/var/www/ticket.ourcompany.com/lib/RT/Group.pm:968)
[Tue Sep  4 07:43:58 2012] [error]: Group::HasMember was called with an argument that isn't an RT::Principal or id. It's (undefined) (/var/www/ticket.ourcompany.com/lib/RT/Group.pm:973)
[Tue Sep  4 07:43:58 2012] [warning]: Use of uninitialized value in concatenation (.) or string at /var/www/ticket.ourcompany.com/lib/RT/User.pm line 341. (/var/www/ticket.ourcompany.com/lib/RT/User.pm:341)
[Tue Sep  4 07:43:58 2012] [critical]: User  is neither privileged nor unprivileged. something is drastically wrong. (/var/www/ticket.ourcompany.com/lib/RT/User.pm:341)
[Tue Sep  4 07:43:58 2012] [warning]: Use of uninitialized value $new_member in pattern match (m//) at /var/www/ticket.ourcompany.com/lib/RT/Group.pm line 911. (/var/www/ticket.ourcompany.com/lib/RT/Group.pm:911)

suggesting that the command is being run without any parameters. At this point I could have actually run the command once for each user in the time I have been trying to solve it, can anyone help?

Chris O'Kelly
  • 1,863
  • 2
  • 18
  • 35
  • Heres something interesting, I tried adding `print "arg=<<$ARGV[0]>>\n";` to the perl script to see what was getting passed. I had been assuming the arg doesn't make it to the perl script but it seems it actually is. With that at the beginning, my output is: `>>g=<<3Cubed` no, thats not a typo, the print just seems to turn out really weird. So the argument is getting to the script... i don't understand. If I paste /home/chris/RT/bin/rt_set_privileged.pl ChrisO into the terminal it works fine. If I paste it into a bash script and run it, I get this problem. – Chris O'Kelly Sep 04 '12 at 23:09
  • Can you try and print ARGV[0] without the << >> in the print statement? In fact, I would make a program that does two things, prints the number of arguments passed ( scalar(@ARGV) ) and then prints $ARGV[0]. Call that program from a bash script like the one you have for this and see what you get. – Scooter Sep 04 '12 at 23:19
  • I get arg=3Cubed, which is perfect. That means it should then do $user->Load( 3Cubed ); which should work fine. If I type in sudo ./rt_set_privileged.pl 3Cubed I get no error messages, the same arg=3Cubed output, and the user is added to the privileged group. Could this be to do with string encoding maybe? – Chris O'Kelly Sep 04 '12 at 23:25
  • 1
    it WAS string encoding! I'll post this in it's own answer – Chris O'Kelly Sep 04 '12 at 23:37
  • Why are you escaping those dquble quotes in the call to the script?? That will cause lines with whitespace to end up being passed as multiple arguments, qith the quotes being part of them. – reinierpost Sep 10 '12 at 07:53
  • @reinierpost I'd started doing that when I couldn't figure out why my arguments weren't being passed through. In this case I knew there would never be whitespace in the file, that there would be a single word per line I wished to use as a single argument. – Chris O'Kelly Oct 10 '12 at 05:18
  • 1
    @Chris O'Kelly: An easier way to call the Perl script for each user is `xargs -n1 rt_set_privileged.pl < usernames.txt` – reinierpost Oct 10 '12 at 11:19
  • @reinierpost that is definitely simpler, I'll keep this in mind for the future. Thanks! – Chris O'Kelly Oct 10 '12 at 22:03

5 Answers5

2

You have a bunch of programming errors in that Perl code — the error messages you get are all compiler messages. So the call is correct, but the Perl code is wrong.

What you want to do is edit the Perl code until all error messages and warnings are gone.

Edit: The Perl code is wrong in the environment it runs in at that time.

ЯegDwight
  • 24,821
  • 10
  • 45
  • 52
cslotty
  • 1,696
  • 20
  • 28
  • Hi,but if I run the script straight from terminal, ie, if I type in /home/chris/RT/bin/rt_set_privileged.pl ChrisO and run it, it works fine, so the perl script must be working. The error messages seemed to suggest (to me at least) that an expected parameter was undefined, which supported my original theory that the command line argument wasn't making it all the way, but now I am not sure. – Chris O'Kelly Sep 04 '12 at 23:14
  • Ok - let's take f.ex. this warning: [warning]: Use of uninitialized value $principal in pattern match (m//) at /var/www/ticket.minecorp.com/lib/RT/Group.pm line 970 -> it says that in the module Group.pm the variable $principal is empty, but shouldn't be. So there probably is an error in the API use of Group.pm - go to Group.pm and see what it expects! – cslotty Sep 04 '12 at 23:17
  • The RT-library probably expects some more arguments/options/function calls or whatever in order to do what you expect - in that your relation to a missing argument might be right. – cslotty Sep 04 '12 at 23:20
  • Well - one more possibility: If two different calls behave differently, it might be caused by the environment (which is a common trap in scripting) - you say a direct call works, but a call from a bash script won't work? So what's the difference in the two environments? – cslotty Sep 04 '12 at 23:26
  • I don't understand what you mean... as far as I can see that error message suggests that the value $principle is set from the output of another function and that function failed. It would seem to me that the instantiation of the $User variable failed and the error messages here cascaded from there. I fail to see how this can be to do with the use of the API when I can run this script from the same directory, with the same command line argument (as far as I can tell) and have it run successfully. – Chris O'Kelly Sep 04 '12 at 23:29
  • I can't see the RT-code so I can't say what exactly is going wrong there..... is the code anywhere online? The $principal var is located somewhere in RT/Group.pm and doesn't get filled for some reason, although it should be, as it's used in a string concatenation, believ me - that's what the error message says... – cslotty Sep 04 '12 at 23:33
  • I'm sorry that I didn't take into consideration that there could be something wrong with the users in the file.... I should have considered it, but it was too late for me ;-) (2a.m.) - So after you've found the solution yourself, of course you were right with your disbelief in my arguments. The perl errors were a result of an improper end (CRLF, as you said) of the string variable containing the username. Thanks for upvoting my answers. – cslotty Sep 05 '12 at 09:43
  • Add `use warnings;` at the top of the script. – reinierpost Oct 10 '12 at 11:20
2

Arrgh, I'm sad it took me this long to realize such a simple issue. The problem was one of string formatting - The file usernames.txt was created by someone using windows and had dos formatting (CRLF). I'm guessing the argument was arriving as [username][LF] and screwing up the instantiation of the $User variable.

I'm sure I never would have gotten this without coming here to discuss it, I was going round in circles just trying it on my own.

The resolution was just to:

sudo apt-get install tofrodos
sudo fromdos usernames.txt

And then the original script worked

Thanks very much for the help guys.

Edit: enough time has now passed for me to move this from an edit of the original question to it's own answer

Chris O'Kelly
  • 1,863
  • 2
  • 18
  • 35
1
perl /home/chris/RT/bin/rt_set_privileged.pl "$LINE"

If the file was made executable, you can also do:

/home/chris/RT/bin/rt_set_privileged.pl "$LINE"

For example,

$ cat usernames.txt 
ikegami
Chris O'Kelly

$ cat script.pl 
#!/usr/bin/env perl
print "arg=<<$ARGV[0]>>\n";

$ while read LINE ; do script.pl "$LINE"; done <usernames.txt
arg=<<ikegami>>
arg=<<Chris O'Kelly>>

You could use xargs instead of while read:

$ xargs -n1 -d\\n script.pl <usernames.txt 
arg=<<ikegami>>
arg=<<Chris O'Kelly>>
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Hi, both scripts are executable already, and I have tried both those lines in my bash script. I get the same error messages. – Chris O'Kelly Sep 04 '12 at 23:03
  • @Chris O'Kelly, Then you have another error somewhere else. You asked how to pass the arguments, and that's what I answers. – ikegami Sep 04 '12 at 23:43
  • You're right, your answer does properly address the title of my question, and for that reason you had my upvote :) – Chris O'Kelly Oct 10 '12 at 05:20
  • Not just the title. The whole question was you showing you didn't know how to do it. In fact, it's the only question for which you provide any useful amount of information. If you have further problems with your script, please start a new question, providing a minimal, runnable demonstration of the problem. – ikegami Oct 10 '12 at 05:27
  • 1
    I'm not sure where the hostility is coming from, I asked a question, I was thankful for your answer, but you appear to be more interested in having an argument over what my question was about. If you read the rest of the thread you would see that I have solved the issue I was having and posted my own solution, so I am not sure what "further problems" you are referring to, but in terms of a runnable demonstration of the problem, did you not notice the full script included in my question? – Chris O'Kelly Oct 10 '12 at 21:59
  • What hostility??? I instructed you how to proceed now that your first question is answered. – ikegami Oct 10 '12 at 22:07
1

Another solution: Don't use a bash script, but directly edit the perl script, open your usernames.txt file and read it line by line - that'll solve your problem, too.

Like here http://www.perlfect.com/articles/perlfile.shtml:

my $file = "usernames.txt";
open USERS, "<$file" or die "Can't open $file ($!)";

while(<USERS>) {
 my $usertoadd = $_;
 chomp $usertoadd;
 $user->Load( $usertoadd );
 $user->SetPrivileged(1);
}
cslotty
  • 1,696
  • 20
  • 28
0

UPDATE: Now accounts for possibility of spaces in usernames, thanks to valuable tip from Peniwize's Blog.

OLD_IFS=$IFS
IFS=$'\n'    
priveleged=( $( cat usernames.txt) )
for i in "${priveleged[@]}"
do
    perl /home/chris/RT/bin/rt_set_privileged.pl $i
done
IFS=$OLD_IFS
David
  • 6,462
  • 2
  • 25
  • 22
  • I just tried this, but I get the same error messages. The weird thing is - there's no spaces in the usernames. A few have $'s in them, which I can see giving some trouble, but this issue persists even when I remove those from the file and just have alpha usernames. – Chris O'Kelly Sep 04 '12 at 23:05
  • @ChrisO'Kelly Strange. What if you replace the line with the call to `perl` with `echo $i`? It should show you a list of all the names in your file. – David Sep 04 '12 at 23:39
  • I tested it with a perl script that uses the command line arg and it seems to work. – David Sep 05 '12 at 00:03
  • @ikegami Updated solution to account for spaces. – David Sep 05 '12 at 00:29