Help with SFTP script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with SFTP script
# 1  
Old 03-20-2017
Question Help with SFTP script

Hi All,


I just wanted to write a simple SFTP script.
I already referred to plenty of posts before but its of no use. Please dont give me previous posts link for reference. Please comment if you are really interested in solving my issue.

1. I dont want to install SSH key
2. I dont have sshpass installed as well
3. I dont have expect installed as well.

I tried below.

Trial 1:
=====
Code:
/usr/bin/sftp $USER@$HOST <<EOF
quote $PASSWD
cd $PATH
ls -lrt
quit
EOF

Trial 2:
=====
Code:
(
sleep 5
echo "password123"
sleep 5
echo " cd /var/opt/mediation"
sleep 2
echo "ls -lrt"
sleep 2
echo "exit"
sleep 2
)| /usr/bin/sftp user@10.1.1.7

I want to pass password in script somewhere and I dont want sftp session to ask password once again.

Please help...!

Last edited by vbe; 03-20-2017 at 01:35 PM.. Reason: code tags
# 2  
Old 03-20-2017
Hi,

There is one approach that might work. Certainly, what I'm about to discuss worked for me on my own local Linux box. But without a doubt the best solution here is to use SSH keys. But if you really don't want to do this (though they are far more secure than passwords, and a better idea by far in general), then you could try the following.

First, create a script like this, whose sole purpose in life is to echo out your password.

Code:
$ cat sshpass.sh 
#!/bin/bash
echo password

Next, create a plain text file containing the commands you want SFTP to run. For example:

Code:
$ cat ssh.txt
cd /
ls -lrt
quit
$

Next, try the following technique (involving setting the SSH_ASKPASS environment variable and the setsid command) to fool sftp into using your script to provide the password. Note that you don't actually need ssh-askpass installed for this to work.

Code:
$ export SSH_ASKPASS=/home/unixforum/sshpass.sh 
$ setsid sftp unixforum@localhost < ssh.txt
$ Connected to localhost.
sftp> cd /
sftp> ls -lrt
drwxr-xr-x    2 root     root         4096 Aug 20  2013 srv
drwx------    2 root     root        16384 Dec 18  2013 lost+found
drwxr-xr-x    2 root     root         4096 Dec 18  2013 cdrom
drwxr-xr-x    6 root     root         4096 Jul 14  2015 mnt
drwxr-xr-x    3 root     root         4096 Sep  1  2015 omd
drwxr-xr-x   12 root     root         4096 Jan 29  2016 usr
-rw-------    1 root     root     30085120 Feb  8  2016 core
drwxr-xr-x    2 root     root         4096 May 19  2016 snap
drwxr-xr-x   16 root     root         4096 Jun  3  2016 var
drwxr-xr-x    2 root     root         4096 Nov 11 15:43 media
drwxr-xr-x   11 root     root         4096 Dec  5 13:03 opt
drwxr-xr-x    2 root     root         4096 Dec  9 09:58 lib64
drwxr-xr-x   30 root     root         4096 Feb 10 09:55 lib
drwxr-xr-x    5 root     root         4096 Feb 11 09:59 home
lrwxrwxrwx    1 root     root           32 Feb 22 10:08 initrd.img.old
lrwxrwxrwx    1 root     root           29 Feb 22 10:08 vmlinuz.old
drwxr-xr-x    2 root     root        12288 Feb 24 09:56 bin
drwxr-xr-x    2 root     root        12288 Mar  8 10:06 sbin
lrwxrwxrwx    1 root     root           32 Mar  8 10:07 initrd.img
lrwxrwxrwx    1 root     root           29 Mar  8 10:07 vmlinuz
drwxr-xr-x    5 root     root         3072 Mar 10 10:07 boot
dr-xr-xr-x  303 root     root            0 Mar 17 10:36 proc
dr-xr-xr-x   13 root     root            0 Mar 17 10:36 sys
drwx------   26 root     root        12288 Mar 17 13:55 root
drwxr-xr-x  218 root     root        16384 Mar 17 13:55 etc
drwxr-xr-x   21 root     root         4900 Mar 20 10:08 dev
drwxrwxrwt   18 root     root        24576 Mar 20 16:42 tmp
drwxr-xr-x   41 root     root         1500 Mar 20 16:42 run
sftp> quit

Now, a few caveats here. The point of the use of setsid here is to detach sftp from your current session, thus preventing it from picking up on the fact it has a valid terminal attached. Without a valid terminal to prompt for a password, it falls back on the program specified in the SSH_ASKPASS environment variable to provide the login details.

This works, but it's very much an odd way to go about doing things, and will leave your terminal in an odd state with sftp still sort-of-connected to it (you'll see what I mean if you try typing some things at what looks like your shell prompt after doing this).

As I say, the best solution by far here (and the one I've used 100% of the time in similar situations myself with no issues whatsoever) is to use key authentication. That is literally what it was designed for. But if for some reason you're dead set against doing that, then this nasty kludge of a work-around should at least work, if not well or cleanly.
# 3  
Old 03-20-2017
By design, SFTP should prevent you from easily shovelling in credentials.
  • If you want to have a manually driven connection, you can always let it prompt to the screen for the password (you can code the user in the sftp such as sftp remote_user@remote_host)
  • If you want to fully automate the connection, keys are the way to go. Is there a reason that you don't want to use keys?



Robin
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 through script

hi, I have a requirement where i need to sftp the file from another server. Let me describe in detail: when i login into putty i use my id i.e. kusvi and then i start another acc i.e.cdram where all development works takes place...so if i am in cdram and i need to do sftp i have to exit... (1 Reply)
Discussion started by: gnnsprapa
1 Replies

3. UNIX for Dummies Questions & Answers

Sftp script

Hi, I have a file as FILENAME.txt kept in /user/home/smalya . This txt file contains list of file names ex : 1.txt 2. txt 3.txt I want a shell script which will read the file name from this txt file and transfer the files into another server. actual files (1.txt,2.txt,3.txt) are present in... (1 Reply)
Discussion started by: smalya
1 Replies

4. Shell Programming and Scripting

SFTP Script

Hi All, I'm creating an sftp script that will pull data from sftp server and will also move files to a certain folder. My question is that how will i know if the moving of files were successful, is there a way for me to determine it. Thanks, Reign (1 Reply)
Discussion started by: reignangel2003
1 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. UNIX for Dummies Questions & Answers

Script for sftp

Hi, I need a script that automatically connect to a sftp server and send a file. The problem I have is that it need to enter the password. As you might enter the password automatically so it would not ask. Thank you. (7 Replies)
Discussion started by: pepeli30
7 Replies

7. UNIX for Dummies Questions & Answers

sftp script in .sh

Hi, I want download files from Server A to Server B when i want run script. my source server : A my Destination server:B I have to run script in server A only. please help me on this. (2 Replies)
Discussion started by: koti_rama
2 Replies

8. Shell Programming and Scripting

sftp script

I am new to the forum. I need help with writing a unix sftp script that will allow me to put a txt file in the destination server. The destination server is a windows box and when I connect from another windows machine, at the dos prompt I need to Type: ftp<sp> ftp-gw<enter> Type: servername... (2 Replies)
Discussion started by: coburn
2 Replies

9. UNIX for Dummies Questions & Answers

SFTP script

I have this script but it is not working I need help, to transmit a file and rename it. #!/bin/ksh final_file="file name" ftp -nvd IP_address << ENDFTP user username password cd <folder where you want to put the file> bin mput $final_file quit ENDFTP (0 Replies)
Discussion started by: HROMERO
0 Replies

10. Shell Programming and Scripting

sftp through a script

Guys How can I use sftp through a shell script? OR is there is a more secure way to transfer files from one host to another (in a non-secure environment)? Please help. I have tried various options. And I am currently doing the transfer of backups manually, I want to automate this. ... (1 Reply)
Discussion started by: skotapal
1 Replies
Login or Register to Ask a Question