Help Simple FTP Script Here Syntax

I have a list of IP address and want to be assess whether FTP is allowing
FTP access. I don't want to use lousy NT shell, but cannot get the syntax down on this. ftphosts.txt is a simple list of IP adresses.
I want to iterate through the IPS and do a simple

ftp IPadress
user ftp password test@abc.org
Then lcd to c:\investigate and get all the files in the root.
Please help me with this syntax. I am running this in a Cygwin bash shell.
Thak you in advance for your help!
Gregg Dotoli

#!/bin/bash -vx
for g in `cat ftphosts.txt`; do echo $g
ftp -u $g<<++EOT++
user ftp ftp@anonymous.com
bin
lcd c:\investigate
hash
bell
mget *.*
user <ftp> <ftp@aol.com>
bin
mget  *.*
quit
++EOT++

for x in `cat file` is a useless use of cat and dangerous use of backticks and much better written as a while read loop.

#!/bin/bash -vx
while read g
do
echo $g
ftp -u $g<<++EOT++
user ftp ftp@anonymous.com
bin
lcd c:\investigate
hash
bell
mget *.*
user <ftp> <ftp@aol.com>
bin
mget  *.*
quit
++EOT++
done < ftphosts.txt

I'm not sure the FTP syntax is right, but that is harder to check -- I can't run it on your FTP after all...

Thank you for the security advice, but the cat worked.
It is the FTP here syntax I needed help with.

Does anyone see the problem with this here syntax?

Thank you.
Gregg Dotoli
:confused:

It's not a 'security problem', it's something that will blow up in your face someday.

What does the here-document do? The syntax is fine, but the contents may not be; in what way does it not work? Be specific. I can't see your computer from here, and don't have cygwin's own FTP client to test with.

Thank you for your help, but if you don't know what here is, you can't be
of help. I think it may not be such a challange for another.

Thank you.
Gregg

If you refuse to cooperate with the people trying to help you, this thread will be closed. We are not mind readers, nobody can work in a vacuum.

What does your script do? Be specific.

#!/bin/bash -vx
while read g
do
echo $g
ftp -i $g<<++EOT++
user ftp ftp@anonymous.com
lcd c:\investigate
hash
bell
bin
mget *.*
quit
++EOT++
done < ftphosts.txt

The file ftphosts is read into the $g variable. The problem is echoing ftp and anonymous at the name and password prompt. The here section between ++EOT++ and ++EOT++ should echo
username and password, then turn hash on, bell and binary before pulling down all the files into
c:\investigate. How do I get the user and password to be passed to the FTP servers?

Thank you in advance.
Gregg Dotoli

The here-document is working fine. FTP is printing the command itself for whatever reason. Perhaps it doesn't like that form of login; that can depend a lot on the FTP server.

You may be able to create a ~/.netrc file for automatic login, with contents like

machine hostname
        login username
        password whatever

My version of the ftp client seems to insist on reading the password from the terminal, not stdin, wondering if you have the same problem. .netrc works.

I already have the IP list, it's a huge amount of work and not very efficient to create a .netrc file. Is anyone out there good with the syntax of the HERE document? It's just a matter of syntax. The FTP servers all provide
the same login screnn and password prompt. It's pretty much a standard.

All I am trying to do is pass the same user ftp and password joe@test.com
to the script. It can't be this hard. I don't want to say anything but 5 messages ago you didn't know what a Here doc was. ANyone????

Thanks,
Gregg Dotoli

You are vastly mistaken. I've been adept at shell scripting for many years.

It is certainly not as easy as you insist. My FTP client doesn't even wait for the 'user' command, it shows a prompt instantly, even when run from script. Even when I close all other file descriptors, it just opens /dev/tty to talk to the terminal directly.

And it may bear no resemblance to yours, because you're also refusing to answer any of my questions. Mine does not have '-u' for instance. It's not even clear whether you're using Windows FTP, Cygwin FTP, some third-party executable, or what at this point, there's many variants. If you don't answer questions, how am I to know if any of my suggestions ever worked...?

Even a screenshot of what happens when you run your program would be more productive than this entire thread so far.

You've refused to even try anything I've suggested, too. Even just knowing that .netrc works for one IP would tell me a lot more about your setup than I know right now.

I'm not stopping anyone from posting in this thread... I think your attitude, and the complete and total lack of information from you, is doing that.

After much experimenting I think the -n option may be needed in order to use the 'user' option, to prevent the FTP server from doing autologin. And the password may require quotes.

This works here:

ftp -n <<EOF
open hostname
user username "password"
ls
bye
EOF

I solved the problem. I am using wget (gnu) in a while read loop. wget has the ability to authenticate without using another Unix command, it is built in. So everything works fine and is much cleaner.

Thanks,
Gregg

---------- Post updated at 03:10 PM ---------- Previous update was at 01:59 PM ----------

Iterating through the FTP IP Host file with the while read loop along with wget allows easy passing of username and password for checking technical evaluation of a list of ftp site.

while read g; do echo $g; wget -r ftp://ftp:fto@$g/ >>filelist;done<ftphosts.txt

There was no need for a here document.

1 Like

You can make this more efficient by running wget only once, since it can accept a list of files via -I, and read stdin via -I -

while read g
do
        echo ftp://ftp:fto@$g/
done < ftphosts.txt | wget -I - >filelist

Since you have Cygwin though, your version of wget may be slightly odd, and may or may not support -I.

What version of wget do you have? Mine is the latest and greatest compiled from
the source code. That code above won't work. Where is the variable for user and
formatted mail address? You also have no recursive switch. I see the pipe function
but this will not work. Mine is sufficient. If it ain't broke, don't fixit. What flavor of UX did you run this on? Please wrap your code in tags so we all can see syntax and function.

Gregg Dotoli

Which? There's lots of imitations now, since the original is tricky to port away from Linux. I have right here 3 or 4 different "wget for windows" programs and that doesn't even touch on cygwin. Even busybox has its own extremely minimal wget imitation now.

Some of them are reduced and butchered versions of GNU wget, with anything tricky to do in Windows disabled. Others are rewritten from scratch, and handle their options slightly or very differently.

Since Cygwin has a UNIX like environment, presumably you could build the original GNU wget with most of its features, though not necessarily all, Cygwin still has corners where it's just not the same. But I don't know if that's what you did -- you could have done what I did, and found wget.exe inside unxutils or busybox. Or even ported it yourself. If you're building it from scratch, hard to guess.

If it serves your needs, absolutely. It wasn't intended as a criticism.

I mostly stick to Linux. I use that construct quite frequently, with various details changed for the task at hand.