Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Writers Block

Reply
Thread Tools

Writers Block

 
 
dale
Guest
Posts: n/a
 
      02-15-2008
Here's what I'm trying to do in this block of code - I'm trying to get
the UIDs from /etc/passwd and present them to the user. Then, the
user can pick a UID not in the list. If the user picks a UID in the
list, user should have his hand slapped and be prompted to enter a new
value.

The "Get UID from user" and "Check to make sure it's a new UID" parts
are what I am having trouble with.


Here is the code:

############################################## GET UID #########
#
### Get /etc/passwd names and UIDs
my $toBe = "/etc/passwd";
my $copy = "/root/bin/temp";
copy($toBe, $copy) or die "\n Cannot open file \n";
`awk -F : '{print \$1"," \$3}' /root/bin/temp > tempUID`;
`rm -f /root/bin/temp`;

### Read login names and UIDs into hash
my $tempUID = "/root/bin/tempUID";
open(FILE,"<$tempUID") || die "\nCan't read: $tempUID$!\n";
my $uid;
my $login;
while(<FILE>){
($login,$uid)=split(/,/,$_);
chomp $uid;
$hashUID->{$uid} = $login;}

### Print UIDs
$counter = 0; # reset counter
while (($uid, $login) = each(%$hashUID))
{
if ( ($uid >500)&&($uid < 5000) ){
print "\n\t$uid \t$login";}
}

### Get UID from user
sub get_UID{
print "\n\n These are the non-system logins on this system.
\n Please input a new UID to start your users with: ";
$uidInput=<>;
chomp $uidInput;}

get_UID();

### Check to make sure it's a new UID
foreach (keys (%$hashUID)) {
print "\nchecking UID"; ##for debugging
if ($uidInput == $uid) {
print "\n That UID exists already. Try another: ";
goto &get_UID;}
}

#
################################################## ##############
 
Reply With Quote
 
 
 
 
Joost Diepenmaat
Guest
Posts: n/a
 
      02-15-2008
dale <> writes:

> Here's what I'm trying to do in this block of code - I'm trying to get
> the UIDs from /etc/passwd and present them to the user. Then, the
> user can pick a UID not in the list. If the user picks a UID in the
> list, user should have his hand slapped and be prompted to enter a new
> value.
>
> The "Get UID from user" and "Check to make sure it's a new UID" parts
> are what I am having trouble with.


Without any other info on what the trouble is:

http://perl.plover.com/FAQs/Buffering.html


--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
 
Reply With Quote
 
 
 
 
dale
Guest
Posts: n/a
 
      02-15-2008
On Feb 15, 2:45 pm, Joost Diepenmaat <jo...@zeekat.nl> wrote:
> dale <dalestubblefi...@gmail.com> writes:
> > Here's what I'm trying to do in this block of code - I'm trying to get
> > the UIDs from /etc/passwd and present them to the user. Then, the
> > user can pick a UID not in the list. If the user picks a UID in the
> > list, user should have his hand slapped and be prompted to enter a new
> > value.

>
> > The "Get UID from user" and "Check to make sure it's a new UID" parts
> > are what I am having trouble with.

>
> Without any other info on what the trouble is:
>
> http://perl.plover.com/FAQs/Buffering.html



I should note that this is my first attempt at a PERL script. I have
hit writer's block due to my lack of PERL knowledge and I am not sure
if I am checking the user's input correctly.

I skimmed over the page you referenced, but I do not understand how it
applies in this situation.

thanks,
-dale-

 
Reply With Quote
 
Randal L. Schwartz
Guest
Posts: n/a
 
      02-15-2008
>>>>> "dale" == dale <> writes:

dale> I should note that this is my first attempt at a PERL script.

Unless you're talking about shooting a commercial for the
non-profit "Protecting Escarpment Rural Lands" (perlofburlington.org),
you are probably working on a Perl script, and not a "PERL script".

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      02-15-2008
dale wrote:
> Here's what I'm trying to do in this block of code - I'm trying to get
> the UIDs from /etc/passwd and present them to the user. Then, the
> user can pick a UID not in the list. If the user picks a UID in the
> list, user should have his hand slapped and be prompted to enter a new
> value.
>
> The "Get UID from user" and "Check to make sure it's a new UID" parts
> are what I am having trouble with.
>
>
> Here is the code:


[ SNIP ]

Try something like this:

#!/usr/bin/perl
use warnings;
use strict;

my %hashUID;
while ( my ( $name, $uid ) = ( getpwent )[ 0, 2 ] ) {
$hashUID{ $uid } = $name;
}

### Print UIDs
while ( my ( $uid, $login ) = each %hashUID ) {
if ( $uid > 500 && $uid < 5000 ) {
print "\t$uid \t$login\n";
}
}

print "\nThese are the non-system logins on this system.\nPlease input a
new UID to start your users with: ";
{
( my $uidInput = <STDIN> ) =~ tr/0-9//cd;
if ( $uidInput <= 500 || $uidInput >= 5000 || exists $hashUID{
$uidInput } ) {
print "\nThat UID exists already. Try another: ";
redo;
}
}

__END__




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
 
Reply With Quote
 
Joost Diepenmaat
Guest
Posts: n/a
 
      02-15-2008
dale <> writes:
> I skimmed over the page you referenced, but I do not understand how it
> applies in this situation.


I'm assuming (since you didn't make it clear) that the "print prompt,
read input" parts is where the problem is, and that the problem is that
you don't actually see the prompt.

Please correct me if that's not the case.

From that assumption, the problem is that perl is buffering your
script's output and you should switch off output buffering. Put

$|=1;

At the top of the code (or at least, before the prompting code).

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
 
Reply With Quote
 
dale
Guest
Posts: n/a
 
      02-15-2008
On Feb 15, 3:47 pm, Joost Diepenmaat <jo...@zeekat.nl> wrote:
> dale <dalestubblefi...@gmail.com> writes:
> > I skimmed over the page you referenced, but I do not understand how it
> > applies in this situation.

>
> I'm assuming (since you didn't make it clear) that the "print prompt,
> read input" parts is where the problem is, and that the problem is that
> you don't actually see the prompt.
>
> Please correct me if that's not the case.
>

[SNIP]

The code I was using and the code posted by John Krahn both work
(Thanks John). In other words, they both will accept numerical input
from the user and store the inputted value in $uidInput.

The problem is that I want the user to be prompted to enter a
different UID if the UID already exists in %hashUID (which is just a
copy of username and uid values from /etc/passwd). I am having
problem with this error checking portion.

Overall, what I would like this script to do is allow a user to create
users on a Linux server in batch from a CSV file (to save me time).
Obviously, a UID that is already in use cannot be used by a newly
created user. On the other hand, the user is encouraged to reuse a
GID (which is what I will probably do 9/10 of the time). In the
future, I would like to also allow the user to create a new group with
this script, but that is outside of the scope of what I am currently
trying to accomplish.

Perhaps knowing my overall goal and seeing my code in it's entirety
would allow anyone to assist me further. I realize there may be some
better way to do things in this script, such as how I awk files with
Perl instead of issuing Bash commands. Hopefully, I will be able to
streamline those things as my Perl skills get better. I just started
learning Perl on Monday. Any suggestions for optimization are
certainly welcome.

Anyway, here is the code:

#!/usr/bin/perl
# Author: Dale Stubblefield
# Purpose: To add multiple users from an input file.
# input file syntax: "login,user name"
#
use warnings;
use strict;
use File::Copy;

# Declare Variables
my $hash;
my $hashGID;
my %hashUID;
my $uid;
my $uidInput;
my $counter = 0;

# Clear the screen
system("clear");

############################# Get input file from user #########
#
print "\n\n Please provide a file with logins and names.";
print "\n\n (File syntax: ag03555,Jack ) ";
print "\n\n (FILE Input example: /root/fake.list ) ";
print "\n\n FILE: ";
my $FILE=<>;

open(FILE,"<$FILE") || die "\nCan't read: $FILE$!\n";
while(<FILE>){
my($login,$name)=split(/,/,$_);
chomp $name;
$hash->{$login} = $name;
}
close(FILE);
#
################################################## #########


############################################## GET GID #########
#
### Get /etc/group names and GIDs
my $toBe = "/etc/group";
my $copy = "/root/bin/temp";
copy($toBe, $copy) or die "\n Cannot open file \n";
`awk -F : '{print \$1"," \$3}' /root/bin/temp > /root/bin/tempGID`;
`rm -f /root/bin/temp`;
### FOR DEBUGGING ###
# print $tempGID;
#####################

### Read group names and GIDs into hash
my $tempGID = "/root/bin/tempGID";
open(FILE,"<$tempGID") || die "\nCan't read: $tempGID$!\n";
my $gid;
my $group;
while(<FILE>){
($group,$gid)=split(/,/,$_);
chomp $gid;
$hashGID->{$gid} = $group;
}
close(FILE);

### Print GIDs
print "\n These are the non-system groups on this system.";
print "\n\tGID\tGroupname";
while (($gid, $group) = each(%$hashGID)){
if ( ($gid >500)&&($gid < 5000) ){
print "\n\t$gid \t$group";}
}

### Get GID from user
print "\n\n Please select a GID to add your users to: ";
my $gidInput=<>;
chomp $gidInput;
#
################################################## #########


############################################## GET UID #########
#
### Get /etc/passwd names and UIDs
my $toBe = "/etc/passwd";
my $copy = "/root/bin/temp";
copy($toBe, $copy) or die "\n Cannot open file \n";
`awk -F : '{print \$1"," \$3}' /root/bin/temp > /root/bin/tempUID`;
`rm -f /root/bin/temp`;

### Read login names and UIDs into hash
my $tempUID = "/root/bin/tempUID";
open(FILE,"<$tempUID") || die "\nCan't read: $tempUID$!\n";
my $uid;
my $login;
while(<FILE>){
($login,$uid)=split(/,/,$_);
chomp $uid;
%hashUID->{$uid} = $login;}

### Print UIDs
print "\n\n These are the non-system logins on this system.\n";

while (($uid, $login) = each(%hashUID)){
if ( ($uid >500)&&($uid < 5000) ){
print "\n\t$uid \t$login";}
}

### Get UID from user
print "\n\n Please input a new UID to start your users with: ";
my $uidInput;
#chomp $uidInput;
{
( $uidInput = <STDIN> ) =~ tr/0-9//cd;
if ( ($uidInput <= 500 || $uidInput >= 5000 ) ||exists
$hashUID{uidInput} ) {
print "\n That UID already exists or is out of range. Try another:
";
redo;
}
}
#
################################################## #########


######################################### Create Users #########
#
$counter = 0; # reset counter
foreach (keys (%$hash)){
# -u = UID
# -g = GID
# -c = comments/user real name
print "\n useradd $_ -u $uidInput -g $gidInput -c \"$hash->{$_}
\" ";
print "\n echo changeme | passwd --stdin $_ ";
print "\n chage -d 2007-10-10 -W 14 -M 90 $_ \n";
print "\n";
##### Left commented out for debugging
##### so users aren't actually created
# `useradd $_ -u $uid -g $gidInput -c \"$hash->{$_}\"`;
# `echo changeme | passwd --stdin $_`;
# `chage -d 2007-10-10 -W 14 -M 90 $_`;
######################################
$uid ++;
$counter ++;
}
print " $counter users were added.\n";
print "\n";
#
################################################## #########


############################################# Clean up #########
`rm -f /root/bin/temp`;
`rm -f /root/bin/tempGID`;
`rm -f /root/bin/tempUID`;
################################################## #########

 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      02-15-2008
dale wrote:
[...]
> The problem is that I want the user to be prompted to enter a
> different UID if the UID already exists in %hashUID (which is just a
> copy of username and uid values from /etc/passwd). I am having
> problem with this error checking portion.
>
> Overall, what I would like this script to do is allow a user to create
> users on a Linux server in batch from a CSV file (to save me time).


There are better ways to push accounts around.

> Obviously, a UID that is already in use cannot be used by a newly
> created user. [...]


> if ( ($uidInput <= 500 || $uidInput >= 5000 ) ||exists

$hashUID{uidInput} ) {

Missing a '$' there..

if ( ($uidInput <= 500 || $uidInput >= 5000 ) ||exists
$hashUID{$uidInput} ) {


However, why do you care about the UID? Just let adduser, or
whatever you're going to use to actually add the account, create it.
A unique username is what you should care about, possibly they
could select their group, but the UID isn't something you generally
care about.

There are plenty of adduser types of scripts available. Search
the Internet and you'll find many solutions.
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      02-16-2008
Joost Diepenmaat <> wrote:


> perl is buffering your
> script's output and you should switch off output buffering. Put
>
> $|=1;



That is not switching off output buffering, that is switching on autoflush.

IO is still buffered, it just gets flushed more often than when
autoflush is left off.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      02-16-2008
On Fri, 15 Feb 2008 12:58:00 -0800 (PST), dale
<> wrote:

>> http://perl.plover.com/FAQs/Buffering.html

[snip]
>I skimmed over the page you referenced, but I do not understand how it
>applies in this situation.


I skimmed the rest of this thread, but try $|++;


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Writers block on complex object model Phy6 Java 2 11-05-2007 09:53 PM
Fo:Block can you check to see if a block contains any text by using the block id? morrell XML 1 10-10-2006 07:18 PM
Infinity is looking for writers. The Modfather The Lounge 71 02-12-2006 08:04 AM
Spyware & Adware writers switching over to Mozilla marc@mercund.org Firefox 4 03-03-2005 11:46 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57