Doubt in .netrc file for ftp login

Hi,

i have a doubt. i am using .netrc file for login to a ftp server.

ftp abc.ftp.com

suppose i have 2 userid and password for the same server as shown below. which one is it going to read from the .netrc file?

cat .netrc
machine abc.ftp.com login admin1 password pass1
machine abc.ftp.com login admin2 password pass2

Try:

ftp admin2@abc.ftp.com

Hope this helps

it gives unknown host error.

That's sftp I'm afraid. The only way I have got this to work is to have one of the following:-

  • Lots of DNS aliases so that the .netrc can have several definitions, even though they eventually go to the same host.
  • Lots of subdirectories that each contain a separate .netrc file. You can then defined $HOME to be the appropriate one before calling ftp.

Neither is perfect, but if you cannot sftp then these might give you a viable option. In the first, there is the overhead of all the alias records and what happens when you migrate services to another server. You might need to have:-

  • A hostname definition
  • An alias for all services on the host
  • A set of alias records pointing to the above for each FTP

That way, if you replace the host, you move the first alias to point to the new hostname and all the other alias records will follow that.

Of course, with the other option you then have the concerns over multiple .netrc files and changing the value of $HOME which can have unexpected consequences depending on what you next do.

I hope that this helps, but let us know if you need more detail on the options.

Robin
Liverpool/Blackburn
UK

ftp reads the first line that the found in the your .netrc file.

Uh :confused:
I used to use .netrc for like 3 ftp servers at the 'same time'.
Meaning i acutaly had 4 entries for 3 servers, 1 been quoted out.
Maybe it was just an illusion that the files got uploaded and were to be seen on the website.

The only error i experienced with .netrc were when i deleted the password 'collum' of a line.

Syntax from manpage: sftp

sftp [user@]host[:dir[/]]

Just curious, why make a file (~/.netrc) that contains all required info on 1 line, if it can handle only 1 entry?
I thought it was ment like: you pass the host to ftp , which will open ~/.netrc and read the line containing host , obviously, it 'should' check for any user parameter to be on the same line too, if there are multiple identical host s.

Well, on Fedora it seems to work that way.

.netrc file is meant for ftp logins not for sftp logins i guess.

On my machine it used to work with:

  • ftp
  • lftp
  • sftp

And it is supposed to work with wget and curl as well.

When the ftp command uses .netrc to look up credentials for a remote server, it looks for the file .netrc in the home directory of the user under which the ftp command is running. Therefore, every user (including root) will each have their own .netrc file. Ftp looks up the 'machine' name and uses the credentials it finds to make the connection to the remote ftp server.

Unless you have one user with multiple accounts on the remote server there is no conflict. If you do have that then I must ask why?

Does that help or have I misunderstood your question?

# man netrc
DESCRIPTION
     This file contains configuration and autologin information for the File Transfer Protocol client ftp(1).
........................

So .netrc only used by the ftp protocol not sftp.
sftp works over the ssh protocol.

lftp uses the ftp and because of it uses the .netrc
from lftp trace

open("/root/.netrc", O_RDONLY)

curl and wget have ftp capabilities.

wget "options/parameters" ftp://10.100.110.83 .....other options

curl needs the -n parameter for .netrc

curl -n "options/parameters" ftp://10.100.110.83 ....other options

regards
ygemici

is it possible to capture the 3 digit return code of ftp commands in a local variable inside a shell script?

ftp remoteserver << ftp 
    quote USER uid
    quote PASS pass
    prompt
    cd remote_directory
    mput file.txt
    bye
ftp

in the above script, if cd command returns 550 in case of failure and 226 in case of success. same way mput also returns.. how cn i capture these return codes.

I would suggest using a different delimeter to mark the in-line text of the "here" document. Using ftp could be rather confusing. Perhaps something like:-

ftp remoteserver << EOFTP
    quote USER uid
    quote PASS pass
    prompt
    cd remote_directory
    mput file.txt
    bye
EOFTP

..... would be clearer.

Remember that EOFTP would have to be the first non-tab character of the line to end the in-line text.

Robin

ok can u answer the below thread.. any idea..

here

# a=$(./unix.com-ftp.sh 10.100.110.83 cd 4) && echo $a
250
# a=$(./unix.com-ftp.sh 10.100.110.83 get 2) && echo $a
226
# a=$(./unix.com-ftp.sh 10.100.110.83 cd 34234qwq) && echo $a
550
#!/bin/bash
# /********************************************************\
# *     unix.com ftp return-codes                          *
# *     @ygemici                                           *
# *     uses $HOME/.netrc for auto-login                   *
# \********************************************************/
arrayX=($@ ); ftpcmd="${arrayX[@]:1}";ok=0
echo "$ftpcmd"|/usr/bin/ftp -v "${arrayX[0]}"> /tmp/tmpuxXXXX.ftp
for i in get put ; do
echo "$ftpcmd"|grep $i|grep -v m >/dev/null
if [ $? -eq 0 ] ; then
for j in "File receive" "File send" ; do
grep "$j" /tmp/tmpuxXXXX.ftp >/dev/null
if [ $? -eq 0 ] ; then
sed 'N;N;x;$!D' /tmp/tmpuxXXXX.ftp|sed -n '$s/ .*$//p';exit;fi
done
sed 'N;x;$!D' /tmp/tmpuxXXXX.ftp|sed -n '$s/ .*$//p';exit ;fi
done
for i in mget mput mdelete mdir mls; do
echo "$ftpcmd"|grep $i >/dev/null; if [ $? -eq 0 ] ; then ok=1;fi
done
if [ $ok -eq 1 ] ; then
echo;echo "detected interactive command! ftp return code(s) maybe cant be determined!!";
sed 'N;N;x;$!D' /tmp/tmpuxXXXX.ftp|sed -n '$p'
else sed '$!N;$!D' /tmp/tmpuxXXXX.ftp|sed -n '1s/ .*$//p' ; fi

regards
ygemici