SFTP password through shell script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SFTP password through shell script.
# 1  
Old 05-27-2013
SFTP password through shell script.

Hi All,

I would be happy, if someone help me on this that I have only SFTP ID and Password to transfer some log files from webserver boxes to SFTP server

Anyone help me that how to pass the password parameter throough the shell scripts, since i don't have ssh login access on the SFTP Server.. only option to pass the password through my script.

Looking some quick response.Thanks in advance
# 2  
Old 05-27-2013
This is a example 'expect' script that should perform what you want(i.e get file from remote host):
Code:
#!/usr/bin/expect --
# Script: sftp_file_from.tcl
# Description: Utility to SFTP a file from(i.e. get) a host(i.e. source)
# usage: sftp_file_to.tcl source_host user pass source_directory local_directory file_name_to_ftp


# Set named parameters
set source_host              [lindex $argv 0]
set sftp_user                [lindex $argv 1]
set sftp_pass                [lindex $argv 2]
set source_directory         [lindex $argv 3]
set local_directory          [lindex $argv 4]
set file_name                [lindex $argv 5]
set sftp_prompt "sftp>?"
set timeout 300

#exp_internal 1;

# Procedure to connect to SFTP server
proc connect { sftp_pass } {
  variable sftp_prompt
  variable source_host
  expect -re "Are you sure you want to continue.*" { exp_send "YES\r"; exp_continue } \
         -re "(Password|password).*:.*"            { exp_send "$sftp_pass\r"; exp_continue } \
         -re $sftp_prompt                          { return 0 } \
         eof                                       { puts "***Error connecting to($source_host)."; return 1 }
         timeout                                   { puts "***Error connecting to($source_host)."; return 1 }
  }

# Procedure to send meesage denoting error, then quit SFTP, then exit with status denoting error.
proc abort { msg } {
  puts "$msg";
  exp_send "quit\r";
  exit 1;
  }

# Connect to the SFTP server
spawn sftp $sftp_user@$source_host
set connect_results [connect $sftp_pass]

# If successful connection, continue, else exit denoting error occured.
if { $connect_results == 0 } {
  # Change to source directory on source server.
  exp_send "cd $source_directory\r"
    expect "No such file or directory" { abort "\n**Error changing to directory($source_directory) on source server." } \
           -re $sftp_prompt            {} \
           timeout { abort "\n**Error changing to directory($source_directory) on source server." }

  # Change local directory.
  exp_send "lcd $local_directory\r"
    expect "No such file or directory" { abort "\n**Error changing to directory($local_directory) on local host." } \
           -re $sftp_prompt            {} \
           timeout { abort "\n**Error changing to directory($local_directory) on local host." }

  # Get file from source host
  set timeout 1800
  exp_send "get $file_name\r"
    expect "not found."                { abort "\n***Error transfering file($file_name) FROM: $source_host)." } \
           -re $sftp_prompt            {} \
           timeout { abort "\n***Error transfering file($file_name) FROM: $source_host)." }

  # QUIT!!
  exp_send "quit\r"
  # Successful SFTP session so exit with zero status
  exit 0
}
# Error connecting to SFTP server so exit with non-zero status
exit 1

# 3  
Old 05-28-2013
Could you generate a key pair with ssh-keygen, retrieve the .ssh/authorized keys from the target server, add your public key to the file and then put that back to the server? That might give you a password-less sign on, but of course, it depends if the server locks your account to a directory from which you cannot get to the .ssh directory. The directory might not exist, so you could probably create it, but you will need to make sure the permissions are rwx --- ---

If you already have a key pair generated, consider connecting as normal with SFTP and:-
Code:
sftp> get .ssh/authorized_keys server_keys
:
:
sftp> !cat server_keys .ssh/id_rsa.pub > new_server_keys
sftp> put new_server_keys .ssh/authorized_keys
:
:
sftp> quit
$ sftp remote-server

Of course, this assumes that the remote server put it's authentication files in ~/.ssh and that you can get to it, but it might be worth a try.


Look in ~/.ssh to see if you already have key generated, but to generate a key-pair / pair of certificates, just run ssh-keygen and follow the prompts.



I hope that this helps. It is not tested and perhaps it's just the hacker in me with a twisted mind, but this might get you going and ease the SFTP to becoming:-
Code:
sftp -b sftp.cmds server

... where sftp.cmds is a plain file containing the cd, get, put or whatever commands to execute when connected.




Robin
Liverpool/Blackburn
UK

Last edited by rbatte1; 05-28-2013 at 06:38 AM.. Reason: Re-phrased a bit
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SFTP script still asking password

Hi All, I have FTP script snippet targetFTP=testcomp userID=testid userPass=XXXXX server_availability () { echo "***********************************************************" >> $FtpLog echo "* Server Availability & User Access Checks *" >> $FtpLog echo... (2 Replies)
Discussion started by: Riverstone
2 Replies

2. Shell Programming and Scripting

SFTP or scp with password in a batch script without using SSH keys and expect script

Dear All, I have a requirement where I have to SFTP or SCP a file in a batch script. Unfortunately, the destination server setup is such that it doesn't allow for shell command line login. So, I am not able to set up SSH keys. My source server is having issues with Expect. So, unable to use... (5 Replies)
Discussion started by: ss112233
5 Replies

3. Shell Programming and Scripting

How to run sftp in shell script without prompting for password?

Hi, Can anyone tell me how to pass password in a shell script for sftp so that i can run it in background without user entering the password? I used a expect script but it timesout after some time and the process remains incomplete at the end. Can anyone suggest any other idea? Will the... (3 Replies)
Discussion started by: Little
3 Replies

4. Shell Programming and Scripting

SFTP prompting for password even though password is in script

Hi All, I am trying to transfer a file from one server to a remote server using SFTP. Client is not ready for key setup. I am working on Solaris 10. Here is the code. #!/bin/ksh # sample automatic Sftp script to dump a file USER="user1" PASSWORD="pass1" HOST="host1" sftp $USER@$HOST... (6 Replies)
Discussion started by: megha2525
6 Replies

5. Shell Programming and Scripting

SFTP-how to log individual sftp command error while executing shell script

Hi, I have situation where i need to automate transferring 10000+ files using sftp. while read line do if ; then echo "-mput /home/student/Desktop/folder/$line/* /cygdrive/e/folder/$line/">>sftpCommand.txt fi done< files.txt sftp -b sftpCommand.txt stu@192.168.2.1 The above... (1 Reply)
Discussion started by: noobrobot
1 Replies

6. Shell Programming and Scripting

sftp batch script with password

I am working on a sftp batch script on a Solaris machine and I need to connect using password. This is not an issue when i do it manually but when I want to make this into a script, i find there are no options for password. Can anyone suggest how I can do it with password? I know using keys is... (3 Replies)
Discussion started by: Leion
3 Replies

7. Shell Programming and Scripting

sftp shell script - Password read error.

Hi, I have script which does the sftp function. In the script in one place it able to read the password from file and other place files with below error. warning: has much more security than supplying the password in the clear warning: on the command line. Failed to get password: File... (0 Replies)
Discussion started by: vino_hymi
0 Replies

8. Shell Programming and Scripting

SFTP in Shell Script with RSA-KEY or password.

I am trying to SFTP to a couple sites. One has an RSA-KEY that was sent to me. Currently I am running that manually using WinSCP. I would like to set it up as a CRON process on our Linux host (Sun). Can I use the rsa-key they sent me in any directory or does it need to be placed in a specific... (2 Replies)
Discussion started by: alemat
2 Replies

9. Shell Programming and Scripting

how to give sftp a password in a shell scripit

I want to write an sftp script, but I can't put ssh keys on my destination. Is it possible to pass sftp a password inside a script? I know that the -b option makes sftp use a file for params. Can the password go in such a file? How can I do this? Thanks in advance (3 Replies)
Discussion started by: kskywr
3 Replies

10. UNIX for Advanced & Expert Users

Change the password in 30 days in sftp script.

Hi, I'm writing a script which actually sftp's(gets!! i'm using mget) files from a windows box to unix server. (i've gone thru forum and got pretty good answers, Thx for that). but the windows box to which i'm ftp'g prompts for a password change every 30 days. so please suggest me how to do... (3 Replies)
Discussion started by: rosh0623
3 Replies
Login or Register to Ask a Question