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. |
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. |
Code:
open(FILE,"file") || die "Can't open file:$!\n"; |
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 '||' . |
All times are GMT -5. The time now is 02:00 PM. |