Solved

SFTP with using password in a shell script

Posted on 2014-02-18
3
1,265 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 26

Accepted Solution

by:
skullnobrains earned 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Groovy:unable to resolve class error 2 69
C# code editing and collaboration 3 98
Linux  Script File Attributes 5 53
Shell Script on AIX 7 63
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
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now