LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 12-21-2007, 10:16 PM   #1
happy13
LQ Newbie
 
Registered: Dec 2007
Posts: 2

Rep: Reputation: 0
Adding users from file using PERL?


I am trying to use perl to add user accounts to my new server. I have the file which holds the usernames and passwords in this format:

username: password (no space between the : and word password)
username2: password (did it here because a funny face showed up)

and so on. I am trying to create a perl script that will read from this file and create the user accounts but am having trouble reading them one after another from the file and I don't seem to get the split function to work correctly. Please Help. Thanks in advance.

Last edited by happy13; 12-21-2007 at 10:17 PM.
 
Old 12-21-2007, 10:25 PM   #2
happy13
LQ Newbie
 
Registered: Dec 2007
Posts: 2

Original Poster
Rep: Reputation: 0
This is what I have so far:

#!/usr/bin/perl
open(FILE,"users") || die "Can't open file:$!\n";
@raw_data=<FILE>;
close(FILE);
foreach $user(@raw_data)
{
chop ($user);
($username, $password) = split(":", $user);
crypt($password, xx);
system("useradd -m -p $password $username -g groupname");
}

I am receiving an error that no sub routine is defined for system.

Last edited by happy13; 12-21-2007 at 11:16 PM.
 
Old 12-21-2007, 11:55 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 237Reputation: 237Reputation: 237
Code:
open(FILE,"file") || die "Can't open file:$!\n";
@raw_data=<FILE>;
close(FILE);
foreach $user(@raw_data)
{   
    ($username, $password) = split(":", $user);
    chomp($username); chomp($password);
    my $p = crypt($password, xx); # check perldoc -f crypt on how to generate random salt
    my $cmd = qq(useradd -m -p $p $username -g groupname); #define your groupname variable
    print $cmd ."\n";
   # system($cmd); #uncomment when needed
}
 
Old 12-22-2007, 07:26 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.2, Centos 5.8
Posts: 11,740

Rep: Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905
Do these changes:

after
@raw_data=<FILE>;
add
chomp(@raw_data);

also, split uses a regex style normally, so
($username, $password) = split(/:/, $user);

don't split the individual fields.

Note chomp(), not chop() .. 2 different operators and you want the first one here.
http://perldoc.perl.org/functions/chomp.html
http://perldoc.perl.org/functions/split.html
Also, due to precedence issues, when checking file open etc, use 'or' not '||' .
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl adding unwanted lines at EOF? ryedunn Programming 1 09-07-2006 09:35 AM
Adding perl module to Appache 1.3.3 for metadot walterbyrd Linux - Software 0 03-19-2005 04:24 PM
cgi perl : I cant get perl to append my html file... the_y_man Programming 3 03-22-2004 05:07 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
Help on Administrative Utility (adding users & data from input file) NightWolf_NZ Linux - Software 0 09-10-2003 08:43 PM


All times are GMT -5. The time now is 02:50 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration