Operating System - HP-UX
1825773 Members
2214 Online
109687 Solutions
New Discussion

Re: Problem with FTP script

 
Sean OB_1
Honored Contributor

Problem with FTP script

Hello. I'm writing a quick script to ftp a bunch of files to all of our servers. The script fails on login, however I can login fine manually executing the exact command sequence that is in the script. Any help is appreciated.

Here's the script.


for SERVER in `cat /tmp/servers`
do
echo "FTP to $SERVER"
sleep 1
ftp -in <<-EOF
open $SERVER
user
ascii
lcd /tmp
cd /tmp
put file1
put file2
chmod 700 /tmp/file2
bye
EOF
done
13 REPLIES 13
Steven E. Protter
Exalted Contributor

Re: Problem with FTP script

The nasty part is I don't know any way to check return codes.

Perhaps there is a non-visual junk character in the username or password?

The script looks good.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
H.Merijn Brand (procura
Honored Contributor

Re: Problem with FTP script

Would this work?

for SERVER in `cat /tmp/servers`
do
echo "FTP to $SERVER"
sleep 1
ftp -in <<-EOF
open $SERVER
user

ascii
lcd /tmp
cd /tmp
put file1
put file2
chmod 700 /tmp/file2
bye
EOF
done

based on the fact that if a user needs a password, the ftp server will prompt for it with something like

user blah needs a password:

Enjoy, Have FUN! H.Merijn [ who would use perl -MNet::FTP ]
Enjoy, Have FUN! H.Merijn
Sean OB_1
Honored Contributor

Re: Problem with FTP script

Nope, no non display chars, retyped the line and all.

breaking the user line to

user


causes the script to pause and ask for my password. I can type in the same password in the script and it continues fine and sends the files.

Sridhar Bhaskarla
Honored Contributor

Re: Problem with FTP script

Hi Sean,

I would try couple of checks.

1. Try with hardcoding the servername in side the ftp script. Take out the for loop.

2. Run it with -v and look at the session log. See if the password is really being passed.

I copied and pasted your script without the for loop and it is working just fine for me.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Sean OB_1
Honored Contributor

Re: Problem with FTP script

The username is being passwd, and it fails even outside of the for loop.

Here is the result:

root@cosmo0:/tmp-> ./ftp1
Connected to
220-####################################################
220-# #
220-# This is a restricted server. Only authorized #
220-# users may access this server. All activity is #
220-# monitored and any unauthorized use will be #
220-# reported to the appropriate authorities. #
220-# #
220-####################################################
220-
220-
220 FTP server ready.
Remote system type is UNIX.
Using binary mode to transfer files.
331 Password required for .
530 Login incorrect.
Login failed.
200 Type set to A.
530 Please login with USER and PASS.
530 Please login with USER and PASS.
530 Please login with USER and PASS.
530 Please login with USER and PASS.
221 Goodbye.
Sridhar Bhaskarla
Honored Contributor

Re: Problem with FTP script

Hi,

Looks like it didn't like the password. Try with a simple password to test your ftp script.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Paul Cross_1
Respected Contributor

Re: Problem with FTP script

Not all ftp servers are the same. When logging into a solaris ftp server, I got the same result as you... when logging into a linux ftp server your script worked just fine. I would use expect for this.
David Burgess
Esteemed Contributor

Re: Problem with FTP script

This is the guts of a script I use to send error messages to a central server :-

file=test.txt
ftplogfile=/tmp/ftplogfile.log

ipaddress=yourhostname

ftp -nv <>$ftplogfile 2>>$ftplogfile
open $ipaddress
user username password
ascii
cd /remote_directory
put $file remotefilename
site chmod 770 remotefilename
bye
EOF

You'll need to substitute yourhostname, username, password, remote_directory, remotefilename

Works every time. One thng to watch out for is sometimes indenting the lines between EOF's can upset ftp. I always left justify the block even inside a loop.

Regards,

Dave.
Mark Greene_1
Honored Contributor

Re: Problem with FTP script

Sean

Run the FTP command with the -i option to disable interactive prompting. Your script with the ID and password on the same line should then work ok.

mark
the future will be a lot like now, only later
Chris Vail
Honored Contributor

Re: Problem with FTP script

I don't like FTP for the very reason that it is so difficult to script. And if you do get a script to work, maintenance is difficult (as you've found out). I recommend that you use secure copy instead. This is secure and it scripts nicely: you can test for return codes, and your script doesn't have to have any passwords in it at all--always a security risk. Get and install secure copy (ssh) on all your systems. Its a PITA to configure, but once you get it done, maintenance is negligible.


Chris


H.Merijn Brand (procura
Honored Contributor

Re: Problem with FTP script

OK, if nobody does it, here's a perl script with a lot of error catching and debugging

Also attached for formatted codeis not supported yet

#!/opt/perl/bin/perl

use strict;
use warnings;

use Net::FTP;

@ARGV = ("/tmp/servers");
while (<>) {
chomp;
my $ftp = Net::FTP->new ($_, Debug => 3) or
die "$_: cannot connect: ", $ftp->message;
$ftp->login ('user', 'password') or
die "$_: cannot logon: ", $ftp->message;
$ftp->ascii or
die "$_: cannot set ascii mode: ", $ftp->message;
chdir "/tmp" or die "/tmp: $!\n";
$ftp->cwd ("/tmp" or
die "$_: cannot chdir to /tmp: ", $ftp->message;
foreach my $file (qw( file1 file2 )) {
$ftp->put ($file) or
die "$_: cannot put $file: ", $ftp->message;
# I have no idea how to do chmod in Net::FTP
# chmod 700 /tmp/file2
$ftp->quit;
}

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: Problem with FTP script

Some embarrasing typo's is what you get when you post half asleep and don't test.

#!/opt/perl/bin/perl

use strict;
use warnings;

use Net::FTP;

@ARGV = ("/tmp/servers");
while (<>) {
chomp;
my $ftp = Net::FTP->new ($_, Debug => 3);
$ftp or die "$_: cannot connect: $@";
$ftp->login ('user', 'password') or
die "$_: cannot logon: " . $ftp->message;
$ftp->ascii or
die "$_: cannot set ascii mode: " . $ftp->message;
chdir "/tmp" or die "/tmp: $!\n";
$ftp->cwd ("/tmp") or
die "$_: cannot chdir to /tmp: " . $ftp->message;
foreach my $file (qw( file1 file2 )) {
$ftp->put ($file) or
die "$_: cannot put $file: " . $ftp->message;
# I have no idea how to do chmod in Net::FTP
# chmod 700 /tmp/file2
$ftp->quit;
}

Should be better.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Kirby A. Joss
Valued Contributor

Re: Problem with FTP script

Creating a $HOME/.netrc file has always proved the best among the unattractive options for me in this situation. man 4 netrc