Problem automating sFTP transfer using script in cron


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Problem automating sFTP transfer using script in cron
# 1  
Old 06-18-2010
Question Problem automating sFTP transfer using script in cron

Hi Newbie here
I am having problems with automating sFTP transfers.

Just to save time - SCP is not an option as sFTP is stipulated by controllers of far end server.

Ineed to automate sFTP transfer of a single file, once a day to a remote server to which i have no control over.
I am using: Solaris server (Solaris 9 9/05 s9s_u8wos_05 SPARC), the UNIX provided OpenSSH, SHELL=/bin/sh

So far i have mangaged to set up the required DSA keys and connected to the server manually using sFTP without having to enter a password(so i know the keys are working ok) and manually put a test file on there.

My next task and the bit i am stuck on is to write a script to automate the daily file upload - same filename every day and simple overwrite.

Problem is that all posts i have currently seen suggest to use -b option and write sFTP in separate file, sFTP reports that -b is an illegal optionSmilie so need to write a script that will initiate the connection, pass the sFTP commands required and then close the connection so that i can put it in the crontab for running once a day.

SmilieAny help would be appreciatedSmilie
Thanks
Smilie
# 2  
Old 06-18-2010
Code:
#!/bin/ksh
cd /path/to/local/file
/usr/bin/sftp 11.22.1.2 <<EOF
put localfilename remotefilename
exit

This is a here document. Is this what you are asking? Or is it something to do with cron? 11.22.1.2 is the remote host name or ip address. cron has to run under the crontab for the user with the keys.
# 3  
Old 06-18-2010
Hi
Thanks for the reply.
It's not a cron problem - i haven't got as far as putting in the cron yet.

I can manually connect:
sftp username@host

this automatically connects me as i have already set up the keys.

I can then transfer file: put file
Then disconnect: exit

That is all that is required except for the fact i want to do is automatically via a script so it can go in the cron - just dont know how to get the sftp commands to be read and get the connection to exit without doing it manually
Cheers
# 4  
Old 06-18-2010
I don't believe you can run sftp in a script. I had to use expect to "automate" it. There are several postings on expect and its use. Good luck.

Here is some code that I put together from posts that I found here. Hope it helps.
Code:
#!../expect -f
# To make sftp non-interactive
# login is the first arg. password is the second arg
set force_conservative 1
if {$force_conservative} {
    set send_slow {1 .1}
    proc send {ignore arg} {
         sleep .1
         exp_send -s -- $arg
    }
}
set timeout -1
set password [lindex $argv 1]
set login [lindex $argv 0]
spawn sftp [lindex $argv 0]
match_max 1000000
expect "$login's password:"
send -- "$password\r"
expect "sftp>"
send -- "put -P /lsf1/hurdata/HMC*\r"
expect "sftp>"
send -- "ls -l\r"
expect "sftp>"
send -- "quit\r"
expect eof


Last edited by jyoung; 06-18-2010 at 03:25 PM..
# 5  
Old 06-18-2010
Quote:
Originally Posted by jyoung
I don't believe you can run sftp in a script.
It's been done. It's designed to. The trick to scripting a login is to use a non-interactive login method, like keys.
Quote:
I had to use expect to "automate" it.
The script you've quoted has a security hole big enough to drop a cow through. Passing a password through commandline parameters lets anyone logged into the system see it, in plaintext!

This is why things like scp, sftp, ssh, sudo, and su won't accept passwords through anything but a terminal. It's a subtle hint, written in mile-high flashing neon letters: This is a bad idea. You're not supposed to do this. By using expect to brute-force it into running the wrong way, you've learned the wrong lesson.

Doubly so, since the OP has already said they already have working passwordless logins, arranged the proper way: keys. Logging in is not the problem here. Please think next time before you suggest brute-forcing passwords insecurely.

---------- Post updated at 01:34 PM ---------- Previous update was at 01:28 PM ----------

Quote:
Originally Posted by robbien
That is all that is required except for the fact i want to do is automatically via a script so it can go in the cron - just dont know how to get the sftp commands to be read and get the connection to exit without doing it manually
Cheers
This works fine for me when passwordless login's arranged:
Code:
cat <<EOF | sftp user@host
ls
EOF

It logs in, does ls, and quits. You can have as many lines of commands as you want, just add them:

Code:
cat <<EOF | sftp user@host
cd public_html
put file_a
put file_b
put file_c
EOF

# 6  
Old 06-18-2010
let try like this Smilie

Code:
# crontab -e
00 14 * * * /root/.myscripts/sftpdoit.sh transferfile.xml

Code:
# cat /root/.myscripts/sftpdoit.sh
#!/bin/bash
myfile="$1"
/usr/bin/sftp youruser@yourhost <<EOF
put $myfile
quit
EOF

# 7  
Old 06-21-2010
Thanks very much for the help i have now successfully tested a script:

Code:
sftp user@host.com <<EOF
lcd LOCALFOLDER
put FILE.TXT
quit
EOF

This done the job for
Thanks again
SmilieSmilieSmilieSmilieSmilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Execute a script after SFTP transfer

Hi All, I am having an issue with the script execution. I am having a SFTP process that send files to a location . I want to execute a script once it transfer the file to that location. Right now I am leveraging autosys file watcher to do that. Is there any way like MFT PPA that can execute... (2 Replies)
Discussion started by: arunkumar_mca
2 Replies

2. Shell Programming and Scripting

Shell script, sftp logfile not showing transfer information

Hello, Recently I have changed some crontab scripts which I execute to do different tasks mainly transfering some files from one server to the other. The main change was the protocol from ftp transfer to sftp. Ftp server was the MS Windows default service, and Sftp server is an proprietary sw... (4 Replies)
Discussion started by: enux
4 Replies

3. UNIX for Dummies Questions & Answers

Execution problem with Cron: Script works manually but not w/Cron. Why?

Hello gurus, I am making what I think is a simple db2 call from within a shell script but I am having difficulty producing the desired report when I run the script shown below from a shell script in cron. For example, my script and the crontab file setup is shown below: #!/bin/ksh db2... (3 Replies)
Discussion started by: okonita
3 Replies

4. Shell Programming and Scripting

Sftp : not able to print the echo statements after the sftp transfer

I had the below sftp script working perfectly but the problem is I am not able to send the echo statements . #!/bin/sh echo "Starting to sftp..." sftp admin@myip << END_SCRIPT cd /remotepath/ lcd /localpath/ mget myfiles*.csv bye END_SCRIPT echo "Sftp successfully." echo echo... (11 Replies)
Discussion started by: scriptscript
11 Replies

5. Shell Programming and Scripting

Shell script to transfer file via SFTP

Hi all, I'm trying to do a script to transfer file between my server and an external server via SFTP protocol. It doesn't use rsa key, but password. When I run the script, it throw back a prompt that request me to put the password. How should I do for automatic login? Pleaes help :( ... (2 Replies)
Discussion started by: Kapom
2 Replies

6. Shell Programming and Scripting

Automating file transfer between two SSH enabled server.

Hi Experts, Few more words to the title, both the servers are ssh enabled but I have read only access to the second server, so I cannot automate SFTP process using RSA/DSA keys. I am using Control M to trigger the script and do not want any manual intervention to enter the password to complete... (4 Replies)
Discussion started by: nchourasiya
4 Replies

7. Solaris

Issues with automating SFTP

Hi We are trying to set up a non-interactive sftp to one of our clients to be able to transfer files to them. For the setup I logged into server1 as user1 and generated RSA public and private keys id_rsa and id_rsa.pub. Then I did an sftp to server2 as user2 and put the id_rsa.pub in the .ssh... (4 Replies)
Discussion started by: vnparo
4 Replies

8. UNIX for Dummies Questions & Answers

cron ? automating a script

Hi all. basically i need to run a script every 30 minutes. my script is simply an error report: errpt thats it, is there anyway to make this happen every 30 minutes without having to type errpt in, the script will get bigger as i add more things to do but just need to know how to... (8 Replies)
Discussion started by: hassanj
8 Replies

9. Shell Programming and Scripting

How to transfer files (By using generic script) when using sftp

I have written generic script to transfer files from one machine to other machine(By passing the command line arguments like server name, user name ,location of the files, file names etc) but when i am using sftp, what are the things I have to specify in the code Is it necessary to specify... (0 Replies)
Discussion started by: gsri
0 Replies

10. Shell Programming and Scripting

automating sftp script

I have to write an automated sftp script which uses password authentication method to access the remote server. I want to pass the password as a parameter or to be included in the script itself, so that when i run the sftp script, it should not prompt me to enter the password. Thanks in advance... (1 Reply)
Discussion started by: Rajeshsu
1 Replies
Login or Register to Ask a Question