Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Passing data from one script to another

by Anonymous Monk
on May 02, 2003 at 15:12 UTC ( #255026=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Peeps

I want to pass data to a script from the command line, so that i can use the data witin that script. Somethign like:
script.pl username=bob password=bob realname=bob jones
And then within the script.pl script i can use those valuse as $username, $password and $realname.

Also, how hard is it to encrypt a plaintext password into an MD5 password?

Replies are listed 'Best First'.
Re: Passing data from one script to another
by Limbic~Region (Chancellor) on May 02, 2003 at 15:22 UTC
    Anonymous Monk,
    CPAN is your friend. See this for your question on MD5.

    You should also look at perldoc perlvar for @ARGV for command line arguments

    Since there isn't a need to roll your own, it is back to CPAN for modules on command line arguments.
    Click Me

    As yet another reason to use CPAN - you may want to look at one of the config modules. This will allow you to have a standard config and then overide those parameters on the command line.
    Links are cool

    Cheers - L~R

Re: Passing data from one script to another
by jgallagher (Pilgrim) on May 02, 2003 at 15:25 UTC
Re: Passing data from one script to another
by halley (Prior) on May 02, 2003 at 16:23 UTC

    MD5 and SHA1 do not encrypt, they hash. You can't undo a hashing function, but you can decrypt something that was encrypted.

    That may be what you want, but it wasn't clear from your question. (Even if you know this, let me clarify for others.)

    The digesting modules like Digest::SHA1 take some source data and make a small fixed-length hash value that would otherwise be very difficult to reproduce with some different source data. They are useful in the same way a CRC (cyclic redundancy check) is useful, only moreso. Nobody can get the original source data out of a hash value. You know the source data hasn't been modified if you get the same hash value (aka digest) that they did. But you still have to send the full data in some way.

    The crypt() function for /etc/passwd passwords works the same way: it's a hashing of the plaintext password. This is handy because nobody can get the original password back from the hash value. The password checker just checks to see if the user supplies any password that would hash to an identical value. Now several operating systems support MD5-generated hash values in addition to the weaker crypt(), but it's the same idea.

    --
    [ e d @ h a l l e y . c c ]

Re: Passing data from one script to another
by hmerrill (Friar) on May 02, 2003 at 15:18 UTC
    Everything you need is in the @argv array, but the best way to access that array IMHO is using 'shift'. Here's a quick test I created to show a script accepting command line parameters:
    #!/usr/bin/perl -w # # This script is set up to accept *3* parameters: # >./perl_argv.pl abc 123 xyz use strict; my $arg1 = shift; my $arg2 = shift; my $arg3 = shift; if (! $arg1) { print "Didn't get 1st argument!\n"; } else { print "1st argument = $arg1\n"; } if (! $arg2) { print "Didn't get 2nd argument!\n"; } else { print "2nd argument = $arg2\n"; } if (! $arg3) { print "Didn't get 3rd argument!\n"; } else { print "3rd argument = $arg3\n"; }
    HTH.
      For the MD5 part, I did a google search for 'perl encrypt md5' and found this link:

      http://www.cpan.org/modules/by-module/Digest/Digest-Perl-MD5-1.5.readme

      I haven't used it, but looks like perl module Digest::MD5::Perl does what you want.

      HTH.
      I have just taken a look at Digest:MD5 and it looks fairly simple to use _if you know how_. How would i pass somethign like $password containing a plain text password thru Digest:MD5 and output the encrypted password as $encr_pass?
Re: Passing data from one script to another
by Elgon (Curate) on May 02, 2003 at 15:25 UTC

    Hi AnonyMonk,

    With the first of these, your best bet is to look at @ARGV in the perldocs and see how to access and parse command line parameters. This node should help you there as the Getopt::Long and Getopt::std modules are your friends. For more info on this, use the search tool at the top left of the website to find the perldocs for these modules.

    It is easy to encrypt to md5, using the MD5 module. See this page for information on how to use it.

    Elgon

    Please, if this node offends you, re-read it. Think for a bit. I am almost certainly not trying to offend you. Rememer - Please never take anything I do or say seriously.

Re: Passing data from one script to another
by dragonchild (Archbishop) on May 02, 2003 at 15:41 UTC
    Check out Getopt::Long. Also, check out MD5::.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      I have just taken a look at Digest:MD5 and it looks fairly simple to use _if you know how_. How would i pass somethign like $password containing a plain text password thru Digest:MD5 and output the encrypted password as $encr_pass?.

        Like this:

        use Digest::MD5 qw(md5); my $password = "passwd"; my $encr_pass = md5($password);

        But for anything new, you should be using SHA1 (as MD5 is quite possibly on its deathbed as far as security applications go), which is done like this:

        use Digest::SHA1 qw(sha1); my $password = "passwd"; my $encr_pass = sha1($password);

        Unfortunately, Digest::SHA1 doesn't come with Perl by default (the MD5 module does).

        Better still, you can use simply Digest with an OO interface to specify the hash algorithm at runtime. IMHO, its important to be able to switch from one crypto/hash algorithm to another on a moment's notice, in case your current algorithm turns out to have a catastrophic security hole. The Digest.pm module helps you to do that change. Here's how to use it:

        use Digest; my $password = "passwd"; my $digest = Digest->new("SHA1"); $digest->add($password); my $encr_pass = $digest->digest();

        The OO interface in the Digest.pm module is basically the same as the interface in the Digest::MD5 and Digest::SHA1 modules (I used the functional interface for those modules above, for simplicity's sake).

        ----
        I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
        -- Schemer

        Note: All code is untested, unless otherwise stated

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://255026]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2021-12-03 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (30 votes). Check out past polls.

    Notices?