LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Fedora
User Name
Password
Fedora This forum is for the discussion of the Fedora Project.

Notices


Reply
  Search this Thread
Old 03-17-2008, 09:24 AM   #1
drwjr
LQ Newbie
 
Registered: Mar 2008
Posts: 2

Rep: Reputation: 0
Unhappy Having problems with automated ftp in Fedora Core 7


I need to have cron schedule an ftp job every day to transfer a file. In /root, I have a .netrc file and I also have a shell script file that uses the info in the .netrc as it's input to provide ftp with what it needs to accomplish. Now, this works without problems in BSD 4.2 but it doesn't seem to work correctly under Fedora Core 7. I need to get it working, so I'd appreciate any assistance with regards to why it isn't working.

I've tried both of the following two entries in the crontab file:

00 8 * * * root echo "$ accesslogupload" | ftp 151.132.211.153
00 8 * * * root /root/fto_accesslog_script

The ftp_accesslog_script contains:

#!/bin/bash
echo "$accesslogupload" | ftp 151.132.211.153

And the .netrc file contains:

machine 151.132.211.153
login ftpuser2
password password2

macdef accessupload
cd Incoming
cd dansguardian
lcd /var/log/dansguardian
bin
put access.log
close
bye

Now, if I go into a terminal session and manually run the ftp_accesslog_script, it runs fine. But whenever cron runs it, I get a login failure. And the results are the same regardless of which of the two entries, above, that I try to use in the crontab file. Anyone got any ideas why I keep getting a login failure when cron tries to run this automated ftp but when I run it manually, it runs fine?
 
Old 03-18-2008, 01:12 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
In the 1st cron entry, you've got a space between $ and accesslogupload, in the 2nd you've misspelt fto_accesslog_script.
Also, you never set a value for the bash var $accesslogupload, so it's not going to work.
 
Old 03-18-2008, 08:20 AM   #3
drwjr
LQ Newbie
 
Registered: Mar 2008
Posts: 2

Original Poster
Rep: Reputation: 0
chrism01,

The fto_accesslog_script is just a typo in my message here. In the crontab file, it says ftp_accesslog_script. As for the space between the $ and the word accesslogupload, that's the correct format in BSD 4.2 and it works just fine under that OS. So, you're saying that this is not the correct format for Fedora? Ok, no problem. I'll change it and try again. But don't forget, I already said that this entry (with the space between the $ and the word accesslogupload) is exactly what I have in the ftp_accesslog_script file. And when I run that script file, manually, from a shell prompt, by typing in the script name and hitting Enter, it runs perfectly. It doesn't fail. It ONLY fails when CRON runs it. As for a variable, I guess you aren't familiar with the use of the .netrc file. The $ accesslogupload refers to a section within the .netrc file that specifies what commands ftp is suppose to run. If you look at my original message, you'll see a macdef (machine definition) called accesslogupload. That is what the $ accessupload is referring to. Take a look at this URL for a better explanation than I can give:

http://www.linux.com/feature/119510?theme=print (the contents of that page is below)

Automate FTP with macros
October 05, 2007 (4:00:00 PM) - 5 months, 2 weeks ago
By: Mark Alexander Bain

Has it been a while since you used FTP from the command line? While there are decent GUI-based FTP clients (such as gFTP), you can automate operations with the command-line version and handle file transfers with no user interaction at all.

When you try connecting to an FTP server and specify a username, FTP won't do anything unless you type in your password, or specify it in the connection command, as in ftp bainm:myftppassword@192.168.1.2. You can take advantage of this syntax to automate your FTP transfers using a pipe:

echo get testfile.txt | ftp://bainm:myftppassword@192.168.1.2
Unfortunately, this doesn't work for every distro. For example, it's fine on SUSE but not on Red Hat or Debian. Therefore, you need a solution that works for all distros.

Create a file that contains the IP address or name of the machine that you want to access, your username, and your password, and save it as ~/.netrc:

machine 192.168.1.2
login bainm
password myftppassword
With ~/.netrc in place, you can send a command to FTP without having to input a password manually:

echo get testfile.txt | ftp 192.168.1.2
Connected to 192.168.1.2.
220 acamas.ilium FTP server (Version 6.4/OpenBSD/Linux-ftpd-0.17) ready.
331 Password required for bainm.
230 User bainm logged in.
Remote system type is UNIX.
Using binary mode to transfer files.
local: testfile.txt remote: testfile.txt
227 Entering Passive Mode (192,168,1,2,9,203)
150 Opening BINARY mode data connection for 'testfile.txt' (0 bytes).
226 Transfer complete.
221 Goodbye.
At this point, you may want to think about creating a shell function that can carry out data transfers for you -- for example:

function compileFTP {
cat <<STOP
prompt off
lcd $1
#Don't worry if the target directory already exists - FTP won't complain:
mkdir $2
cd $2
mput *
STOP
}
You can then call the function with a command like:

compileFTP ~/webfiles webdirectory | ftp 192.168.1.2
FTP runs the commands in the function without you having to input a password. This means that you can automate any FTP operations that you need to carry out. For example, you might want to save the code from above into a file, make it executable with a command like chmod +x ~/FTP/updateWeb, then run that file daily at midnight using cron:

0 0 * * * ~/FTP/updateWeb
You're not limited to writing functions in shell scripts -- you can also write them for FTP. However, in that case, they're called macros.

If you want to write an FTP macro, you'll need to go back to the .netrc file, because that's where the macros are stored. The rules for writing a macro are simple:

Start a new macro definition by using the macdef keyword.
Write your FTP commands.
End the macro definition by adding a blank line.
For example, you could add the following code to .netrc:

macdef updateWeb
prompt off
lcd /home/bainm/webfiles
mkdir webdirectory
cd webdirectory
mput *

Don't forget that blank line!

You can call a macro from the Linux command line, but in a slightly different manner than the way you call a shell function:

echo "$ updateWeb" | ftp 192.168.1.2
You have to include a $ in the command to be sent to FTP to tell FTP that the command is a macro. Now you can change the crontab entry accordingly:

0 0 * * * echo "$ updateWeb" | ftp 192.168.1.2
At this point, you're probably wondering about variables -- you don't want to have to create a new macro for every upload that you want to carry out. Fortunately, macros use variables the same way shell functions do. The macro, therefore, becomes:

macdef updateWeb
prompt off
lcd $1
mkdir $2
cd $2
mput *

You can now send the names of the directories to your macro rather than hard-coding them:

echo "$ updateWeb /home/bainm/webfiles webdirectory" | ftp 192.168.1.2
You can also create another macro that supplies the inputs to your generic one (remembering to use the $ to tell FTP that you're calling a macro):

macdef myWebUpdate
$ updateWeb /home/bainm/webfiles webdirectory

What we're doing here is defining the new macro by using the macdef keyword, then calling our original macro (updateWeb) and supplying it with the directories that we want to copy from and to (i.e. from /home/bainm/webfiles on the local machine to webdirectory on the server. You can now run the macro from the command line:

echo "$ myWebUpdate" | ftp 192.168.1.2
Of course, you're not going to throw away your favorite FTP GUI. However, with some command line magic, you can automate FTP and run it with no human intervention at all. You can go home each evening knowing that all your Web servers will be updated or all your files backed up -- all through the power of FTP macros and the .netrc file.

Read in the original layout at: http://www.linux.com/feature/119510
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
automated ftp sang_froid Programming 6 05-07-2007 10:53 AM
automated ftp Anthraxnz Linux - Newbie 4 10-15-2005 09:36 PM
FTP in Fedora Core 2 Thommy Linux - Newbie 8 09-27-2004 04:34 AM
Severe problems with Fedora Core 1 and Fedora Core 2 installatiom TheOneKEA Fedora - Installation 2 07-14-2004 02:12 PM
Automated FTP Upload Zingaro2002 Linux - Networking 7 05-05-2004 01:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Fedora

All times are GMT -5. The time now is 07:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration