1832080 Members
3020 Online
110036 Solutions
New Discussion

Re: scripting sftp

 
SOLVED
Go to solution
Gary L. Paveza, Jr.
Trusted Contributor

scripting sftp

I have a requirement to write a script which will prompt our operators for information in order to ftp (or sftp) a file to a remote site. Once all the information is gathered, then the file is transfered.

With ftp this is easy as I can create a temporary file with all the login / instructions in it, then pass it to ftp using the -n option and << constructs.

However, with sftp I can't seem to get it to work with the login. I know that I can setup ssh keys, however the idea is to "automate" this. Our operator's are supposed to be protected with how it works. Since the sftp connection is encrypted, sending the passwords isn't an issue, and quite frankly, the temporary file isn't an issue for the short term.

Any ideas how I can get sftp to accept the password from an input file?
5 REPLIES 5
TwoProc
Honored Contributor

Re: scripting sftp

I too Gary have looked, and although it seems that a simple redirect would do it - it won't work. After researching here, I decided to use keys. However, I just read the other day someone using a program called "expect" - which I've not had a chance to try yet.
We are the people our parents warned us about --Jimmy Buffett
Peter Godron
Honored Contributor
Solution

Re: scripting sftp

Gary,
with expect installed the script below should give you an idea:
#!/usr/bin/expect -f
#
spawn ftp
expect "Name"
send "anonymous\r"
expect "Password:"
send "email@domain.com\r"
expect "ftp>"
send "binary\r"
expect "ftp>"
send "mput *.ema\r"
expect "mput"
send "y\r"
expect "ftp>"
send "exit\r"
exit
Regards
Peter Godron
Honored Contributor

Re: scripting sftp

Gary,
if you need a copy of expect:
http://gatekeep.cs.utah.edu/hppd/hpux/Tcl/expect-5.43/
Regards
Rory R Hammond
Trusted Contributor

Re: scripting sftp


Expect is a good choice.

You can use kermit over tcp as an expect alternative see example of actual sucessful script.
some names have been changed.
Rory
#!/bin/sh
# sample kermit transfer for sftp
# This sample when executed on ecom1 will use kermit to login to localhost(ecom1) as "transfer"
# using the transfer passwd of TRAN_23_44
# and then execute sftp using user "between" to host "rattler-172.18.8.9"
# use the passwd be2345. and transfer outfile (on ecom1) to infile (on rattler)
#
# NOTE Big FTP files will need to have WAIT_THIS_LONG set to a larger number
# this example uses 15
#

dojob()
{
# This macro will sftp a secure file
# sftp expect to talk to a logged in user
# so I used kermit to achieve a terminal interface.
# kermit logins and runs the sftp

RUSER=$(echo ${1})
RHOST=$(echo ${2})
PASSWORD=$(echo ${3})
FROMFILE="${4}"
TOFILE="${5}"
WAIT_THIS_LONG="${6}" #added to allow for compression of large files
WAIT_THIS_LONG=${WAIT_THIS_LONG:-10} #added for compression of large files

echo "
set network TCP/IP
set host localhost
input 10 login:
if failure exit 1 No login prompt
output transfer\13
input 10 word:
if failure exit 1 No password prompt
output TRAN_23_44\13
input 10 $
if failure exit 1 No prompt
output sftp ${RUSER}\x5c\x40${RHOST}\13
input ${WAIT_THIS_LONG} word:
if failure exit 1 No remote prompt
output ${PASSWORD}\13
input 10 sftp>
output put ${TOFILE} ${FROMFILE}\13
input ${WAIT_THIS_LONG} sftp>
output quit\13
output exit\13
"
}


trap "" 1 2 3
dojob between 172.18.8.9 be2345 /usr/tmpi/outfile /usr/tmpi/infile 15 |/usr/bin/kermit
There are a 100 ways to do things and 97 of them are right
Gary L. Paveza, Jr.
Trusted Contributor

Re: scripting sftp

Thanks. It appears that I can't do what I want (without additional sotware). Operators are just going to have to enter the password. Not a big deal. Thanks for all the help.