[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SFTP with using password in a shell script

Posted on 2014-02-18
3
Medium Priority
?
2,452 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

649 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