Discussion Locked

FTP using netrc file

0 helpful
9 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I'm trying to write a script that will transfer files via FTP Automatically off of the crontab. I created the netrc file and stuck it in root dir. When I try to FTP to the site I'm still prompted for a usrname and passwd. I don't want my script to have to go through the authentication. I want the netrc file to pass the necessary credentials to the remote FTP site. I want to get to a ftp> prompt and have the script start issueing ftp commands after that. How is that done?

9 replies

by gazz

This is what worked for me. Hope it works for you.
a) Create a .netrc file in your home directory.

# sample .netrc
machine sco login joeuser password MyPass123

**The permissions should be set to "600" with the chmod command.

(b) Next, create the following bourne shell script:

#cut here
#Make sure that it is executable
# In this example "sco" is the machine that I am ftp'ing to.
# 1) changes directory to /etc
# 2) executes an ls of the /etc directory on sco.
# 3)Toggles prompt mode to be turned off (it is on by default), otherwise, the
# ftp interface will prompt the user with a yes or no action for all file
# transfers.
# 4) Transfers the message of the day (/etc/motd) from the machine sco to the
# local machine under the name /tmp/sco.motd on my machine.
#
# note the eof's : when you have << eof you must have a eof on a line by itself
# in the shell script. This is a standard shell programming method when you
# wish to write shell scripts with interactive programs.
ftp sco << eof
cd /etc
ls
binary
hash
prompt
get motd /tmp/sco.motd
eof
# comments are allowed again...
echo all done with shell script
#cut here

To run this shell script perform these actions:

chmod 755 file_name_of_the_shell_script

type "file_name_of_the_shell_script" and the file will be transferred. Type
file_name_of_the_shell_script > /dev/null so that output will not echo to the
screen, or to a logfile, if you wish to log error messages.

by btidwell

The file is actually .netrc and it needs a permission of 400 (read only by owner). Also the file needs to be in the home directory.

Another possibility is to code the user/password combination in the script. Such as

ftp -n << EOF
user abc password
open server_name
put $1 /dir_to_ftp_to/$1
...
quit
EOF

by dberger92

1.
On SCO UNIX, root is not allowed to use ftp by default config. which is security wise.
Use .netrc and .rhosts in the regular user home directory - user that will use crontab.

2.
chmod 600 .netrc .rhosts in $HOME of user

3.
I have a script who uses ftp and/or rcp so root can use this script to transfer files.
Also it takes many files to transfer as parameters. You can GET or PUT files to another node.
Syntax:
u_ftp [put|get]

If you want it, email me.denis_berger@yahoo.com

This discussion is closed to new replies.