#!/usr/bin/perl -w # # addusers.pl - a perl script for easing adding many users to groups # # users.txt - takes in users, one per line in the following format: # # username, Full Name # # if the username does not have an '@' symbol, the email address # is generated from the $fqdn variable # # passwords are currently randomly generated (not printed so either # email needs to be good or other authN must be used [ie, LDAP]) # # groups.txt - group name, one per line # # note: if any of the users or groups exist, the creation will fail silently # but the script will still try to add the usernames to the groups # use strict; use WWW::Mechanize; use HTML::TokeParser; use Crypt::RandPasswd; ### DEBUG # use Data::Dumper; # Confluence basepath my $BASE = 'https://confluence.example.edu/'; # Login Info (admin l/p) my $login = 'login'; my $password = 'password'; # Input files open(USERS, '< users.txt') or die "Couldn't open users file: $!\n"; open(GROUPS, '< groups.txt') or die "Couldn't open groups file: $!\n";; my @groups = (); # email my $fqdn = '@example.edu'; ### Shouldn't have to touch below ### # User Agent my $a = WWW::Mechanize->new(); $a->agent_alias('Mac Mozilla'); # TokeParser my $stream; # Login login(); # print "Login OK.\n"; ### DEBUG add_groups(); add_users(); # Add Crap # print $a->content(); # $stream = HTML::TokeParser->new(\$a->content()); # print "Stream OK.\n"; ### DEBUG # Login sub login { $a->get($BASE . 'login.action'); $a->form('loginform'); $a->field('os_username', $login); $a->field('os_password', $password); $a->submit(); } # Add Groups sub add_groups { while() { s/^\s+|\s+$//g; $a->get($BASE . 'admin/users/browsegroups.action'); $a->form('creategroupform'); $a->field('name', $_); $a->submit(); push(@groups, $_); } } # Add Users sub add_users { while() { @_ = split /,/; # TODO: figure out error conditions for no name # next if $_[0] eq ''; # $_[1] = '' if $_[1] eq ''; $_[0] =~ s/^\s+|\s+$//g; $_[1] =~ s/^\s+|\s+$//g; # password my $pw = Crypt::RandPasswd->chars(8,8); # email my $em; if(/@/) { $em = $_[0]; } else { $em = $_[0] . $fqdn; } # add user $a->get($BASE . 'admin/users/createuser.action'); $a->form('createuserform'); $a->field('username', $_[0]); $a->field('password', $pw); $a->field('confirm', $pw); $a->field('fullname', $_[1]); $a->field('email', $em); $a->submit(); my $groups = '&groupsToJoin=' . join('&groupsToJoin=', @groups); # add to group $a->get($BASE . 'admin/users/editusergroups.action?username=' . $_[0] . $groups . '&join=Join+%3E%3E'); } }