delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2008/05/09/08:04:53

X-Recipient: archive-cygwin AT delorie DOT com
X-Spam-Check-By: sourceware.org
X-Yahoo-Newman-Property: ymail-3
X-Yahoo-Newman-Id: 581968 DOT 81779 DOT bm AT omp106 DOT mail DOT in2 DOT yahoo DOT com
X-YMail-OSG: 2MEZk5sVM1lV_1510FD35SBy18CYeK4J.Jf4gt.sKWaBvWDx9xKnR.HEt_lkqm1M3gk4ap070cBZhMxP0utBuojMirMRi9ftykxsn6hx.B7UWP9Sk4RsgLr30xQ-
X-Mailer: YahooMailWebService/0.7.185
Date: Fri, 9 May 2008 17:34:20 +0530 (IST)
From: Jaspreet Singh <jaspreet_online2000 AT yahoo DOT com>
Reply-To: jaspreet_online2000 AT yahoo DOT com
Subject: cygwin perl useradd command
To: cygwin AT cygwin DOT com
MIME-Version: 1.0
Message-ID: <491706.30417.qm@web94904.mail.in2.yahoo.com>
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id m49C4qAA002365

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019