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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Script Reading Password from File

Status
Not open for further replies.

bigkid123

ISP
Nov 5, 2002
18
US
I am using a unix box that allows only a few people access. On this box I have several small scripts that I use to ftp files and to execute small commands at our hosts. I would like to know if there is a way to have the script pull the password from a text file so that when I change the password I would not have to change each individual script.

Here is a sample script that I use. In the @password field I would like this to point to the text file located in /home/b088/ISP directory and read the password.

Any help would be appreciated.

!/usr/local/bin/perl

use TE::BiLo;
use Net::FTP;

$store = shift;

$user = "root";
@password = ("help123");

$| = 1;

$te = TE::BiLo->new($store, $MYUSER, $MYPASS)
|| die "Unable to connect to store \"$store\"";

$te->sendln("cd /admin/script/");
$te->sendln("chmod 755 mkpos ");
$te->sendln("echo ExitNow");
$te->expect("\nExitNow");
 
Simple answer, yes. How you read the password depends how you store it. If it is in your file on its own, you can just read it as:

open FILE, $file or die "Cannot open file: $!\n";
my $password = <FILE>;
chomp $password;
close FILE;

If you have a key/value pair in CSV format for different users:

open FILE, $file or die &quot;Cannot open file: $!\n&quot;;
while(<FILE>) {
chomp;
($password) = (/$user,(.*)/);
last if($password);
}
close FILE;

Providing the file with the password is in a directory that cannot be accessed directly via the webserver then you should be fine. Ensure the file permissions are set so the userid, which the webserver runs as, can access the file.


Barbie
Leader of Birmingham Perl Mongers
 
First off why an array, and are the fields $user, & @password assigned to $MYUSER, and $MYPASS.

What you're looking for is fairly easy, but bloody dangerous.

Write it as a subroutine so you can later encrypt the details you want to pass
...
@password=&get_pass;
...

sub get_pass {
open (FH, &quot;/home/b088/ISP/fred.txt&quot;) || die &quot;can't open file $!&quot;;
@pass=<FH>;
close FH;
return @pass;
}

HTH
Paul
see statement about bloody dangerous

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
to help tighten things up, if you are going from *ix to *ix, you might want to utilize scp instead of ftp with keys intead of passwords... then no password maintenance to worry about...
 
Hello Everyone,

First of all thanks for the quick responses. I am trying the suggestions now to see if I can get them to work. Also the orginal script there was a typo and not sure if that would make a difference in the suggestions given. Here is the script again with changes made. I took out the MYUSER and MYPASS.

Thanks again to Everyone.

!/usr/local/bin/perl

use TE::BiLo;
use Net::FTP;

$store = shift;

$user = &quot;root&quot;;
@password = (&quot;help123&quot;);

$| = 1;

$te = TE::BiLo->new($store, $user, @password)
|| die &quot;Unable to connect to store \&quot;$store\&quot;&quot;;

$te->sendln(&quot;cd /admin/script/&quot;);
$te->sendln(&quot;chmod 755 mkpos &quot;);
$te->sendln(&quot;echo ExitNow&quot;);
$te->expect(&quot;\nExitNow&quot;);
 
I am now able to get the password part to work but when I run the script it gives me an error when trying to ftp a file to the /tmp directory. Can anyone tell me what needs to be changed in order for it to run successfully.


Thanks
bigkid

#!/usr/local/bin/perl -w

use Net::FTP;

$store = shift;

$user = &quot;root&quot;;
@password=&get_pass;

$dir = &quot;/tmp&quot;;
$file = &quot;stores.txt&quot;;

$| = 1;

$ftp = Net::FTP->new($store , Debug => 3);
$ftp->login($user, @password)
or die &quot;Unable to connect to store \&quot;$store\&quot;&quot;;
$ftp->cwd(&quot;$dir&quot;);
$ftp->binary();
$ftp->put(&quot;$file&quot;);
$ftp->quit();

sub get_pass {
open (FH, &quot;/home/b088/ISP/pass.txt&quot;) || die &quot;can't open file $!&quot;;
@pass=<FH>;
close FH;
return @pass;
}


and this is the output that I get when I run the script.

Net::FTP: Net::FTP(2.33)
Net::FTP: Exporter
Net::FTP: Net::Cmd(2.11)
Net::FTP: IO::Socket::INET
Net::FTP: IO::Socket(1.1603)
Net::FTP: IO::Handle(1.1504)

Net::FTP=GLOB(0x2017b5e8)<<< 220 bst00223 FTP server (Version 4.1 Tue Feb 16 14:
50:43 CST 1999) ready.
Net::FTP=GLOB(0x2017b5e8)>>> user root
Net::FTP=GLOB(0x2017b5e8)<<< 331 Password required for root.
Net::FTP=GLOB(0x2017b5e8)>>> PASS ....
Net::FTP=GLOB(0x2017b5e8)<<< 230 User root logged in.
Net::FTP=GLOB(0x2017b5e8)>>> CWD /tmp
Net::FTP=GLOB(0x2017b5e8)<<< 500 '': command not understood.
Net::FTP=GLOB(0x2017b5e8)>>> TYPE I
Net::FTP=GLOB(0x2017b5e8)<<< 250 CWD command successful.
Net::FTP=GLOB(0x2017b5e8)>>> PORT 10,128,128,100,162,190
Net::FTP=GLOB(0x2017b5e8)<<< 200 Type set to I.
Net::FTP=GLOB(0x2017b5e8)>>> STOR stores.txt
Net::FTP=GLOB(0x2017b5e8)<<< 200 PORT command successful.
Net::FTP=GLOB(0x2017b5e8)>>> QUIT
Net::FTP=GLOB(0x2017b5e8)<<< 425 No data connection


 
I think cwd should be cd

Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top