automate sftp using unix script | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

automate sftp using unix script

Shell Programming and Scripting


Tags
linux, linux commands, mtime, sftp script, solaris

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-01-2005
priyamurthy2005 priyamurthy2005 is offline
Registered User
 
Join Date: Apr 2005
Last Activity: 20 April 2005, 9:31 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
automate sftp using unix script

Hi All,
I need to write a UNIX script that automates the sftp process. I should be able to do a sftp to a secure box and get a file from there. I am having a problem doing this because no matter what I do, when I run my script, I get a prompt at command line asking for a password. How could I automate this? Could someone give me a sample script?
Thanks in advance.
Priya
Sponsored Links
    #2  
Old 04-01-2005
dangral dangral is offline Forum Advisor  
Registered User
 
Join Date: Oct 2002
Last Activity: 31 July 2013, 6:52 PM EDT
Posts: 723
Thanks: 3
Thanked 4 Times in 4 Posts
This has been answered before in the forums here.
If you don't want to specify a password you need to generate a passwordless key with ssh-keygen and then copy the public key to the target host's ~/.ssh/authorized_keys file. This also has been discussed here before. Try a search.
Sponsored Links
    #3  
Old 04-01-2005
priyamurthy2005 priyamurthy2005 is offline
Registered User
 
Join Date: Apr 2005
Last Activity: 20 April 2005, 9:31 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you so much dangral. I looked at your reply and looked at the sites you suggested. I am still at a loss. It asks me to use ssh. I tried that and created a public key file. Now it asks me for passphrase for public key file. I do not want any interactive mode at all. I somehow want it to automatically take the password for a file or something. I tried the following:
sftp -b passwordfile username@IP
The file "passwordfile" has passowrd stored in it. Still, it does not work. It first asks me for password interactively and only after I typed the password, it will execute the "passwordfile".
I looked on help for sftp and I was told that if I use non-interactive authentication, only then the above statement will work, meaning, it will take password for the batch file.
My question now is that: how do I achieve non-interactive authentication?

Thanks in advance.
    #4  
Old 04-01-2005
dangral dangral is offline Forum Advisor  
Registered User
 
Join Date: Oct 2002
Last Activity: 31 July 2013, 6:52 PM EDT
Posts: 723
Thanks: 3
Thanked 4 Times in 4 Posts
I think you have two issues here:

1) Achieving passwordless authentication
2) automating sftp

I will try to discuss one at a time. Forgetting about sftp for a second, in order to achieve passwordless authentication ( in your terms non-interactive authentication) you need to set up a key that does not have a password. See this link for tips on how to set that up.

However, please see this warning and tips on how to set it up to be secure. (Sorry for all the links)

Once you have verified passwordless authetication is working, you can now attempt to automate sftp. What the link in my previous post said is:
Quote:
However, it's nice to keep everything together in one file using "here documents." What you can do, is this:

#!/bin/sh
echo "OK, starting now..."
sftp -b /dev/stdin remotehost <<EOF
cd pub
...

Most modern Unix flavors provide a way to access the current process' standard streams via the filesystem. Linux has /dev/stdin; under Solaris, it would be /dev/fd/0 yours may be different.
You would not be putting a password file after sftp -b, you would be putting your standard-in device or a file with all the ftp commands. So on solaris, your script might look like this:


Code:
  
  #!/bin/sh
  echo "OK, starting now..."
  sftp -b /dev/fd/0 remotehost <<EOF
  cd pub
  ascii
  get filename.txt
  bye
  EOF


Last edited by dangral; 04-01-2005 at 12:00 PM.. Reason: added security warning
Sponsored Links
    #5  
Old 04-01-2005
priyamurthy2005 priyamurthy2005 is offline
Registered User
 
Join Date: Apr 2005
Last Activity: 20 April 2005, 9:31 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks again. Unfortunately, the command ssh-keygen does not work on my machine. It gives me the error:
ssh-keygen not found.
I guess I need to call my system admin and maybe get something installed, right???
Sponsored Links
    #6  
Old 04-20-2005
RishiPahuja's Avatar
RishiPahuja RishiPahuja is offline
Registered User
 
Join Date: Apr 2005
Last Activity: 15 April 2011, 1:11 AM EDT
Location: Bangalore, India
Posts: 203
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by dangral
I think you have two issues here:

1) Achieving passwordless authentication
2) automating sftp

I will try to discuss one at a time. Forgetting about sftp for a second, in order to achieve passwordless authentication ( in your terms non-interactive authentication) you need to set up a key that does not have a password. See this link for tips on how to set that up.

However, please see this warning and tips on how to set it up to be secure. (Sorry for all the links)

Once you have verified passwordless authetication is working, you can now attempt to automate sftp. What the link in my previous post said is:


You would not be putting a password file after sftp -b, you would be putting your standard-in device or a file with all the ftp commands. So on solaris, your script might look like this:


Code:
  
  #!/bin/sh
  echo "OK, starting now..."
  sftp -b /dev/fd/0 remotehost <<EOF
  cd pub
  ascii
  get filename.txt
  bye
  EOF


Hi,
When I tried to use the snippet of your code, I am unable to connect. the -b option used by you is not viable in solaris. here it stands for buffersize.

> sftp -h
Usage: sftp2 [-D debug_level_spec] [-B batchfile] [-S path] [-h]
[-V] [-P port] [-b buffer_size] [-N max_requests]
[-c cipher] [-m mac] [-4] [-6] [-o option_to_ssh2]
[user@]host[#port]
> sftp -V
sftp: F-Secure SSH 3.1.0 (build 12) on sparc-sun-solaris2.8
>

Thanks
Rishi
Sponsored Links
    #7  
Old 04-20-2005
dangral dangral is offline Forum Advisor  
Registered User
 
Join Date: Oct 2002
Last Activity: 31 July 2013, 6:52 PM EDT
Posts: 723
Thanks: 3
Thanked 4 Times in 4 Posts
You are using F-Secure sftp. The version I was referring to is OpenSSH.
Try using -B instead of -b.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Using expect to automate sftp vedder191 Shell Programming and Scripting 0 06-14-2011 12:40 PM
How to automate sftp without using expect script? Tuxidow Shell Programming and Scripting 5 06-28-2010 02:52 AM
How to automate sftp in a script to 'get' files. ChicagoBlues Shell Programming and Scripting 1 08-10-2009 03:07 PM
Automate SFTP UNIX to Windows mahiban AIX 6 05-13-2009 10:34 AM
Automate SFTP UNIX to Windows mahiban Windows & DOS: Issues & Discussions 1 07-22-2008 05:56 AM



All times are GMT -4. The time now is 05:12 AM.