FTP connection refused

Hi I am trying to execute a shell script which is in unix server gs1. The script is below which basically connects to another unix server q15 and tries to get a file using FTP .
But i get error as "ftp: connect: Connection refused
Not connected.
Not connected."

Please help with if the below script has any flaw

#!/bin/ksh
HOST=q15.dev.com
USER=sam
PASSWD=sam
FILE1=/common/PDS/xyz.psv
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
lcd /app/daily/scripts
get $FILE
quit
END_SCRIPT
exit 0

regards
Sam

Are you able to manually ftp to the other server?

If tha answer to fpmurphy's question is an affirmative, disregard what follows.

Your error message suggests that the FTP server cannot connect to the client machine, which it needs to do for normal (active) FTP. This is usually the result of the client being firewalled. You may have better luck using passive mode.

Regards,
Alister

I just checked that normal FTP is not allowed and it should be sftp. I never tried. Can anyone help me to modify the above script i posted with how to make it work for sftp ?

sftp is not a drop-in replacement for ftp, it does some things very differently, especially passwords. You will need to arrange passwordless login if you want to script it. Just having the right files in the right places lets automatic login happen.

Then you can just ignore the password part and do

sftp username@host <<EOF
lcd /app/daily/scripts
get $FILE
quit
EOF

I thought that in-line commands were not allowed for sftp and that one always has to put the commands in a file and refer to them with the -b file. Of course, this might vary depending on the OS. I would expect something more like:-

echo "lcd /app/daily/scripts
get $FILE
quit"
> sftp.cmds

sftp -b user@server

Have I missed something?

Robin
Liverpool/Blackburn
UK

It may indeed vary on the OS, since I have not encountered that restriction myself.

1 Like

Actually I tried the sftp connection to connect to serverq15 and used get to get a file to the local unix server gs1.

sftp usersam@serverq15 <<END_SCRIPT
get usersam@serverq15:/common/PDS/xyz_OUT.psv /app/daily/scripts
quit
END_SCRIPT
exit 0

But got the below error message although the file exists in the target server:

sftp> Couldn't stat remote file: No such file or directory
File "usersam@serverq15:/common/PDS/xyz_OUT.psv" not found.

Assuming that you get connected okay, you do not need to specify the connection again in your get command. Try:-

sftp usersam@serverq15 <<END_SCRIPT
get /common/PDS/xyz_OUT.psv /app/daily/scripts
quit
END_SCRIPT

... and see if that improves things.

Robin