Shell : sftp autologin not working ...

Hi folks,

for sftp autologin, while browing in unix.com forums.. I got the below code. I tried to execute that code but no luck.

#!/bin/sh
HOST=yourservername
USER=yourusername
PASS=yourpassword
echo "sftping file ..."
lftp -u ${USER},${PASS} sftp://${HOST} <<EOF
cd /tmp
get tmpfile
bye
EOF
echo "done"

the error message is as follows

ubuntu1[johng]/mypath>sh sftp_new.sh
sftping file ...
sftp_new.sh: line 8: lftp: command not found

Could anyone please help me what went wrong....

You don't have lftp installed.

1 Like

Now I have installed the lftp and it is running fine.

If I sftp only one single file it is working fine...

But I am trying to ftp only the latest file from the remote machine to local machine with the below code.

#!/bin/sh  
HOST=myhost
USER=myuser
PASS=mypass

echo "sftping file ..."  

lftp -u ${USER},${PASS} sftp://${HOST} <<EOF 
cd /mypath/
SOURCE_FILE=$(ls -ltr file*.txt |tail -l)
get "$SOURCE_FILE"
bye 
EOF  
echo "done"

it is showing the below error message

sftp_.sh: line 13: warning: here-document at line 7 delimited by end-of-file (wanted `EOF')
ls: cannot access file*.txt: No such file or directory
Unknown command `SOURCE_FILE='.
get: Access failed: Failure

I made sure that files exist in the directory but still it is the showing "No such file or dierctory" error..

Could anyone please help me on this ?

---------- Post updated at 05:34 AM ---------- Previous update was at 04:45 AM ----------

The below code is sending all the files but I need to send only the latest file only.

Could anyone please help me?

#!/bin/sh
HOST=myhost
USER=myuser
PASS=mypass
echo "sftping file ..."

lftp -u ${USER},${PASS} sftp://${HOST} <<EOF
cd /mypath/
mget  carlog*.txt
bye
EOF
echo "done"

Actually in the directory.. there are 3 files with names
carlog_2013_03_19.txt
carlog_2013_03_21.txt
carlog_2013_03_24.txt

So from the above.. I need to get only the latest one.. i.e., carlog_2013_03_24.txt

Regards,
J

The ls command is run on the local machine, not the remote machine.

You could find the name of the most recent file in a first call to lftp . You can write the output into a temporary file, grep the filename out and then call lftp a second time to actually transfer the file.

1 Like

@above.. thanks Sir..

But it doubles the work... if not wrong...I had the below ftp code and expected the same can be done to the sftp also.. In case you are right.. but Is there any other way(jst like below ftp code)

cd /mypath/
SOURCE_FILE=$(ls -rt myfile*.DAT |tail -l)
ftp -i -n -v <<-!
open myipaddress
user "IUSER\netrep1" 1132115c
lcd /localpath/
cd /remotePath/
put "$SOURCE_FILE"
close
quit
!
echo 'The file has been successfully FTPed to the server'

Thanks in advance....

The here document is completely prepared, before "lftp" is started. So it cannot react on lftp's output.

Another hint would be to try "expect". It is a tool to interact with programs and react on their outputs, often used to control telnet/ssh/ftp/...-sessions.

1 Like

thanks Sir...

Just now I had installed expect rpm package in my machine..

Could you please help with sample sftp code with expect which pulls only the lastest file from the remote server.

This script works here:

#!/usr/bin/expect

set user [lindex $argv 0]
set pass [lindex $argv 1]
set host [lindex $argv 2]

proc getLastFile {} {
  send    "ls -rt1\r"
  expect  "sftp>"
  return  [lindex [split $expect_out(buffer) \n] end-1]
}

spawn   sftp $user@$host
expect  "password:"
send    "$pass\r"
expect  "sftp>"
send    "cd mypath\r"
expect  "sftp>"
set     file [getLastFile]
send    "get $file\r"
expect  "sftp>"
send    "quit\r"

Save it as "getit" and then you can call it with three arguments: username, passwort, hostname, for example

./getit test mypassword localhost

You might have to adjust the script to your needs.