?
Solved

SFTP with using password in a shell script

Posted on 2014-02-18
3
Medium Priority
?
2,119 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

770 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