LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-11-2008, 01:57 AM   #1
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Rep: Reputation: 17
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
 
Old 03-11-2008, 02:53 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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";
}
 
Old 03-11-2008, 04:01 AM   #3
TheBeli
LQ Newbie
 
Registered: Feb 2008
Distribution: LFS
Posts: 23

Rep: Reputation: 15
Quote:
Originally Posted by john83reuben View Post
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.
 
Old 03-11-2008, 08:33 AM   #4
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Original Poster
Rep: Reputation: 17
****************************************
#!/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

Last edited by john83reuben; 03-11-2008 at 08:35 AM.
 
Old 03-11-2008, 09:02 AM   #5
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,541
Blog Entries: 23

Rep: Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943
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

Last edited by onebuck; 03-11-2008 at 09:04 AM. Reason: code tags
 
Old 05-27-2008, 04:32 PM   #6
kansho
LQ Newbie
 
Registered: May 2005
Distribution: Fedora 8
Posts: 5

Rep: Reputation: 0
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.
 
Old 05-27-2008, 07:43 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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.
 
Old 05-28-2008, 01:58 PM   #8
kansho
LQ Newbie
 
Registered: May 2005
Distribution: Fedora 8
Posts: 5

Rep: Reputation: 0
@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'
 
Old 05-28-2008, 07:48 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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....
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
useradd script headache croc Programming 7 11-07-2006 05:15 AM
Samba --> nobody run "add user script = /usr/sbin/useradd ....." ??!! Unconfigured Fedora 1 02-23-2006 05:54 AM
Useradd - Cannot locate /etc/default/useradd in Solaris Paean Solaris / OpenSolaris 4 12-09-2005 02:36 AM
useradd script newuser455 Programming 4 01-28-2005 12:48 AM
Useradd Script jcornel7 Linux - Newbie 2 02-17-2003 10:48 AM


All times are GMT -5. The time now is 10:46 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration