Run SFTP - Pwd From Bash Script

4

Click here to load reader

description

Run SFTP - Pwd From Bash Shell Script unix linux command

Transcript of Run SFTP - Pwd From Bash Script

Page 1: Run SFTP - Pwd From Bash Script

     sign up log in tour help  stack overflow careers

 Sign up ×Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

How to run the sftp command with a password from Bash script?

I need to transfer a log file to a remote host using   from a Linux host. I have been provided credentials for the same from my operationsgroup. However, since I don't have control over other host, I cannot generate and share RSA keys with the other host.

sftp

So is there a way to run the   command (with the username/password provided) from inside the Bash script through a   job?sftp cron

I found a similar Stack Overflow question,  , but there was no satisfactory answer to my problem.Specify password to sftp in a Bash script

       bash shell unix ssh sftp

edited Nov 11 '14 at 21:36Eric Leschinski28.8k 17 150 147

asked Mar 22 '11 at 3:40anubhava294k 27 103 177

9 Answers

You have a few options other than using public key authentication:

1.  Use keychain

2.  Use   (less secured but probably that meets your requirement)sshpass

3.  Use   (least secured and more coding needed)expect

If you decide to give sshpass a chance here is a working script snippet to do so:

export SSHPASS=your‐password‐heresshpass ‐e sftp ‐oBatchMode=no ‐b ‐ sftp‐user@remote‐host << !   cd incoming   put your‐log‐file.log   bye!

edited Jun 27 '14 at 17:19Andrew Medico14.7k 8 31 58

answered Mar 22 '11 at 3:54anubhava294k 27 103 177

1    –    For using it on Mac: stackoverflow.com/questions/9102557/… anubhava Feb 27 '12 at 14:18

1    –    No need to export, I think.   should do.SSHPASS=password sshpass ‐e ... Jens Mar 28 '13 at 13:10

5   –    

I was able to do a one­liner to dowload a log file:  sshpass ‐p "my_password" sftp ‐oPort=9999user@host:dir/file.log gorus Jun 18 '13 at 20:24

2    –     was the crucial missing piece for me.‐oBatchMode=no richardkmiller Jun 6 '14 at 4:58

1    –    You can add   to redirect the output and stderr to a file.> ~/file.out 2>&1 anubhava Sep 8 at 13:46

Another way would be to use lftp:

lftp sftp://user:password@host  ‐e "put local‐file.name; bye"

OR the cool mirroring method (can include delete after confirmed transfer '­­Remove­source­files'):

lftp ‐e 'mirror ‐R /local/log/path/ /remote/path/' ‐u user,password sftp.foo.com

answered Jul 24 '13 at 6:20Karassik253 2 7

3    –    +1 for suggesting an alternative but can we avoid supplying password on command line? anubhava Jul24 '13 at 6:57

Page 2: Run SFTP - Pwd From Bash Script

Expect is a great program to use.

On Ubuntu install it with:

sudo apt‐get install expect

On a CentOS Machine install it with:

yum install expect

Lets say you want to make a connection to a sftp server and then upload a local file from yourlocal machine to the remote sftp server

#!/usr/bin/expect

spawn sftp [email protected] "password:"send "yourpasswordhere\n"expect "sftp>"send "cd logdirectory\n"expect "sftp>"send "put /var/log/file.log\n"expect "sftp>"send "exit\n"interact

This opens a sftp connection with your password to the server.

Then it goes to the directory where you want to upload your file, in this case "logdirectory"

This uploads a log file from the local directory found at /var/log/ with the files name being file.log tothe "logdirectory" on the remote server

edited Jun 27 '14 at 17:18Andrew Medico14.7k 8 31 58

answered Mar 28 '13 at 13:04rezizter1,065 9 18

     –    Thanks for your answer, I had expect as one of my options for achieving this task. anubhava Mar 28'13 at 13:09

    –    

Thank you. I found your answer through a search, used it and built a script successfully with expect. Justposted the script for poterity rezizter Mar 28 '13 at 13:11

     –    Thank you so much for you answer, I have already upvoted it :) anubhava Mar 28 '13 at 13:12

     –    This worked flawlessly for me. Thank you! Scott Fleming Dec 1 '14 at 16:40

You can use lftp interactively in a shell script so the password not saved in .bash_history orsimilar by doing the following:

vi test_script.sh

Add the following to your file:

#!/bin/shHOST=<yourhostname>USER=<someusername>PASSWD=<yourpasswd>

cd <base directory for your put file>

lftp<<END_SCRIPTopen sftp://$HOSTuser $USER $PASSWDput local‐file.namebyeEND_SCRIPT

And write/quit the vi editor after you edit the host, user, pass, and directory for your put file typing  .Then make your script executable   and execute it 

.:wq chmod +x test_script.sh./test_script.sh

answered Jul 29 '13 at 22:27

Page 3: Run SFTP - Pwd From Bash Script

Mike S.2,484 12 20

1    –     

To overwrite existing file, add "set xfer:clobber on" after lftp<<END_SCRIPT Balaji Natarajan Dec 17 '13at 17:49

I was recently asked to switch over from ftp to sftp, in order to secure the file transmissionbetween servers. We are using Tectia SSH package, which has an option   to passthe password on the command line.

‐‐password

example :  sftp ‐‐password="password" "userid"@"servername"

Batch example :

(  echo "  ascii  cd pub  lcd dir_name  put filename  close  quit    ") | sftp ‐‐password="password" "userid"@"servername"

I thought I should share this information, since I was looking at various websites, before runningthe help command ( ), and was i surprised to see the password option.sftp ‐h

edited Jun 27 '14 at 17:18Andrew Medico14.7k 8 31 58

answered Apr 5 '13 at 6:52Mahony91 1 1

4   –    

+1 for you good suggestion. However this will require you to pass the password on command line andanybody on the system doing   command would be able to see your password.ps anubhava Apr 5 '13at 11:54

You can override by enabling Password less authentication. But you should install keys (pub,priv) before going for that.

Execute the following commands at local server.

Local $> ssh‐keygen ‐t rsa 

Press ENTER for all options prompted. No values need to be typed.

Local $> cd .sshLocal $> scp .ssh/id_rsa.pub user@targetmachine:Prompts for pwd$>  ENTERPASSWORD

Connect to remote server using the following command

Local $> ssh user@targetmachinePrompts for pwd$> ENTERPASSWORD

Execute the following commands at remote server

Remote $> mkdir .sshRemote $> chmod 700 .sshRemote $> cat id_rsa.pub >> .ssh/authorized_keysRemote $> chmod 600 .ssh/authorized_keysRemote $> exit

Execute the following command at local server to test password­less authentication. It should beconnected without password.

$> ssh user@targetmachine

edited Nov 11 '14 at 22:15Eric Leschinski28.8k 17 150 147

answered Feb 20 '14 at 6:34realspirituals4,579 6 18 46

     –    +1 for your answer but question was about doing it without public/private keys. anubhava Feb 20 '14 at

Page 4: Run SFTP - Pwd From Bash Script

6:37

     –    oops.. Just saw the restrictions on key sharing :) realspirituals Feb 20 '14 at 6:41

    –  

Out of curiosity will this command   wheninvoked from a shell script print them some where? How did you solve apart from sshpass?

lftp ‐p ${port} ‐u ${login_id},${password} ${ip_number}realspirituals

Feb 20 '14 at 6:43

     –    I don't have   and rely on   from Mac and Linux both.lftp sshpass anubhava Feb 20 '14 at 6:47

     –    Pure gold. Thanks! :) Nikolay Tsenkov Mar 13 at 16:32

Combine sshpass with a locked­down credentials file and, in practice, it's as secure as anything ­if you've got root on the box to read the credentials file, all bets are off anyway.

answered Dec 12 '11 at 11:39Rich Harding125 3

Bash program to wait for sftp to ask for a password then send it along:

#!/bin/bashexpect ‐c "spawn sftp username@your_hostexpect \"Password\"send \"your_password_here\r\"interact "

You may need to install expect, change the wording of 'Password' to lowercase 'p' to match whatyour prompt receives. The problems here is that it exposes your password in plain text in the fileas well as in the command history. Which nearly defeats the purpose of having a password in thefirst place.

answered Nov 11 '14 at 20:32Eric Leschinski28.8k 17 150 147

     –    +1 but I think you state it clearly what the problems are with this approach. anubhava Nov 11 '14 at20:57

You can use sshpass for it. Below are the steps

1.  Install sshpass For Ubuntu ­  sudo apt‐get install sshpass

2.  Add the Remote IP to your known­host file if it is first time For Ubuntu ­> ssh user@IP ­>enter 'yes'

3.  give a combined command of scp and sshpass for it. Below is a sample code for war copingto remote tomcat  sshpass ‐p '#Password_For_remote_machine' scp /home/ubuntu/latest_build/abc.war #user@#RemoteIP:/var/lib/tomcat7/webapps

answered Jun 12 at 7:33ravi ranjan1,836 1 5 9

 by   protected Mark Hall Aug 8 '13 at 3:03

Thank you for your interest in this question. Because it has attracted low­quality answers, posting an answer now requires 10   on this site. 

Would you like to answer one of these   instead?

reputation

unanswered questions