SFTP / SCP using password


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SFTP / SCP using password
# 1  
Old 08-24-2012
SFTP / SCP using password

Hi,

I was provided with sftp servername, user and password and the requirement is to connect to sftp server using credentials provided and drop the file.
Manually i am able to connect with commands like
sftp user@servername
and after clicking enter, i was asked for a password and entering the password i am able to get it the remote server.
How can i do the same using scripts i.e when i execute the script, it should connect to server using credentials ( without any manual interverntion )?

I used to do these sftp connections via key exchange ( public key deployed to remote server ) but not sure how to use passwords for sftp connection.

I appreciate your help.
Thanks
# 2  
Old 08-24-2012
sftp works with key based login too. Just upload your public key to ~/.ssh/authorized_keys and make sure, the access mode of the .ssh directory is correct (700).
# 3  
Old 08-24-2012
No.

We are not allowed to use key based connection.
I am required to use credentials and connect to sftp server via script with not manual intervention.
Can i know how can i do this?
# 4  
Old 08-24-2012
Quote:
Originally Posted by forums123456
We are not allowed to use key based connection.
That's unfortunate!

You can't provide the password to sftp via stdin-redirection, because sftp doesn't read stdin, but it opens /dev/tty to do that.

Therefore you have to resort to expect. Read here: https://www.unix.com/shell-programmin...tp-expect.html
# 5  
Old 08-24-2012
This is a simple script I use when I'm not allowed to use public/private keys(which I don't like!!), it might be overkill for what you want to do but at least it gives you some ideas:
Code:
#!/usr/local/bin/expect --
# Script: sftp_file_to.tcl
# Description: Utility to SFTP a file to a specified host
# usage: sftp_file_to.tcl target_server_port target_server user pass target_directory source_directory file_name_to_ftp rename_file

# Set named parameters
set ssh_port                 [lindex $argv 0]
set sftp_server              [lindex $argv 1]
set sftp_user                [lindex $argv 2]
set sftp_pass                [lindex $argv 3]
set target_directory         [lindex $argv 4]
set source_directory         [lindex $argv 5]
set file_name                [lindex $argv 6]
set rename_file              [lindex $argv 7]
set sftp_prompt "sftp>?"
set timeout 300

# Default variable(rename_file) to "N" if not supplied by caller.
if { $rename_file eq "" } {
  set rename_file N
}

# Procedure to connect to SFTP server
proc connect { sftp_pass } {
  variable sftp_prompt
  variable sftp_server
  expect -re "Connecting to.*"                     { exp_continue } \
         -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($sftp_server)."; return 1 }
         timeout                                   { puts "***Error connecting to($sftp_server)."; 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 -oPort=$ssh_port $sftp_user@$sftp_server
set connect_results [connect $sftp_pass]

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

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

  # If rename_file = "Y" transfer file with '.xferring' as extension.
  if { $rename_file == "Y" } {
    set file_name_to_xfer $file_name
    append file_name_to_xfer .xferring
  } else {
    set file_name_to_xfer $file_name
  }

  # Put file to target host.
  set timeout 1800
  exp_send "put $file_name $file_name_to_xfer\r"
    expect "not found."                { abort "\n***Error transfering file($file_name) to: $sftp_server)." } \
           "No such file or directory" { abort "\n***Error transfering file($file_name) to: $sftp_server)." } \
           "Failure"                   { abort "\n***Error transfering file($file_name) to: $sftp_server)." } \
           -re $sftp_prompt            {} \
           timeout { abort "\n***Error transfering file($file_name) to: $sftp_server)." }

  # If rename_file = "Y" remove extension '.xferring' from file name after successful transfer.
  if { $rename_file == "Y" } {
    exp_send "rename $file_name_to_xfer $file_name\r"
      expect "No such file or directory" { abort "\n***Error renaming file($file_name_to_xfer) to($file_name) on $sftp_server)." } \
             "Failure"                   { abort "\n***Error renaming file($file_name_to_xfer) to($file_name) on $sftp_server)." } \
             -re $sftp_prompt            {} \
             timeout { abort "\n***Error renaming file($file_name_to_xfer) to($file_name) on $sftp_server)." }
  }

  # 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



---------- Post updated at 09:48 PM ---------- Previous update was at 02:40 PM ----------

I also found another script I had created to to process sftp commands that are in a different file:
Code:
#!/opt/csw/bin/expect --
# Script: run_sftp_script.tcl
# Description: Utility to run a SFTP script
# usage: run_sftp.tcl target_server_port target_server user pass sftp_script_name

# Set named parameters
set ssh_port                 [lindex $argv 0]
set sftp_server              [lindex $argv 1]
set sftp_user                [lindex $argv 2]
set sftp_pass                [lindex $argv 3]
set sftp_script_name         [lindex $argv 4]
set sftp_prompt "sftp>?"
set timeout 30

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

# Procedure to run SFTP commands from file
proc send_sftp_commands { sftp_script_name } {
  variable sftp_prompt
  #  Slurp up the sftp commands from file
  set fp [open "$sftp_script_name" r]
  set sftp_cmds [read $fp]
  close $fp

  #  Process sftp commands
  set sftp_command_list [split $sftp_cmds "\n"]

  foreach sftp_command $sftp_command_list {
    set comment [string index $sftp_command 0]
    if { $comment == "#" || $comment == "" } {
      continue
    }
    exp_send "$sftp_command\r"
      expect -re "Uploading.*"           { exp_continue } \
             "Failure"                   { abort "\n**Error running command($sftp_command)." } \
             -re $sftp_prompt            {} \
             timeout { abort "\n**Error running command($sftp_command)." }
  }
  return 0
}

# 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 and run sftp commands
spawn sftp -v -oPort=$ssh_port $sftp_user@$sftp_server
set connect_results [connect $sftp_pass]

# If successful connection, continue, else exit denoting error occured.
if { $connect_results == 0 } {
  puts "Successful connection/login to target host($sftp_server)."
  set sftp_results [send_sftp_commands $sftp_script_name]
  # If successful connection, continue, else exit denoting error occured.
  if { $sftp_results == 0 } {
    puts "Successful sftp session to target host($sftp_server)."
  # Successful SFTP session so exit with zero status
  exit 0
  }
}
# Error connecting to SFTP server so exit with non-zero status
puts "***Error running sftp script($sftp_script_name), for target host($sftp_server)."
exit 1

This User Gave Thanks to spacebar For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. Shell Programming and Scripting

SFTP and SCP help

Hi All, I have to make an automated script that needs to do SFTP or SCP from my Unix server to another Unix server. I have gone through search of this website for sftp and scp. I really get confused when it talk about ssh key or rsh key and sftp -b thing I would really appreciate if... (4 Replies)
Discussion started by: pinnacle
4 Replies

4. AIX

what to use sftp or scp

hi, i have a weird problem i have to copy the file with caret(^) in it. but when i tries to copy with scp. It(scp) says that it cant use ^file_name scp mohit^narang user@machine/mohit^narang the error comes in the second parameter.if i used user@machine/mohit_narang(under score) instead... (2 Replies)
Discussion started by: narang.mohit
2 Replies

5. Shell Programming and Scripting

scp script for doing sftp

Hi, I have to do SFTP from Linux machine to Salaries SFTP folder. Using psftp I got the following fingur print and I know using scp I can go the sftp transfer. But I believe I need public key file generated. Can some one pls let me know how to generate the public file using following finger... (1 Reply)
Discussion started by: iamakshay
1 Replies

6. Red Hat

sftp/scp without password

Hi, I want to use sftp/scp without password.How can I do that ?? I plan to use script with scp/sftp and execute by cronjob ,any sample or example?? How can I test the scp/sftp working or not in the same user account , in the same red linux server?? any suggestion ??? (5 Replies)
Discussion started by: chuikingman
5 Replies

7. HP-UX

sftp/scp/ssh script with password as authentication

Hello, Do you guys know set of commands that can incorporate to sftp/scp/ssh to add password in a script to automate file transfer. Our client is not using ssh keys authentication so we are force to create a script to pass the password into the script to transfer files via sftp/scp/ssh. We... (4 Replies)
Discussion started by: james_falco
4 Replies

8. UNIX for Advanced & Expert Users

sftp vs scp

My transmit rates are waaay faster using scp over sftp....anyone know why scp is faster than sftp? I am using solaris 8 for my unix systems. -S (2 Replies)
Discussion started by: Sowser
2 Replies

9. UNIX for Dummies Questions & Answers

sftp/scp autologin

Ive have 2 unix machines i need to transfer files from one to the other. Im trying to use either sftp or scp as ftp is out of the question. Ive created the RSA key on one machine and copied it across but im still prompted for a password. Assume this is due to the fact that im not using the same ID... (8 Replies)
Discussion started by: devid
8 Replies

10. UNIX for Dummies Questions & Answers

OpenSSH: SFTP X SCP

Hi all, I'm implementing some updates to my scripts that are now using ftp command. I was thinking about replace it by SFTP. Now I see that SCP does basically the same that SFTP. So... what are the differences between them? Thanks in advance... ;O) (1 Reply)
Discussion started by: alienET
1 Replies
Login or Register to Ask a Question