Link to home
Start Free TrialLog in
Avatar of kalyan_arjun
kalyan_arjun

asked on

automate sftp without key authentication and expect

Here is the requirement and want to know whether i would be able to do it or not:

I want to automate the sftp to get a file from a remote server without key authentication and expect. can anybody provide any script for this to achieve?
Avatar of Shreedhar Ette
Shreedhar Ette
Flag of India image

Hi,

Refer this:
http://cpanforum.com/threads/3329

It might help,
Shree
Avatar of kalyan_arjun
kalyan_arjun

ASKER

The link you had sent us linked to another one which i am not able to access. And moreover, it seems like he is using expect with the perl. Like I said, i am looking for a script without key authentication and expect.
Anyway, Thanks for the quick response.
This is what I have used before http://troy.jdmz.net/rsync/index.html

Works GREAT!
Hi lewis, The link you had sent me is really a nice but it mentioned that it needs to have the keys on the remote host(check Configuring remotehost section). This will not work for me.

Let me be more specific on my requirement.
I dont have any permissions/authority to create a file on the remote host and remote directory. A file would be placed daily and im looking for a script to get that file from the remote host.
I am able to do sftp@remotehost and get that file after entering the password. I want to acheive this in the script without paswordkey and expect.

Lewis, I appreciate for the link that you had sent and is really a nice article regarding the secuirty tip where you can mention the ipadreeses that would allow the sftp. Thanks!
If your system has autoexpect here is a slick scripting solution:

http://dbaspot.com/forums/shell/196804-novice-needs-help-using-expect-automate-sftp.html

No way.
There is no possibility in sftp to provide a password on the commandline or to have it read from a file or stdin or the like.
You will have to go with key authentication or with something like expect.
ssh-agent will not work either, as it relies on keys and not system passwords.
wmp
 
Do you have php installed?  As the SFTP sub-system permits password authentication, see: http://www.phpbuilder.com/manual/function.ssh2-sftp.php
Along similiar lines, if Java is your language of choice, the mindterm java library and SFTPCopyFile.java example provide the functionality you require:

Usage: SFTPCopyFile <server:port> <username> <password> to|from <src_file> <dst_file>

See: http://www.appgate.com/index/products/mindterm/
i was able to do sftp with the expect as there were no options until I use another language. Thanks everybody for your suggestions.

And one more thing is, i need to get the latest file from the other server. Eveyday one file is copied to the remote server and i need to get the latest file from the remote server:

sftp> latest_file=`ls -ltr | tail -1 | awk '{print $9}'
Invalid command.
It seems like only the sftp server is installed on this server

but ls -ltr works and it will give a list of files. Not sure what the other vendor has it. Is there anyway i could do it?

#!/usr/local/bin/expect

#DATE1=`date "+%m%d%y%H%M%S"'
#LOGFILE= /ul/batch/logs/log_$DATE1.log
spawn sftp abc@hostname.com
expect "password:"
send "pass\n";

expect sftp>
send "get abc.txt\r"
expect sftp>
send "exit\r"

expect eof
Any help with this or do i need to open another question for this?
It looks like the question was answered with information that appears on the dbaspot.com.

As for getting the newest file that sounds like a new question.

SFTP has a fairly limited command set and since you can't run anything on the remote machine you might try capturing a list of file names to your machine and then figuring out what the latest file is. What do the file names look like?

#!/usr/local/bin/expect

#DATE1=`date "+%m%d%y%H%M%S"'
#LOGFILE= /ul/batch/logs/log_$DATE1.log
spawn sftp abc@hostname.com
expect "password:"
send "pass\n";

expect sftp>
send "ls /ul/test\n"
expect "No Such File" {send "exit\n"}\
expect sftp>
send "get abc.txt\r"
expect sftp>
send "exit\r"
expect eof

I tried using exit and quit and neither of them are working. Even though the file is not there, it is executing the next steps.

I am running with an agent where it looks for an exit code 0 for successful and 1 for failure. I am looking for a script for this..
Hi, you now appear to be using "expect" but  your question: "automate sftp without key authentication and expect"
states you require a solutions that does not use "expect", have you changed your mind?
sorry I should have changed it. Now,i am using expect for this sftp.Like, i mentioned, i want to quit or exit if the file is not there.
Replace your:

expect "No Such File" {send "exit\n"}\


with:

expect "No such" { send "exit\r"
                           exit 2}
still not working..
Sorry forgot it's FORMAT sensitive, you need a blank line after the "{", try:

expect "No such" {
                           send "exit\r";
                           exit 2
                         }

If still not working add a "-d" debug flag to first line e.g.

#!/usr/local/bin/expect -d

Should show you were you going wrong, ELSE try the following, which works:


#!/bin/sh
# Name:    sftp_get_using_pwd.sh
# Purpose: SFTP test and pull a remote file, using password authentication.
#
EXPECT_DIR="/usr/local/bin/"
LOG_DIR="/tmp"
LOG_FILE="no_key-sftp"
DEST_DIR="."
SFTP_USER="abcdef"
SFTP_PASS="xxxxxx"
SFTP_HOST="some.host"
SFTPEXIT=0
tstFile="/ul/test"

# ----- Procedures -----
sftp_batch() {

$EXPECT_DIR/expect 2>&1 > "${LOG_DIR}/${LOG_FILE}" <<EOF
set timeout 100
spawn  /usr/bin/sftp $SFTP_USER@$SFTP_HOST
log_file ${LOG_DIR}/${LOG_FILE}-sftp
expect "Connecting to $SFTP_HOST..." {
  expect {
           -re ".*assword:" {
                              send -- "$SFTP_PASS\r";
                              expect {
                                       "Password:" {
                                                       send_user "ERROR SFTP password invalid\n";
                                                       exit 1
                                                      }
                                       "sftp> " {
                                                       send "ls $tstFile\r";
                                                       expect {
                                                                "No such file or directory" {
                                                                              send_user "ERROR No such File: $tstFile\n";
                                                                              send "quit\r";
                                                                              exit 8
                                                                             }
                                                                "sftp> " {
                                                                        send "get $tstFile $DEST_DIR\r";
                                                                        expect "sftp>" {
                                                                                        send "quit\r";
                                                                                        exit
                                                                                      }
                                                                        send_user "ERROR SFTP get failed OR timed out\n";
                                                                        send "quit\r";
                                                                        exit 7
                                                                        }
                                                            }
                                                        send_user "ERROR SFTP listing failed\n";
                                                        send "quit\r";
                                                        exit 6
                                                   }
                                     }
                           }
       }
       send_user "ERROR SFTP connection failed\n"
       exit 3
}
send_user "ERROR SFTP initialization failed\n"
exit 4
EOF
SFTPEXIT=$?
Woops, cut'n'paste failure, the whole script is below:


#!/bin/sh
# Name:    sftp_get_using_pwd.sh
# Purpose: SFTP test and pull a remote file, using password authentication.
#
EXPECT_DIR="/usr/local/bin/"
LOG_DIR="/tmp"
LOG_FILE="no_key-sftp"
DEST_DIR="."
SFTP_USER="abcdef"
SFTP_PASS="xxxxxx"
SFTP_HOST="some.host"
SFTPEXIT=0
tstFile="/ul/test"

# ----- Procedures -----
sftp_batch() {

$EXPECT_DIR/expect 2>&1 > "${LOG_DIR}/${LOG_FILE}" <<EOF
set timeout 100
spawn  /usr/bin/sftp $SFTP_USER@$SFTP_HOST
log_file ${LOG_DIR}/${LOG_FILE}
expect "Connecting to $SFTP_HOST..." {
  expect {
           -re ".*assword:" {
                              send -- "$SFTP_PASS\r";
                              expect {
                                       "Password:" {
                                                       send_user "ERROR SFTP password invalid\n";
                                                       exit 1
                                                      }
                                       "sftp> " {
                                                       send "ls $tstFile\r";
                                                       expect {
                                                                "No such file or directory" {
                                                                              send_user "ERROR No such File: $tstFile\n";
                                                                              send "quit\r";
                                                                              exit 8
                                                                             }
                                                                "sftp> " {
                                                                        send "get $tstFile $DEST_DIR\r";
                                                                        expect "sftp>" {
                                                                                        send "quit\r";
                                                                                        exit
                                                                                      }
                                                                        send_user "ERROR SFTP get failed OR timed out\n";
                                                                        send "quit\r";
                                                                        exit 7
                                                                        }
                                                            }
                                                        send_user "ERROR SFTP listing failed\n";
                                                        send "quit\r";
                                                        exit 6
                                                   }
                                     }
                           }
       }
       send_user "ERROR SFTP connection failed\n"
       exit 3
}
send_user "ERROR SFTP initialization failed\n"
exit 4
EOF
SFTPEXIT=$?
}

# ----- Main -----
echo "ls" > $CMD_FILE
sftp_batch
echo "----------------"
echo "Returned $SFTPEXIT"
echo "--"
cat "${LOG_DIR}/${LOG_FILE}"
This script works like a charm. I am trying to remove the files after it is downloaded where i am unable to do it. Could you throw some light on this?
ASKER CERTIFIED SOLUTION
Avatar of arober11
arober11
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
perfect solution and it worked like a charm...