Operating System - HP-UX
1819818 Members
3221 Online
109607 Solutions
New Discussion юеВ

Re: Shell script that executes FTP in cronjob

 
SOLVED
Go to solution
Ngoh Chean Siung
Super Advisor

Shell script that executes FTP in cronjob

How to write a shell script that executes FTP?

Currently I have 2 servers (Pluto & Mercury).
I want to ftp a file (abc.unl) from pluto to mercury every morning at 9:00am. So, is there any script that can ftp this file through cronjob? I already know how to set the cronjob but just not sure about the script only.

regards.
12 REPLIES 12
Sanjiv Sharma_1
Honored Contributor

Re: Shell script that executes FTP in cronjob

Hello,

Run the script on Pluto.
The script should be:

Name of the script: ftp.sh

Contents of the script:
#!/usr/bin/ksh

PATH=${PATH}:(make it as per your requirement)
export PATH

SERVER=`hostname`

ftp -i -n -v mercury << EOF
user root root123(password of root)
lcd /ss
put abc.unl
bye
EOF

You can use any user other than root too.

hth.
Everything is possible
Michael Schulte zur Sur
Honored Contributor

Re: Shell script that executes FTP in cronjob

Hi,

if you don't want to put the file into the user's home dir then you have to insert a
cd /dir
before the put.

greetings,

Michael
Elmar P. Kolkman
Honored Contributor

Re: Shell script that executes FTP in cronjob

If you are going to use ftp for this, you need something like an .netrc file. But that means you have a security issue, because you will have to have the password of the remote user un-encrypted in the script or .netrc file.

A better way might be to use scp. By distributing the public key, you can copy files without using the password.
Every problem has at least one solution. Only some solutions are harder to find.
John Kelly_3
Regular Advisor

Re: Shell script that executes FTP in cronjob

#!/usr/bin/ksh
#
/usr/bin/ftp -vn mercury < /tmp/ftp.log 2>&1
user
asc
lcd
cd
put
bye
EOM
/usr/bin/grep -q "^[45]" /tmp/ftp.log
if [ $? = 0 ]
then
echo "Errors found. Check FTP log"
exit 1
else
exit 0
fi
# end of script
Ngoh Chean Siung
Super Advisor

Re: Shell script that executes FTP in cronjob

Hi Sanjiv,

Still hv some questions.

Let said the file (abc.unl) in pluto is at /tmp/jr and I want to ftp to mercury /tmp/jr as well.

1) Inside the script, I need to put this line as well??? #!/usr/bin/ksh
What does it means?

2) For my case, means that PATH=${PATH}:(make it as per your requirement) should be PATH=$/tmp/jr ?

3) What is this command means? lcd /ss
Can I put other path?

regards.
Michael Schulte zur Sur
Honored Contributor

Re: Shell script that executes FTP in cronjob

Hi NCS,

let me clarify it.
What Sanjiv posted is a ftp script within a korn shell script.
#!/usr/bin/ksh
means this script is to be run by korn shell
You do not need the PATH statement, if you explicitely run all commands with full path.
lcd sets the local directory to get the file from.
cd sets the directory on the remote host to put the file to.

you can specify any existing directories on your hosts as long as you have the necessary rights.

greetings,

Michael
Ngoh Chean Siung
Super Advisor

Re: Shell script that executes FTP in cronjob

So, the script is likes:

#!/usr/bin/ksh

ftp -i -n -v mercury << EOF
user
asc
lcd /tmp/jr
cd /tmp/jr
put abc.unl
bye
EOF

1) What is asc stands for?

2) If I want to ftp more than 1 file, I need to key in bin and then follow by prompt. Then I can use mput command, right. So, where should I put in the bin and prompt command? Btw, what are bin and prompt stand for?

3) Is bye and quit command same? I think in FTP, we should use bye, right?

4) What is the difference among EOF, EOM and EOD?

regards.
John Kelly_3
Regular Advisor
Solution

Re: Shell script that executes FTP in cronjob

1. asc is short for ascii. This tells ftp that the files for transfer are text files. Use bin for data files or executables.

2. bin - see 1. If you don't want prompts when transferring multiple files (using mput) add -i in the ftp command line. E.g. ftp -i -n -v hostname.

3. bye and quit do the same thing.

4. In the examples, EOM and EOD are start and end markers for the ftp commands. It could be any text string.
Ngoh Chean Siung
Super Advisor

Re: Shell script that executes FTP in cronjob

May I know what is the different between Korn shell script and POSIX shell script? What I know is Korn shell script is to be executed by /usr/bin/ksh and POSIX shell script is to be executed by /usr/bin/sh. Any other things besides this?

Inside the script, we need to determine whether the script is executed by Korn or POSIX. Example: #! /usr/bin/ksh

Is there any setting that we can make to enable every script that we write will be executed by POSIX shell script only without specify inside the script?

regards.
Naveej.K.A
Honored Contributor

Re: Shell script that executes FTP in cronjob

Hi,

The difference is there in the shell. POSIX shell is the default shell from HPUX 10.2 and above. POSX shell is /usr/bin/sh while the Korn shell is /usr/bin/ksh.

By default, the script gets executed in the POSIX shell or the shell defined to the user.

With best wishes
Naveej
practice makes a man perfect!!!
Ngoh Chean Siung
Super Advisor

Re: Shell script that executes FTP in cronjob

Hi,
What you mean is actually there is no different either the script was executed by POSIX or Korn. The result is still the same. Am I right?

regards.
Naveej.K.A
Honored Contributor

Re: Shell script that executes FTP in cronjob

Hi,

Yes exactly, i don't find any difference in the way features that POSIX and KORN shells have. Both has command history, command line editing, arithematic evaluation, coprocess facility....

The root user's shell is the posix shell always, as there is no /sbin/ksh..

regds
Naveej
practice makes a man perfect!!!