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 |
Please post code in [code] tags, this will improve readability. You can use the getpwname function for this:
Code:
if (defined(getpwnam($username))) |
Quote:
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. |
****************************************
#!/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 |
Hi,
Very simple to use the code tags, see; Code:
#!/usr/bin/perl 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 |
useradd in a script
Not sure if your still messing with this but here is what I what I used:
Code:
#!/usr/bin/perl 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. |
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. |
@chrism01
Thanks for the response... I will change 'chop()' to 'chomp()' and add the Code:
#!/usr/bin/perl -w hehe and sorry bout the '/n' :) |
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. |