1842297 Members
2811 Online
110188 Solutions
New Discussion

Re: automatic ftp

 
SOLVED
Go to solution
bossuyt_2
Occasional Advisor

automatic ftp

I want to use ftp in a script sh or ksh
I have defined an automatic open ( .netrc)
So my shell is esay like that :
#!/bin:ksh
ftp toto
cd dir
put fic
bye
EOF

when I execute this I anm under
ftp >

So I want to execute automatics commands !

Thanks if you know . . .
8 REPLIES 8
Rodney Hills
Honored Contributor

Re: automatic ftp

Use the -n option on ftp and << to include the lines in the shell script as input to ftp. Example-

ftp -n ftp.somesite.com <user anonymous user@mysite.com
ls
quit
EOD

-- Rod H
There be dragons...
Shannon Petry
Honored Contributor

Re: automatic ftp

If you read up on the man pages for netrc, you will find that you can define a macro "init". This is executed at all server logins. I have in the past used init to login, cd, get, put, delete, and more! You should not have to issue other commands through a ksh, or any other shell, just use netrc to execute it all. This saves a whole lot of debugging later!

Regards,
Shannon
Microsoft. When do you want a virus today?
Dan Bonham
Advisor
Solution

Re: automatic ftp

Don't use ftp -n if you are going to use the .netrc file. the -n negates auto login.

also, I tend not to use the macros in the netrc because that give you two files to maintain if you need to make changes. here is what I do.
Make your entry into .netrc
in your script use
ftp 11.11.11.11 << EOF
get ***
put ***
bye
EOF
You can also use variables as well
ftp 11.11.11.11 << EOF
get $newfile
put $oldfile
bye
EOF

Rusty Sapper
Michael F. Dick
Advisor

Re: automatic ftp

Well, why use shell script (If you have to use it use expect).

I would use perl (5.x) with the ftp module. this gives you also the possibility to hide the password in a root-only readable file. The nice thing about the perl solution is, that all the error checking is in the module.

Anyway, just a thought
Well, thats all just my $.02
A. Clay Stephenson
Acclaimed Contributor

Re: automatic ftp

Hi:

I have to agree with Micheal.

When I have to do ftp scripting these days, I use perl with the Net::FTP module (available for download at www.perl.org/CPAN). It really makes FTP operations very simple. Any error conditions and timeouts are easy to test for and set. It you don't know perl, now would be a good time to learn.

It's usually something as simple as this (with error checking omitted):

use Net::FTP;

$ftp = Net::FTP->new("remotehost.name", Debug => 0);
$ftp->login("anonymous",'cstephen@xxx.com');
$ftp->cwd("/downloads");
$ftp->get("testfile.txt");
$ftp->quit;


Another nice feature is that this same script works in the Windows world as well.


Clay
If it ain't broke, I can fix that.
Radu Swider_1
Occasional Contributor

Re: automatic ftp

make a shell script with the following lines:

echo "open the.ip.of.machine \user username password \[bin|ascii] \[get|put] file \quit" | ftp -n -i -v >> ./ftp.logfile
Mike Brown_3
Frequent Advisor

Re: automatic ftp

Another (slight) variation on the ksh theme if you want to send multiple files;

#!/bin/ksh
... setup your variables here ...

# If all the files to be ftp'd are in multiple
# locations, get an 'ls' listing of these and
# record them in a file, $FILELIST.
# Move the $FILELIST files into one temp area.

for i in `cat $FILELIST`
do
mv $i $FTPDIR
done

# ... and then ftp them ...
cd FTPDIR
{ echo "open TARGET-NODE
mput *
close"
} | ftp -v 2>&1 | tee FTPLOG

... using tee to log the process.
There's no substitute for experience
Magdi KAMAL
Respected Contributor

Re: automatic ftp

Hi Bossuyt,

You need to redirect you standard iput like the following :

#ftp <put
get
END

: stand for either IP address or hostname.

Magdi