?
Solved

SFTP with using password in a shell script

Posted on 2014-02-18
3
Medium Priority
?
2,663 Views
Last Modified: 2014-02-24
Hello,
I am setting up a SFTP connection, however, need to pass a password to the site.

I normal use a key , however, this time need to use a password.  How do I pass a password with the below information.

This is what I use in the script. (when using a key)

cat /dev/null > $ftp_commands_file
cat /dev/null > $ftp_error_file
echo "lcd /dev/tmp" >> $ftp_commands_file
echo "put $file $/home/dev/$file"  >> $ftp_commands_file

sftp -b $sftp_commands_file $user@$nodename >> $sftp_error_file

Thanks for the help.
0
Comment
Question by:bje
3 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39868919
sftp from OpenSSH does not accept passwords from stdin or from a file.

Check if you have "expect" installed, and if so try something along these lines:


cat /dev/null > $ftp_commands_file
cat /dev/null > $ftp_error_file
echo "lcd /dev/tmp" >> $ftp_commands_file
echo "put $file /home/dev/$file"  >> $ftp_commands_file
echo "exit"  >> $ftp_commands_file

PASS="password"

expect -c "
spawn sftp -b $ftp_commands_file $user@$nodename
expect \"password:\"
send \"$PASS\r\"
interact
" >> $ftp_error_file


I took for granted that the variables "ftp_commands_file", "ftp_error_file", "user" and "nodename" were already set at this point.

Note: I corrected some minor errors/typos in your script.
0
 
LVL 8

Expert Comment

by:Surrano
ID: 39870216
OpenSSH (and I believe the foundations of SSH philosophy in general) reject using a password in a non-interactive session for a good reason. Tools like expect may work but I'd say that any such tool goes against what the first letter of the SSH acronym is about, i.e. "Secure".

Whoever restricted you from using keys should think twice and should be persuaded that
- either they go secure and don't force usage of password,
- or they force usage of password and accept the security leak they'd just imposed.

Of course, I can imagine that they accept passwords only because they don't expect any scripts to connect and you are just trying to circumvent their security measures. In that case, you're probably not really concerned about security, just wish to make your script work. Cross fingers they won't lock your user (or even ban your IP) if they reckon your attempts :)
0
 
LVL 27

Accepted Solution

by:
skullnobrains earned 2000 total points
ID: 39882322
this might be more complete

cat <<EOF | expect
spawn rsync -az $verb $lnk -e ssh $src $dst_.part
set timeout -1
while { true } {
	expect	"Password"		{ send "PASSWORD\n" }	\
		"assword:*"		{ send "PASSWORD\n" }	\
		"DSA"			{ send "yes\n" }	\
		"(yes/no)? "		{ send "yes\n" }	\
		eof			{ exit 0 }		\
		"lost connection"	{ exit 6 }		\
		"Warning: Permanently added * to the list of known hosts." { exp_continue }	\
		"Warning:*" { exp_continue }	\
		"*passphrase*"		{ send "***********\n" }	\
		timeout			{ send_user "\n==> session timeout\n" ; exit 1 }		\
		busy       {puts busy\n ; exp_continue} \
		"Connection to * closed." { exit 4 } \
		#"?*"			{ exit 1 }
}
EOF

Open in new window


obviously replace my rsync command with your sftp/scp/ssh or whatever

---

also, you may want to try curl's sftp which allows you to provide the password on the command line
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This article will show, step by step, how to integrate R code into a R Sweave document
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses
Course of the Month16 days, 12 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question