LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 08-30-2013, 02:15 PM   #1
2buck56
Member
 
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:
Code:
#!/bin/bash

# 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 file_grab.sh script on myotherserver

LOG=/home/myuser/mylog.log
DATEDISP=`date +%m%d%Y`
TIMEDISP=`date +%H%M%S`

COMBO=$DATEDISP"_"$TIMEDISP".ABCD"

# 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
else
   echo "The xxxxxx file was not found - `date +"%D %H:%M"`" >> $LOG
   exit
fi

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

rm cmdfile
rm $COMBO
sleep 2

# pick the file back up to confirm it transferred
echo get $COMBO > cmdfile
sleep 2
/home/myuser/user_expect.sh &
sleep 2
GPID=`ps -ef |grep user_expect.sh |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
else
   echo "The $COMBO file transfer to XXXX failed - `date +"%D %H:%M"`" >> $LOG
fi

# remove all references to today's files regardless of transfer results
rm cmdfile
rm $COMBO
This is my expect script:
Code:
#!/usr/local/bin/expect

spawn sftp -b cmdfile ftp.theirserver.com
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, 11:10 AM   #2
rhbegin
Member
 
Registered: Oct 2003
Location: Arkansas, NWA
Distribution: Fedora/CentOS/SL6
Posts: 381

Rep: Reputation: 23
http://stackoverflow.com/questions/5...om-bash-script

See if this helps.
 
Old 08-31-2013, 03:40 PM   #3
2buck56
Member
 
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.
 
  


Reply



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 09:14 AM
setup sftp to use ssh keys + LDAP hophilip Red Hat 1 07-05-2012 02:30 AM
SFTP and SSH session password yourlovenotenough Linux - General 3 08-05-2011 12:56 PM
[SOLVED] ssh keys - no password issue kongfranon Linux - Server 12 10-12-2010 12:25 AM
Distributing SSH host keys for password-less login kenneho Linux - Security 6 09-16-2008 06:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:57 PM.

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