Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Delete users, account login from a txt file to the database

by rodrigoo (Initiate)
on Apr 10, 2015 at 05:12 UTC ( [id://1123010]=perlquestion: print w/replies, xml ) Need Help??

rodrigoo has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys, I have the following problem. I have created the following code where I add to the database all the usernames and passwords automatically that are in the text file.

#!/usr/bin/perl -w use DBI; # connect to the database my $dbh = DBI->connect('dbi:mysql:ass2db', 'test', '123') or die "Can't connect: ", $DBI::errstr; # prepare a SQL statement to insert your data # (using placeholders ("?") so you only have to # compile the statement once) my $sth = $dbh->prepare(<<SQL) or die "Can't prepare: ", $dbh->errstr; insert into ASS2 (User_name, Pass) values (?, ?) SQL # open up your data file open FILE, "data.txt" or die "Can't open: $!"; while (<FILE>) { chomp; # parse your data out of the file my($d1, $d2) = split(':', $_); # assuming tab-separated data #use system command to add user and set passwd system "useradd -m -p $d1 $d2"; # insert your data into the database $sth->execute($d1, $d2); } close FILE; $sth->finish; $dbh->disconnect;

This code works like a charm, now the problem is that I need to do the same process but removing the usernames and passwords using the text file.

the data.txt contains: user1:password1 user2:password2

I have tried everything and I cannot make it work. Im a total newbie with perl and I suck at programming so it makes it really hard for me. Any ideas would be much appreciated. Thanks

Replies are listed 'Best First'.
Re: Delete users, account login from a txt file to the database
by hippo (Bishop) on Apr 10, 2015 at 08:45 UTC
    #use system command to add user and set passwd system "useradd -m -p $d1 $d2";

    Please do not do this. Ever.

    • useradd requires escalated privileges
    • You are not running in taint mode
    • You have done no validation on $d1 or $d2
    • There is no need for the shell at all here

    and that's not even to consider that your choice of variable names ($d1, $d2) means that you have confused them.

    Instead: use taint mode and strict, validate your input, give your variables meaningful names and use the exec form of system, eg: system('useradd', @args); If you are writing anything in perl which requires escalated privileges then do please read through all of perlsec first.

    Security is always important. If you are giving the user a root shell, it becomes vitally important.

Re: Delete users, account login from a txt file to the database
by NetWallah (Canon) on Apr 10, 2015 at 06:43 UTC
    ..."This code works like a charm"
    Are you sure it does what it is supposed to ?

    What user name do you think gets added by your "useradd" command ?

    If you do understand what it actually does, you can reverse the process by using a SQL "DELETE" instead of "INSERT", and a "userdel" command instead of "useradd".

            "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

Re: Delete users, account login from a txt file to the database
by vinoth.ree (Monsignor) on Apr 10, 2015 at 05:15 UTC
    I have tried everything and I cannot make it work

    Show us what you have tried! So that we can help you better.


    All is well. I learn by answering your questions...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1123010]
Approved by vinoth.ree
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-20 15:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found