SFTP in a shell script without public/private key


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SFTP in a shell script without public/private key
# 1  
Old 12-17-2009
Data SFTP in a shell script without public/private key

Hi everybody,

I need some help on writing a script that is able to remote copy file to one server. I already created this types of scripts, and works ok as long as this server I want to copy from is access through telnet. Here is how I do it:

Code:
ftp -n xxx.xxx.xxx.xxx << _EOF_
user user_name password
cd $directory_path
ascii
prompt off
get file_i_want_to_copy
_EOF_

So you see that the input << allows the user name and password to be prompt.

The problem is when I try to do the same with the other servers that can be accessed only through ssh protocol. I tried to do this:

Code:
sftp -b batfile username@host

where my batfile contains:

Code:
password
cd $directory_path
get file_i_want_to_copy
quit

But still when I run the script it prompts for the password, and (after i enter the password) it says that there is no such file in such path....

I already sniffed around the net, and I already comprehend that sftp is mainly interactive, that the best way is to do it in a script is through scp with using a public/private key. But my problem is:
  1. With scp, instead of remote cp to the server where the script is runing, I have to write a script in the server I want to copy from
  2. I really rather not create a public/private key as root, but the files are accessible as root.

But right now, I´m open to suggestions, and in case there is no way to do this withouth the creation of a key, can you guys show me how to create this keys, and how it works??
There is still a lot of thing to learn in unix...... a lot!

Thanks for your time and help.

---------- Post updated at 05:29 PM ---------- Previous update was at 03:26 PM ----------

Hey guys, a little help please?? Kind of desperate here...
Thanks a lot
# 2  
Old 12-17-2009
Quote:
With scp, instead of remote cp to the server where the script is runing, I have to write a script in the server I want to copy from
Why that? You can usually copy from anywhere in both directions with scp, as well as you can with rcp.

Allowed pub-key makes filteransfer much more relaxing.
In our internal environment we mostly use pubkey authentication to run our file transfers via scp.
Exchanging keys is usually more secure than having the username and password unencrypted in some file.

There are plenty of threads in the forum and also on the web how to setup passwordless pub key authentication. Use the forum's search engine maybe.
# 3  
Old 12-17-2009
Agreed. You need to setup the key authentication for scripts to really work well.
# 4  
Old 12-17-2009
The openSSH standard actually "forbids" reading a password from a file in an interactive shell script. If the script runs without a controlling terminal in the process tree, it will allow it.
# 5  
Old 12-18-2009
Quote:
Originally Posted by zaxxon
Why that? You can usually copy from anywhere in both directions with scp, as well as you can with rcp.

Allowed pub-key makes filteransfer much more relaxing.
In our internal environment we mostly use pubkey authentication to run our file transfers via scp.
Exchanging keys is usually more secure than having the username and password unencrypted in some file.

There are plenty of threads in the forum and also on the web how to setup passwordless pub key authentication. Use the forum's search engine maybe.

Zax one example you can't allow SCP is because of security standards. I have to use the SFTP-internal subsystem jailing and that doesn't allow any protocol but SFTP. You can still do Pubkey authentication with it though. THe scripting is a little more of a challenge though hah


Also Jim, how do you run a script with no controlling terminal? like ssh -t?
# 6  
Old 12-18-2009
Have a look at lftp. It supports scripting sftp sessions.
# 7  
Old 12-30-2009
Thanks!

Hi all.... sorry for this very long delay, but I went on a trip.

After acknowledging the anwser of Zaxxon, I realized that I could solve my particulary problem by writing two scripts on the two servers I wanted to copy from using ftp, since the issue was that I could not connect through ftp from server 1 (the server I needed to copy to), but it was possible to connect through ftp from the other servers (the ones I wanted to copy from).
Hope this is not confusing....Smilie

Anyway, by writing this two scripts, and using the put command, I worked out my problem in time so I could go on my vacations without a guilty conscience...

But ofcourse the ideia was to write just one script on the main server, so thats why I posted this question, but it seemes that ssh and sftp will not allow to work in scripts without promping the user and password, only the use of public/private keys...

Anyway, and once again, your help was preciouse and again sorry for so late replay from me.
Thank you all Smilie


P.S. By the way, in case I could not use ftp on either direction, and the use of public/private keys was not an option (case where the server are crucial and very sensible to errors one might do, and the perimission to mingle with the .ssh directory is not recommended) there is absolutly no way to write scripts capable of copying files from other servers??!! Just a question, since I found nothing satisfying on net....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Private and public key encryption

Hi, we have private and public key, encrypt file using public and want to decrypt using private key. can you please advise below commands are correct or other remedy if unix have? encrypt -a arcfour -k publickey.asc -i TESTFILE.csv -o TESTFILE00.csv decrypt -a arcfour -k privatekey.asc... (2 Replies)
Discussion started by: rizwan.shaukat
2 Replies

2. Shell Programming and Scripting

Rsa public private key matching

Hi All, I have a requirement where i need to check if an rsa public key corresponds to a private key and hence return success or failure. Currently i am using the command diff <( ssh-keygen -y -e -f "$PRIVKEY" ) <( ssh-keygen -y -e -f "$PUBLICKEY" ) and its solving my purpose. This is in... (1 Reply)
Discussion started by: mritusmoi
1 Replies

3. UNIX for Dummies Questions & Answers

Public and Private Key generation for scp

Hi, What tool is used to generate public and private keys for SCP? Do you have an example script that generates these keys, puts them in files and then another example script that references them from SCP? Thanks, (9 Replies)
Discussion started by: Astrocloud
9 Replies

4. Solaris

Public private key setup issue in Solaris 10

Hi i am using solaris 10.I am trying to setup a public/private key but it is not working.Appreciate your repsonse on it There are two servers DB1 server and DB2 server. 1)I have generated public/private key using below step on both servers. ssh-keygen -t rsa 2)From DB1 server moved the... (6 Replies)
Discussion started by: muraliinfy04
6 Replies

5. Shell Programming and Scripting

sftp using private and public keys

hi guys , i have few files i have to do sftp, the public and private key work like a magic , no problem at all the syntax is as below sftp -0 identityfile=~/ure/blsl/loc2/.ssh/id_rsa_ssh1 ssh1@remote >log_dir/file.timestamp<<end lcd folder cd folder put * quit end ================ my... (3 Replies)
Discussion started by: rockymayavia
3 Replies

6. Shell Programming and Scripting

SFTP - Private and Public keys

Hi All, I have a query....say on server A, I have generated the Private and Public keys and shared the public key with server B. Now i can surelyconnect(without password) from server A to server B..... but can i similarly connect from server B to server A as well Regards (1 Reply)
Discussion started by: Arpit Narula
1 Replies

7. Shell Programming and Scripting

Shell script for SFTP using Public and private keys

Hi all, I needed a shell script for file transfering using public/private keys for authentication. Could you please help me out on this? A procedure to write a shell script is enough. Thanks in advance. Regards. Vidya N (8 Replies)
Discussion started by: Vidya N
8 Replies

8. UNIX for Dummies Questions & Answers

how to create a public/private key using ssh-keygen

Hi, please guide me create a public/private key using ssh-keygen, lets say I have been access to server named pngpcdb1with a userid and password ...!!! and also please explain in detail the concept of these keys and ssh as I was planning to use them in ftp related scripts..! Thanks in... (1 Reply)
Discussion started by: rahul125
1 Replies

9. UNIX for Dummies Questions & Answers

SSL Public key/Private question

Hi everyone, I have a quick/newb question: I know that a public key is used to encrypt data and a private key is used to decrypt data but who keeps the public/private keys?? Does the Web Server hold both? Does the Web Server have the public key and does the client have the private key? ... (3 Replies)
Discussion started by: tical00
3 Replies

10. Windows & DOS: Issues & Discussions

Public/Private Key SSH from UNIX to Windows (Cygwin)

Hello all, I have a bit of trouble working a passwordless SSH from UNIX to Cygwin running windows 2k3. Here are some details. I AM able to SSH from the Windows box to the UNIX box using the keys. Also, I'm able to SSH from UNIX to Windows w/o the keys. However, when I try to do it with the keys... (9 Replies)
Discussion started by: kclerks11
9 Replies
Login or Register to Ask a Question