I have finally written this code, but the prob with this is that when it is prompting for password it doesnt send any password to the ftp machine, we have to do it manually, but after typing it manually, it take the password line as command and shows that that is invalid command.
please tell me a way to pass the password automatically
Shell Scripting Syntax (Toggle Plain Text)

CONFIG_FILE="${HOME}/c/config.cfg"
Password=`awk '$1 ~ /password/ { print $3 }' $CONFIG_FILE`
echo $Password
if [ ! -r $CONFIG_FILE ]
then
echo "File not readable";exit0;
fi

ftp -i `awk '$1 ~ /server_IP/ { print $3 }' $CONFIG_FILE` <<END
`awk '$1 ~ /user/ { print $3 }' $CONFIG_FILE`
`awk '$1 ~ /password/ { print $3 }' $CONFIG_FILE`
`awk '$1 ~ /file_mode/ { print $3 }' $CONFIG_FILE`
cd `awk '$1 ~ /file_path/ { print $3 }' $CONFIG_FILE`
get `awk '$1 ~ /file_name/ { print $3 }' $CONFIG_FILE`
quit
END


the config file is as
(Toggle Plain Text)

server_IP =
user = scpyogesh
password = scpyogesh
file_mode = binary
file_path = sample/test/
file_name = test_ftp.txt

Dani AI

Generated

A short, practical addendum tying the replies together and showing reliable alternatives.

As several replies hinted, the root cause is that many interactive FTP clients read the password from the controlling TTY rather than from standard input. That explains why a here‑document with the password line can appear to hang at the password prompt. pointed this out and correctly suggested using clients intended for scripting. The following options cover common, robust fixes and useful troubleshooting checks.

Options that work reliably

  • Use a non‑interactive client built for automation (examples shown inline): lftp -u user,password host -e "get path/to/file; bye" or curl -u user:password ftp://host/path -O. These send credentials over the protocol layer and avoid the TTY problem.
  • Use an FTP client that accepts user username password from stdin (some do) — invoking ftp -n then issuing a user command in a script can work on those builds, but it’s not portable.
  • Use an automation tool that drives the interactive session, for example expect. A short expect script can match the login prompts and send the password safely from a secured config file.

Example expect pattern (trimmed for clarity):

#!/usr/bin/expect -f
spawn ftp HOST
expect "Name"
send "USERNAME\r"
expect "Password"
send "PASSWORD\r"
expect "ftp>"
send "get remote/file\r"
send "quit\r"
expect eof

Troubleshooting and security notes

  • Verify the config extraction commands actually return the expected strings (run in a shell with set -x to see what gets passed). Watch for CRLFs from Windows files and simple shell typos that stop execution.
  • Avoid storing credentials world‑readable; prefer chmod 600 on config or use .netrc with strict perms or move to SFTP with key authentication.
  • For long‑term automation prefer lftp, ncftp/ncftpget, curl, or SFTP/SCP; use expect only when protocol alternatives aren’t available.

Recommended Answers

All 5 Replies

If you look at the man pages for ftp it states that you add the account parameter to the command line followed by the password for the account. To look at the manula pages use:

man ftp

from a shell prompt.

The ftp command is not designed for use in scripts; it is intended for interactive use. For scripting, use the ncftp family of commands. (There are also some others, I believe, that are made for scripting.)

Hello,

I hate to disagree with someone on a response but ftp works fine is shell scripts. Typical ftp client programs under Unix, Linux, Solaris and NetBSD all read the ftp password from /dev/tty (or /dev/pty). The following site has a discussion of the issues involved and a script that does what you want.

http://www.stratigery.com/scripting.ftp.html

I tried all that mentioned in the page. but it is still not taking the password. as in it just stucks to the point of username. it says the user name is ok but it keeps on blinking for the password. but is not able to pass the password .

ok.. its done.. thankz

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.