LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   useradd in a script (https://www.linuxquestions.org/questions/linux-newbie-8/useradd-in-a-script-627133/)

john83reuben 03-11-2008 12:57 AM

useradd in a script
 
hi, im just simply trying out writing scripts for fun, I have done a simple adduser script in perl, but now i want to modify a bit.

Here is my script

#!/usr/bin/perl

print "pls enter name\n";
$name = <STDIN>;

system("useradd $name");

if let say when i execute the script and the name i choose is "bond", a user is created succesfully

But when i execute the script again and the name i input is again "bond", it will display useradd: user bond exist.

What I would like to do is, when a same name is entered, I want to display my own message like "pls try again", how to check the /etc/passwd file and then display the message i prefer.

pls help

Thank you

john

matthewg42 03-11-2008 01:53 AM

Please post code in [code] tags, this will improve readability. You can use the getpwname function for this:

Code:

if (defined(getpwnam($username)))
{
    print "$username exists on the system\n";
}
else
{
    print "$username does not exist on the system\n";
}


TheBeli 03-11-2008 03:01 AM

Quote:

Originally Posted by john83reuben (Post 3084660)
hi, im just simply trying out writing scripts for fun, I have done a simple adduser script in perl, but now i want to modify a bit.

Here is my script

#!/usr/bin/perl

print "pls enter name\n";
$name = <STDIN>;

system("useradd $name");

if let say when i execute the script and the name i choose is "bond", a user is created succesfully

But when i execute the script again and the name i input is again "bond", it will display useradd: user bond exist.

What I would like to do is, when a same name is entered, I want to display my own message like "pls try again", how to check the /etc/passwd file and then display the message i prefer.

pls help

Thank you

john

First of all, to add users you need to be running this as root, or the useradd command thru sudo or similar, what makes the use of a variable directly in the system function really dangerous.
Users running this script could easily get root access.
Or users just trying to input a "cool" name with lot of unsupported chars will get an error. You need to sanitize and validate the variable before passing it to the system function.

john83reuben 03-11-2008 07:33 AM

****************************************
#!/usr/bin/perl

print "pls enter name\n";
$username = <STDIN>;

system("useradd $username");

if (defined(getpwnam($username)))
{
print "$username exists on the system\n";
}
else
{
print "$username does not exist on the system\n";
}

************************************
when i execute the script, and i insert any names, it will does not exist on the system. I have also insert a username that is already in /etc/passwd itself, stil the same result

onebuck 03-11-2008 08:02 AM

Hi,

Very simple to use the code tags, see;

Code:

#!/usr/bin/perl

print "pls enter name\n";
$username = <STDIN>;

system("useradd $username");

if (defined(getpwnam($username)))
{
print "$username exists on the system\n";
}
else
{
print "$username does not exist on the system\n";
}

You should really place the code within code tags as suggested.

You should also consider the problem with root that was suggested.

edit: the code tags are at the top of the reply or edit window

kansho 05-27-2008 03:32 PM

useradd in a script
 
Not sure if your still messing with this but here is what I what I used:

Code:

#!/usr/bin/perl
print "pls enter name\n";
$username = <STDIN> ;
chop($username);


if (defined(getpwnam($username)))
{
print "$username exists on the system\n";
}
else
{
print "$username does not exist on the system\n";
print "Creating $username\n";
system("useradd $username");
system("passwd $username");
system("chage -d 0 $username");
}

The chop() takes the '/n' off of the end of the <STDIN> which is why the
script was always returning "$username does not exist on the system".
Also I added the "passwd" command and "chage -d 0" which will force the newly created user to change their password at 1st login.

chrism01 05-27-2008 06:43 PM

Don't use chop(), use chomp(): http://perldoc.perl.org/functions/chomp.html , and that's '\n', not '/n'

Also, change the top lines to

#!/usr/bin/perl -w
use strict;

These 2 strictures will save you a lot of grief later.
If you have trouble understanding the warnings/errors they give you, temporarily add

use diagnostics;

as well.

kansho 05-28-2008 12:58 PM

@chrism01
Thanks for the response... I will change 'chop()' to 'chomp()' and add the
Code:

#!/usr/bin/perl -w
use strict;

I always forget to 'use strict'
hehe and sorry bout the '/n' :)

chrism01 05-28-2008 06:48 PM

I ALWAYS use those strictures in every Perl prog, even very short ones. Its amazing how often it turns out to be a good idea.
;)
Otherwise, you write a quickie, without strictures,that 'seems' to work, then later, you borrow that code & put it in a 'real' prog and all of a sudden you either get mysterious errors (if you haven't got them in the real prog), or you immediately get error/warning msgs.
I test new progs by doing

perl -wc newcode.pl

does a test compile without execution. If you have the strictures in there, it saves you from yourself.
Maybe its because I did a lot yrs using C, but I'm a bit anal about clean code....


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