Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 08-30-2013, 03:15 PM   #1
Registered: Oct 2004
Posts: 54

Rep: Reputation: 15
SFTP with both SSH keys and password

I have a client that I transfer files to each night. For years the process has just been a simple FTP process using .netrc and SSH keys to handle the transfers. The client is moving everything to a new server. The new server requires both SSH keys and password authentication for "increased security". They will not drop the password authentication even though I told them I don't think it adds any security over SSH keys.

Logging into their server manually using SSH keys and password authentication works fine. I first though of using a Perl script as it can handle both the keys and the password. However, my server is about 5 years old and does not have the Perl Net::SFTP or Net:Foreign modules installed. I tried installing them but their are so many dependencies that it isn't worth the effort. I can't use CPAN because the server is behind a PCI firewall and does not have internet access.

I have come up with a solution where I have a bash script calling an expect script. That works fine but I am somewhat concerned about reliability in the case of their server being temporarily unavailable. The reason for the bash script is I need to do write status messages to a log file. I will be retrieving the status messages from the log and sending out emails with that information to several individuals.

Does anyone have a better idea of how to handle the sftp/password requirement?

This is my bash script:

# We have to call the expect script from this script since their server
# requires both a SSH key and a username/password combination to login
# The xxxx file is placed in the myuser home directory by the
# xxxxxxx user's script on myotherserver

DATEDISP=`date +%m%d%Y`
TIMEDISP=`date +%H%M%S`


# rename the xxxxxx file to format required by XXXX
mv "xxxxxx" $COMBO > /dev/null 2>&1
if [ "$?" = "0" ]; then
   echo "The xxxxxx file was renamed to $COMBO - `date +"%D %H:%M"`" >> $LOG
   echo "The xxxxxx file was not found - `date +"%D %H:%M"`" >> $LOG

echo put $COMBO > cmdfile
sleep 2
# call the expect script to send file
/home/myuser/ &
sleep 2
PPID=`ps -ef |grep |awk '{ print $2 }'`
wait $PPID

rm cmdfile
sleep 2

# pick the file back up to confirm it transferred
echo get $COMBO > cmdfile
sleep 2
/home/myuser/ &
sleep 2
GPID=`ps -ef |grep |awk '{ print $2 }'`
wait $GPID

ls -l |grep $COMBO
if [ "$?" = "0" ]; then
   echo "The $COMBO file was successfully transferred to XXXX - `date +"%D %H:%M"`" >> $LOG
   echo "The $COMBO file transfer to XXXX failed - `date +"%D %H:%M"`" >> $LOG

# remove all references to today's files regardless of transfer results
rm cmdfile
This is my expect script:

spawn sftp -b cmdfile
expect "password:"
send "Bad.1234\n";
expect "sftp>"
expect interact
Any ideas would be appreciated. However due to the age of my server, I will be limited as to what programs I can install.
Old 08-31-2013, 12:10 PM   #2
Registered: Oct 2003
Location: Arkansas, NWA
Distribution: Fedora/CentOS/SL6
Posts: 381

Rep: Reputation: 23

See if this helps.
Old 08-31-2013, 04:40 PM   #3
Registered: Oct 2004
Posts: 54

Original Poster
Rep: Reputation: 15
I believe at this point the expect solution I have written is probably the best way to handle this.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
ssh requires password even though Ive set up keys. sr_25 Debian 10 01-09-2014 10:14 AM
setup sftp to use ssh keys + LDAP hophilip Red Hat 1 07-05-2012 03:30 AM
SFTP and SSH session password yourlovenotenough Linux - General 3 08-05-2011 01:56 PM
[SOLVED] ssh keys - no password issue kongfranon Linux - Server 12 10-12-2010 01:25 AM
Distributing SSH host keys for password-less login kenneho Linux - Security 6 09-16-2008 07:52 AM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:33 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration