Operating System - HP-UX
1753454 Members
6280 Online
108794 Solutions
New Discussion юеВ

How to write a script to FTP

 
SOLVED
Go to solution
Narasimham
Advisor

How to write a script to FTP

I am working on HP-UX 9000/800 servers.

How to write a script to FTP a file from
1> one server to another server in same network
and
2> To a NT desktop (different network).
Anybody can send a sample file for the script.


Thanks in advance

Narasimham

12 REPLIES 12
Paula J Frazer-Campbell
Honored Contributor
Solution

Re: How to write a script to FTP

Hi

######################################
# Send the files to the new server
ftp -n 172.20.20.17 << End_of_Ftp
user
prompt
put
bye
End_of_Ftp


Will do an automatic ftp


Paula
If you can spell SysAdmin then you is one - anon
James R. Ferguson
Acclaimed Contributor

Re: How to write a script to FTP

Hi:

Here are two scripts:

#!/usr/bin/sh
ftp -n << EOF
verbose
open thehost
user uuu ppp
get /tmp/stuff
close
EOF

...

#!/usr/bin/sh
{ echo "open thehost
user uuu ppp
hash
mput *
close"
} | ftp -i -n -v 2>&1 | tee /tmp/ftplog.$$

Regards!

...JRF...
Paula J Frazer-Campbell
Honored Contributor

Re: How to write a script to FTP

Hi

See man ftp as you may have to set in in binary mode to get a claen ftp.

Paula
If you can spell SysAdmin then you is one - anon
A. Clay Stephenson
Acclaimed Contributor

Re: How to write a script to FTP

HI:

In the past, I used to do this stuff in the shell but now I NEVER do because I have found a much cleaner way to do it that makes error trapping duck soup. Do this stuff in Perl using the Net::FTP module which is available from http://www.cpan.org .

Here's how simple it can be:

#!/usr/bin/perl -w

use Net::FTP;
use strict;

my $ftp = Net::FTP->new("remotehost",Debug => 0);
$ftp->login("cstephen","top_secret");
$ftp->cwd("/tmp");
$ftp->get("myfile");
my $stat = $ftp->status;
my $full_stat = $ftp->code;
# $stat contains the first digit; usually all
# that you need to do is test if it is equal
# to 2. $full_stat contains the full 3-digit
# value but is seldom needed
printf("Status: %d Full Status: %d\n",$stat,$full_stat);
# Sample Test
if ($stat == 2)
{
print "Get was good\n";
}
else
{
print "Get was bad\n";
}
$ftp->quit;

I think if you start using this module you will never go back to shell scripting FTP. Moreover, these same scripts will run in the NT world. You can download a free version of perl for windows at http://www.activeperl.com.
Note that the answer is now the same on UNIX and NT and on the same subnet or across the net.


Notice that this method easily handles the error checking. If you like, you can use the shell for most of your script and simply use a bit a perl for the actual FTP transfers. In that case add the statement exit($stat) to the perl script and then your shell script does have a valid status indication.

Regards, Clay
If it ain't broke, I can fix that.
Cara Tock
Regular Advisor

Re: How to write a script to FTP

You can also use a tool called expect. It is a tool for automating interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, etc. It can create scripts for you using autoexpect. You can find the package at http://expect.nist.gov/.
Narasimham
Advisor

Re: How to write a script to FTP

Hi..
i could do the FTP between unix machines using Paula's sample script.

waiting for some sample script for FTP from UNIX to NT desktop

Thank you all
Narasimham
James R. Ferguson
Acclaimed Contributor

Re: How to write a script to FTP

Hi again:

The scripts already offered should work for Unix to NT. The key issue is transfer your text files in ASCII mode. In this fashion, Unix "newlines" are automatically translated to Windows/DOS linefeed/carriage-return pairs, or vice versa in the opposite direction.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: How to write a script to FTP

Hi:

If you are going to transfer to a Windows box then you must have an FTP server service running on the NT side. This is not a standard product but there are packages available. However, if you turn the question around and let the Windows box serve as a client to transfer the file then all you need is the above Perl script. This way the same script works for both platforms.
If it ain't broke, I can fix that.
Cara Tock
Regular Advisor

Re: How to write a script to FTP

I use this method to transfer files from Unix to NT. Put a file called .netrc in the home directory of the user transfering files. Then put the following in that file :
machine "machine name" login "user" password "password"
(/home/user/.netrc with r-------- rights only chmod 400 .netrc)
Then the followind script will work
ftp hqmediware << EOF
put "file"
bye
EOF