Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
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
Old 05-19-2005, 10:58 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
redhead is on a distinguished road
Code:
# build another nice hash table
for ($k=0; $k < $cntPasswd; $k++)
{
  chomp($passwd[$i]);
Shouldn't this be:
Code:
# build another nice hash table
for ($k=0; $k < $cntPasswd; $k++)
{
  chomp($passwd[$k]);
Eventho it dosn't affect the outcome of this program, it will ensure your $shell will hold a usefull info, if you plan on using it else where.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 05-19-2005, 11:09 AM   #3 (permalink)
Admin
$_['Your_Mom'];
 
Admin's Avatar
 
Join Date: May 2002
Location: Santee
Posts: 627
Admin is on a distinguished road
Ahhhh, nice find. Thanks.
__________________


Urban Clothing
Admin is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing a server script from PHP sde PHP 4 02-05-2005 11:12 AM


All times are GMT -8. The time now is 01:51 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting