Connect to server-1 from server-2 and get a file from server-1

I need to connect to a ftp server-1 from linux server-2 and copy/get a file from server-1 which follows a name pattern of FILENAME* (located on the root directory) and copy on a directory on server-2. Later, I have to use this file for ETL loading... For this I tried using as below

/usr/bin/ftp -n ftpsitename.com <<EOF
quote USER 12345
quote PASS 12345
cp /FILENAME* /directory_location_to/copy/on_server2/
EOF

I get the error as "?Invalid command".

Please advise if this is the right way to get the file or if there are alternative solution for grabbing the file.

Thanks
Dhruuv

if ftp knows about mkdir or cd, it certainly doesnt know cp... since it uses put, get, mput, mget
alternative would be with ssh: scp
do a man of scp to see if you have it installed and look how it is used...

Hi vbe,

I tried to use SCP as shown below

scp -P 21 username1@hostname1:/filepattern* username2@hostname2_where_copy_the_file:/directory/to/copy/the/file

I got the error

ssh: connect to host hostname1 port 22: Connection refused

Any idea on what's happening here? Even though I mentioned the port no. as 21, it's throwing me an error related to port no. 22...

However, I am able to connect to the hostname1 on 21 port using WINSCP tool and get the file onto my local windows machine... Please advise...

You should not specify a port number or certainly not 21 which is for FTP and generaly closed on secured boxes...

I have even tried to give the command without any port number, still I get the same error...

Lets start from the beginning: This server 1 is an FTP server? OK then

/usr/bin/ftp -n ftpsitename.com <<EOF
quote USER 12345
quote PASS 12345
mget /FILENAME* /directory_location_to/copy/on_server2/
EOF

Should work (we get all files FILENAME* from root of source to destination on server 2 ...)
For invalid command was cp... did not say it would not work with known ftp commands...

I put your code in a file by name, "ftp_test.sh".

cat ftp_test.sh

gives me

/usr/bin/ftp -n ftpsitename.com <<EOF
quote USER 12345
quote PASS 12345
mget /FILENAME* /directory_location_to/copy/on_server2/
EOF

and I use

to run the script on server2, I am now getting the following error:

Currently, FILENAME26.txt is the only file available on the server1.

So I tried to give the exact file name which is present on server1 and changed "mget" to "get" to check if this copies the file on server2 location (/directory_location_to/copy/on_server2/) this time, now the script looks like

cat ftp_test.sh
/usr/bin/ftp -n ftpsitename.com <<EOF
quote USER 12345
quote PASS 12345
get /FILENAME26.txt /directory_location_to/copy/on_server2/
EOF

i get a different error:

However, I checked on server 2 if the directory

exists or not, and it does exist... I even tried to get the file on server2 home directory, but I get the same error as above... How does the command know to put the file on the server2(since we are not mentioning about it in the code)??? any further suggestion on this???

If you connect to server1 from server2 it understand with get: get (from server1) ... mget meaning "multiple" get...

long time since last time I wrote a script for ftp...
You have 2 other things you could do, and read also the man pages of your ftp command..., using cd and lcd
lcd for local cd
cd for remote cd
so your script:

/usr/bin/ftp -n ftpsitename.com <<EOF
quote USER 12345
quote PASS 12345
lcd /directory_location_to/copy/on_server2/
cd /
mget FILENAME* 
EOF

But not sure I understood what you wanted in fact: Is it
bring back all files beginning by FILENAME
to
/directory_location_to/copy/on_server2/
?
(And not as I understood first : and content of /directory_location_to/copy/on_server2/...)