Online Expert Day - HPE Data Storage - Live Now
April 24/25 - Online Expert Day - HPE Data Storage - Live Now
Read more
System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

Script - SFTP for file transfer

Narendra Uttekar
Regular Advisor

Script - SFTP for file transfer

Hi,
I want to write a script for file transfer using sftp. I want to automate this script so that at schedule time the script will run and transfer the file. I am already having a script for ftp to transfer the file.
HOST='xxxx.host.com'
USER='abcd'
PASSWD='xxxx'
/usr/bin/ftp -nv $HOST <user $USER $PASSWD
mput *.txt
EOF
How can i modify this script for sftp...so that the script can automatically login to there sftp server without asking me for the password. As i have already the password of there sftp server. I am able to connect there sftp server manually.I want to do this task through script.

Thanks,
Narendra
6 REPLIES
Prashantj
Valued Contributor

Re: Script - SFTP for file transfer

Hi Narendra,

Main Script
-------------

HOST='xxxx.host.com'
USER='abcd'
PASSWD='xxxx'
sftp -b ${USER}@${HOST}


Authentication:
---------------
On Local box run 'ssh-keygen -t dsa' to generate private and pub keys copy the public key to remote box in '~user/.ssh/authorized_keys'with cat command

Batchfile:
----------
cd
mput *.txt

Tested OK.
Hope this will help you

Prashant


Good judgment comes from experience and experience comes from bad judgment.
Steven E. Protter
Exalted Contributor

Re: Script - SFTP for file transfer

Shalom,

As you see sftp works in a pretty similar way to ftp scripting.

The bad part is the script has to have a password hard coded.

There is a better way.

Public Key authentication. Works for ssh,scp,sftp

http://www.hpux.ws/?p=10

The parent site also has a trick for forcing public key authentication only, to stop random script kiddie authentication guessing.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Ganesan R
Honored Contributor

Re: Script - SFTP for file transfer

Hi Narendra,

First you should configure ssh password less login for that user between source and destination server to automate the sftp script.

Once you configured the password less login you can schedule a simple script like this to do the job.

#--------------------------------------------
# Connect to remote server and put the files
#--------------------------------------------
sftp remoteuser@10.0.0.100 <<**
lcd /data/files
put filename.txt
**
#--------------------------------------------

You can also refer this link for how to generate the public key and sftp script.

http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=1310366
Best wishes,

Ganesh.
Rita C Workman
Honored Contributor

Re: Script - SFTP for file transfer

If you're using 'sftp' then you have the SSH protocols.

As the user (on your side) that is to connect create your keys to transfer to the other server. Run:
ssh-keygen -t

It will create a private key & a public key (id_rsa.pub for example) under the user the .ssh directory. It will create that directory if it doesn't exist.

Now...take that id_rsa.pub key file and send that over to the other server. If you control this remote server, then just put it in the user on that box account in the .ssh directory.
If it's a box you don't control, contact them to make arrangements to get the file there. I don't recommend email, cause sometimes the file corrupts. Send it ftp . The folks on that side can add the key.

The first time you make the connection you will have to respond "yes", depending on the other side you will likely the first time have to give the password. This updates and accepts the new keys.
From this point on you should be go and not require a password again. Whic means.........no password in your script!

Hope this helps,
Rita
Narendra Uttekar
Regular Advisor

Re: Script - SFTP for file transfer

Hi,
I know the ssh for password less authenticaition by generating the keys. But what i am looking is i am already having the password for sftp connection to there server. I can manually connect to there server. But i want to do the sftp file transfer through script for automation.
I have searched on google...some-one was reffering for the except, Is anyone have done the scripting for sftp file transfer with except, Please let me know the procedure.

Thanks,
Narendra
Narendra Uttekar
Regular Advisor

Re: Script - SFTP for file transfer

Hi,
I have installed the 3 software
expect-5.43-hppa-11.11.depot
libXft-2.1.13-hppa-11.11.depot
tcltk-8.5.7-hppa-11.11.depot

and modified the script to use expect for interactive response to answer...

Thanks

Narendra