Operating System - HP-UX
1832473 Members
2806 Online
110043 Solutions
New Discussion

Re: script to ftp file from HP-UX server to a windows 2003 server

 
SOLVED
Go to solution
Helen Herring
Frequent Advisor

script to ftp file from HP-UX server to a windows 2003 server

I have an existing script that ftps a database file from our HP-UX server to another HP-UX server and is executed every night using cron. I do not have user/password for the receiving server in the script. I need to ftp same file to a window 2003 server but need to include authentication for the windows 2003 server (NOT anonymous access) in the script. How is this specified in the script?
12 REPLIES 12
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: script to ftp file from HP-UX server to a windows 2003 server

My weapon of choice for this, unless high security is needed (in which case ssh and/or sftp is the answer) is Perl's Net::FTP module. It makes FTP transfers painless AND does error checking.

The attached script, ftpput.pl, would login to a remote server and put the files listed on the command line.

ftpput.pl myfile1 myfile2
would put both of these files and return a 0 exit code upon success.

Note in the code where the login is done. Yopu could simply supply the user and the password could be read from the .netrc file. That section of code is commented. Man .netrc, Net::FTP for details. I think that you will find that this method is much cleaner than shell scripting for FTP operations especially if you deal with those pesky little trivial issues like checking for errors.




If it ain't broke, I can fix that.
Fragon
Trusted Contributor

Re: script to ftp file from HP-UX server to a windows 2003 server

That's easy:

# more putdbfile.sh
#!/usr/bin/sh
ftp -n IP_OF_Windows2003 <
Helen Herring
Frequent Advisor

Re: script to ftp file from HP-UX server to a windows 2003 server

The file name changes daily so this is what I have:
ftp -n ip_address_of_server ,,!
user USERNAME PASSWORD
binary
cd $rdmpdir (generated in the unix script, it is the dir on the windows server)
put $dmpfil (generated in unix script, changes daily)
bye
!

Can I cd to the required directory? If not what command do I need to use?
Helen Herring
Frequent Advisor

Re: script to ftp file from HP-UX server to a windows 2003 server

Looks like script ran and connected to win server but now receiving message could not find the directory. I had the dir in double quotes. It ignored the \s.
I appreciate your help.
Paula J Frazer-Campbell
Honored Contributor

Re: script to ftp file from HP-UX server to a windows 2003 server

Kathy

Run your script so:-


ksh -x <scriptname>

It will show yoy where it is failing.

I would also echo back the $rdmpdir to confirm thjat it is correct, maybe it requires the full windoze path c:\bla\bla\dir.

HTH

Paula
If you can spell SysAdmin then you is one - anon
Helen Herring
Frequent Advisor

Re: script to ftp file from HP-UX server to a windows 2003 server

my problem is the substitution variables.
unix script generates the file and directory but i do not know how to pass to windows.
previous part of script that generates the DAY portion of file not shown. this script currently runs oK going from hp server to hp server but I'm getting errors on the $dmpfil on the windows server. I tried using double quotes around the "$dmpfil" in the put command. See below:

dmpfil="RP5470full_${Day}.dmp.Z"
ldmpdir="/u08/oracle/bob/exp/"


cd $ldmpdir
ftp -n IP_ADDRESS_OF_SERVER << ENDFTP
user USER PASSWORD
cd "E:\oracle\oradata\alice\RP5470_SAVE"
put $dmpfil
bye

ENDFTP
Bill Hassell
Honored Contributor

Re: script to ftp file from HP-UX server to a windows 2003 server

ftp has a LOT of commands available, cd and lcd and mkdir, etc. Try typing in the ftp commands in your script by hand to see the error messages. For instance, if the E:\ directory does not exist, you'll get an error. If you need to create it, create a variable in your script, then add the appropriate mkdir commands (mkdir in ftp does not have a recursive feature). Then you can cd to the directory.

Another item is that you are using the shorthand version of put. put normally requires 2 parameters, source and destination. If you leave out the destination then the source is repeated for the destination. In your case, just the filename is sent so as long as the filename is valid (never assume that special characters are valid for filenames) then the file should be created. But of course, the Windows system may have permission issues, etc.

Also, never assume you have the right mode (ASCII or BINARY). Always explicitly code the translation method in your script. Since this is a .Z file, you'll need BINARY to prevent corruption of the file at the Windows side.


Bill Hassell, sysadmin
Helen Herring
Frequent Advisor

Re: script to ftp file from HP-UX server to a windows 2003 server

I checked the windows server and the file had been created on the windows server. But Yes, I need to specify the destination file name which needs to be a .zip file.
How can I specify or change the filename?

put $dmpfile variablefilename.zip

Thanks to all of you for taking the time to help.
Mark Landin
Valued Contributor

Re: script to ftp file from HP-UX server to a windows 2003 server

What you might do, since your FTP command file is full of variables, is to write a script that writes the command file, and then passes it to FTP.

Something like this:

USER=user
PASSWORD=password
CMDFIL=/tmp/ftp.cmd
FILENAME=myfile
TGTFILENAME=newfile.zip

echo "open server" > ${CMDFIL}
echo "user ${USER} ${PASSWORD}" >> {$CMDFIL}
echo "cd ${DIRECTORY}" >> ${CMDFIL}
echo "put ${FILENAME} {$TGTFILENAME}" >> ${CMDFIL
eval "ftp -n < ${CMDFIL}"

This way all the complicated logic about filenames, etc, can be done in the script .. the ftp command file only has literal values in it.
Helen Herring
Frequent Advisor

Re: script to ftp file from HP-UX server to a windows 2003 server

Mark, good idea.
My last attempt included the following:
In the unix script, I determined the file and then uncompressed the file on the unix server. Setting the variable again for just the uncompressed file name. Then the ftp portion of the script executed. It ran, but the total kb is different on the windows server than what is displayed on unix. I will look into your suggestion. I'm going to make a couple more tests but it looks like it is working. Like I said, I've just got to verify the ftp'd file size is correct. Anyway, I really appreciate yours and everyone's help. In researching, looks like the windows command to compress is compact? I'll give that a try. I want to automate all this.
Mark Landin
Valued Contributor

Re: script to ftp file from HP-UX server to a windows 2003 server

I wouldn't worry too awful much about filesizes matching exactly.

If you are dealing with text files, then you may need to do a "ux2dos" on the UNIX file first. If you are dealing with compressed files, like .Z or .gz files, then you may not need to uncompress them before sending them to Windows .. many of the Windows zip utilities will also read .Z and .gz files with no problem.

Don't forget to hand out points to all those people you are wishing to express your gratitude for. :)
A. Clay Stephenson
Acclaimed Contributor

Re: script to ftp file from HP-UX server to a windows 2003 server

Okay Kathy, I'll post the version of ftpput.pl that I typically use.

1) In your home directory (or the sender's home directory) create a file called .netrc with 600 (rw-------) permissions owned by the sender.
Have an entry like this:
machine remotehost login cstephen password topsecret account mostsecret

The account data is required on boxes which add an extra layer of authentication. You probably won't need that. Man .netrc for details.

2) cd to the desired directory on the local host

3) ftpput.pl -h remotehost -B -d dirname -t 4 -v file1 file2 file3
STAT=${?}

That will ftp to remotehost, attempt to login as the current user using password and account data in .netrc. -B specifies binary xfers, -d dirname cd's to dirname on the remote host, -t 4 tells it to try to send a file up to 4 times before giving up. -v set verbose. Finally, file1 file2, file3 are sent. You don't have to check file sizes or do checksums, if ${?} is zero then everything (login,hostname,xfers) was okey-dokey.

Invoke this puppy as ftpput.pl -u for full usage.
If it ain't broke, I can fix that.