View Single Post
Old 05-19-2005, 10:14 AM   #1 (permalink)
Admin
$_['Your_Mom'];
 
Admin's Avatar
 
Join Date: May 2002
Location: Santee
Posts: 627
Admin is on a distinguished road
Cool My first Perl script.

Since there is no Perl forum I am posting this here....

I have never written a Perl script from scratch before, just modified very basic functionality in them.

I am working on setting up a new FTP server that requires me to import all the users from the old FTP server. The new server uses virtual users where the old system used standard users. I needed a way to combine the /etc/passwd & /etc/shadow file to import them into the new system. Here is the little script I whipped up;

Code:
#!/usr/bin/perl
# i am a super cool perl script

# pull passwd into array
open(PASSWD, "passwd") || die "$!";
@passwd=<PASSWD>;
close(PASSWD);

# pull shadow into array
open(SHADOW, "shadow") || die "$!";
@shadow=<SHADOW>;
close(SHADOW);

# count elements
$cntShadow = @shadow;
$cntPasswd = @passwd;

# build a nice hash table
for ($i=0; $i < $cntShadow; $i++)
{
  chomp($shadow[$i]);
  ($user,$pass,$junk) = split(/:/,$shadow[$i],3);
  $sprShadow{$user} = { 'pass' => $pass };
}

# build another nice hash table
for ($k=0; $k < $cntPasswd; $k++)
{
  chomp($passwd[$i]);
  ($user,$pass,$uid,$gid,$domain,$home,$shell) = split(/:/,$passwd[$k]);
  $sprPasswd{$user} = { 'domain' => $domain, 'home' => $home };
}

# figure out which file is longer
if ($cntShadow >= $cntPasswd) { %cntrFile = %sprShadow; }
else { %cntrFile = %sprPasswd; }

foreach $key (keys %cntrFile) {
  print $key . ":" .  $sprShadow{$key}->{pass} . ":" . $sprPasswd{$key}->{domain} . ":" . $sprPasswd{$key}->{home} .  "\n";
}
I know it isn't much, but I'm happy with it.
__________________


Urban Clothing
Admin is offline   Reply With Quote