Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check free UID 1

Status
Not open for further replies.

Mag0007

MIS
Feb 15, 2005
829
US
how can I display free UIDs on my system? For example

grep -i nobody /etc/passwd | sed -e 's/:/ /'g | awk {'pr
int $3-1'} will work but it is only for a base system.

Instead of us doing, "id username", is it possible to do a "id uid" ?

 
There could be 65000+ free/unused UIDs, would you want to list all of them??

No, you can't do an id uid, as I guess you've found out.

Annihilannic.
 
ha no! i would just like to test if a uid is being used or not I guess.

 
A starting point:
id=1024
awk -F: '$3=='$id'{printf "uid=%d(%s) gid=%d\n",$3,$1,$4}' /etc/passwd

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I have tried this PHV:

awk: syntax error near line 1
awk: bailing out near line 1

 
It's a little off topic but...
The attached perl script is called using
Code:
chkuser.pl -p <user name>
checks users accross multiple systems by username using an exact match
Code:
chkuser.pl -u <comment>
checks users accross multiple systems by comment (gecos on AIX systems) using a grep -i type match
The code also returns the lowest available UID between 16000 and 17000 which is what we use for normal users.
It may be a hammer tocrack a nut for what you want but it's saved me hours co-ordinating UID across multiple systems
Code:
#! /usr/bin/perl -w
use strict;
use Getopt::Std;
 
sub printusage ()
  {
  print "Usage $0 -u <user name> | -p <PIN>\n";
  exit 1;
  }
 
my @hosts = qw ( host1 host2 host3 host4 host5 host6 );
my %uids;
my %opts;

getopt ( 'up', \%opts ) or printusage;
! defined $opts{'u'} and ! defined $opts{'p'} and printusage;
defined $opts{'u'} && defined $opts{'p'} and (print "Use either 'p' or 'u' flag\
n"), printusage;
exists $opts{'u'} and  ! $opts{'u'} and (print "No user name passed with 'u' fla
g\n"), printusage;
exists $opts{'p'} and  ! $opts{'p'} and (print "No PIN passed with 'p' flag\n"),
 printusage;
 
foreach my $host ( @hosts )
  {
  foreach ( `ssh $host "cat /etc/passwd"` )
    {
    my ( $pin, undef, $uid, undef, $gecos ) = split /:/;
    $opts{'p'} and $pin =~ /^$opts{'p'}$/ and print "$host,$pin,$uid,$gecos\n";
    $opts{'u'} and $gecos =~ /$opts{'u'}/i and print "$host,$pin,$uid,$gecos\n";
    $uid > 16000 and $uid < 17000 and $uids{$uid}++;
    }
  }
 
my $i;
for ( $i = 16001; $uids{$i}; $i++ ){}
print "Next free user id is ", $i, "\n";

Columb Healy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top