Mail Archives: cygwin/2008/05/09/08:04:53
hey guys,
anybody has useradd command on cygwin. Here's something interesting.
i wrote a perl script for useradd command. it takes following arguments
-u : Username\n";
-p : Password\n";
-g : Initial Group\n";
-c : Comments\n";
-d : Home Directory\n";
-s : Shell\n";
-m : Copy /etc/skel/* to <home_directory>\n";
-h : Show usage\n";
-v : Show version\n";
--help : Show usage\n";
--version : Show version\n";
like useradd.pl -u username -p password
then it resolves all given arguments, like -u, -p, -c and others
then adds "_usr" at the end of the username passed to script
then it creates a NET USER /ADD command with /COMMENTS: and /FULLNAME: if -c is found in arguments
then it execute it
confirm it by executing mkpasswd -l -u username command, it returns usersting like
username:password:UID:GID:comments:home_dir:user_shell
or
error
if username is found in output then new user is added to windows
NOW, this is where i am stuck when new user is suppose to be added to /etc/passwd file
the thing i want to have is enable this script to add user and group of same name to passwd and group file
where i am stuck is that it can add default userstring output from mkpasswd command to /etc/passwd file. but what i want is to verify if theres any user with same username (username minus "_usr") in passwd file if there is it is noty even suppose to add user to windows. if there is a group with same username in group file it is suppose to add a virtual user like
<username minus "_usr">:UID:<GID of group with same username>:comments:home_dir:user_shell
to passwd file.
if anybody even understand what i am tring to do and can complete this perl script. please reply
if anybody completes this then that person can also create usermod, userdel, groupadd, groupmod, and groupdel perl scripts to
THIS WILL BE A REALY COOL THING IN CYGWIN
------------------------------------------------
HERES MY useradd.pl SAMPLE
------------------------------------------------
#!/usr/bin/perl
$ArgsCount = $#ARGV + 1;
$version = "Open Source Perl useradd Script For cygwin\n";
use constant vbcrlf => "\r\n";
foreach $i (0 .. $#ARGV)
{
if ($ARGV[$i] eq "-v")
{
die $version;
}
elsif ($ARGV[$i] eq "--version")
{
die $version;
}
elsif ($ARGV[$i] eq "-h")
{
print $version;
print "\n";
print "usage :\n";
print " -u : Username\n";
print " -p : Password\n";
print " -g : Initial Group\n";
print " -c : Comments\n";
print " -d : Home Directory\n";
print " -s : Shell\n";
print " -m : Copy /etc/skel/* to <home_directory>\n";
print " -h : Show usage\n";
print " -v : Show version\n";
print " --help : Show usage\n";
die " --version : Show version\n";
}
elsif ($ARGV[$i] eq "--help")
{
print $version;
print "\n";
print "usage :\n";
print " -u : Username\n";
print " -p : Password\n";
print " -g : Initial Group\n";
print " -c : Comments\n";
print " -d : Home Directory\n";
print " -s : Shell\n";
print " -m : Copy /etc/skel/* to <home_directory>\n";
print " -h : Show usage\n";
print " -v : Show version\n";
print " --help : Show usage\n";
die " --version : Show version\n";
}
}
$username = "";
$password = "";
$UID = "";
$GID = "";
$comments = "";
$home = "";
$shell = "";
foreach $i (0 .. $#ARGV)
{
if (($ARGV[$i] eq "-u") && ($username eq ""))
{
if (($ARGV[$i + 1] eq "-u") || ($ARGV[$i + 1] eq "-p")
|| ($ARGV[$i + 1] eq "-g") || ($ARGV[$i + 1] eq "-c")
|| ($ARGV[$i + 1] eq "-d") || ($ARGV[$i + 1] eq "-s")
|| ($ARGV[$i + 1] eq "-m"))
{
die "command failed.\n";
}
else
{
$username = $ARGV[$i + 1];
}
}
elsif (($ARGV[$i] eq "-p") && ($password eq ""))
{
if (($ARGV[$i + 1] eq "-u") || ($ARGV[$i + 1] eq "-p")
|| ($ARGV[$i + 1] eq "-g") || ($ARGV[$i + 1] eq "-c")
|| ($ARGV[$i + 1] eq "-d") || ($ARGV[$i + 1] eq "-s")
|| ($ARGV[$i + 1] eq "-m"))
{
#die "command failed.";
}
else
{
$password = $ARGV[$i + 1];
}
}
elsif (($ARGV[$i] eq "-g") && ($GID eq ""))
{
if (($ARGV[$i + 1] eq "-u") || ($ARGV[$i + 1] eq "-p")
|| ($ARGV[$i + 1] eq "-g") || ($ARGV[$i + 1] eq "-c")
|| ($ARGV[$i + 1] eq "-d") || ($ARGV[$i + 1] eq "-s")
|| ($ARGV[$i + 1] eq "-m"))
{
#die "command failed.";
}
else
{
$GID = $ARGV[$i + 1];
}
}
elsif (($ARGV[$i] eq "-c") && ($comments eq ""))
{
if (($ARGV[$i + 1] eq "-u") || ($ARGV[$i + 1] eq "-p")
|| ($ARGV[$i + 1] eq "-g") || ($ARGV[$i + 1] eq "-c")
|| ($ARGV[$i + 1] eq "-d") || ($ARGV[$i + 1] eq "-s")
|| ($ARGV[$i + 1] eq "-m"))
{
#die "command failed.";
}
else
{
$comments = $ARGV[$i + 1];
}
}
elsif (($ARGV[$i] eq "-d") && ($home eq ""))
{
if (($ARGV[$i + 1] eq "-u") || ($ARGV[$i + 1] eq "-p")
|| ($ARGV[$i + 1] eq "-g") || ($ARGV[$i + 1] eq "-c")
|| ($ARGV[$i + 1] eq "-d") || ($ARGV[$i + 1] eq "-s")
|| ($ARGV[$i + 1] eq "-m"))
{
#die "command failed.";
}
else
{
$home = $ARGV[$i + 1];
}
}
elsif (($ARGV[$i] eq "-s") && ($shell eq ""))
{
if (($ARGV[$i + 1] eq "-u") || ($ARGV[$i + 1] eq "-p")
|| ($ARGV[$i + 1] eq "-g") || ($ARGV[$i + 1] eq "-c")
|| ($ARGV[$i + 1] eq "-d") || ($ARGV[$i + 1] eq "-s")
|| ($ARGV[$i + 1] eq "-m"))
{
#die "command failed.";
}
else
{
$shell = $ARGV[$i + 1];
}
}
}
if ($username eq "")
{
print "Cant find username\n";
print "\n";
print $version;
print "\n";
print "usage :\n";
print " -u : Username\n";
print " -p : Password\n";
print " -g : Initial Group\n";
print " -c : Comments\n";
print " -d : Home Directory\n";
print " -s : Shell\n";
print " -m : Copy /etc/skel/* to <home_directory>\n";
print " -h : Show usage\n";
print " -v : Show version\n";
print " --help : Show usage\n";
die " --version : Show version\n";
}
$cmdline = "";
$cmdline = "NET USER /ADD ".$username."_usr";
if ($password ne "")
{
$cmdline = $cmdline." ".$password;
}
if ($comments ne "")
{
$cmdline = $cmdline." /COMMENT:"."\"".$comments."\""." /FULLNAME:"."\"".$comments."\"";
}
$CMD = `$cmdline 2>&1`;
$temp = $username."_usr";
$CMDMK = `mkpasswd -l -u $temp 2>&1`;
chomp;
($login, $npasswd, $nuid, $ngid, $ngcos, $nhome, $nshell) = split(/:/,$CMDMK);
if ($login ne $temp)
{
die "Command failed.\n";
}
$userstring = $username.":".$npasswd.":".$nuid.":".
#print $username.":".$password.":".$UID.":".$GID.":".$comments.":".$home.":".$shell."\n";
#print "Not dead";
Meet people who discuss and share your passions. Go to http://in.promos.yahoo.com/groups/bestofyahoo/
--
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Problem reports: http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ: http://cygwin.com/faq/
- Raw text -