My l3g3nd

There is no spoon

SFTP File Upload Script

| Comments

Following is the script that can be used to upload a file to a secure ftp (SFTP) when there is a limitation of sharing ssh keys and it is okay to pass the password in the script. With above in mind additional package SSHPASS needs to be installed. SSHPASS is in the Fedora repo however it can be installed on CentOS 5.x x86_64:

1
2
3
wget -O epel.rpm https://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm # Download latest  epel-release rpm
sudo rpm -Uvh epel.rpm # Install the package
sudo yum install sshpass # Install sshpass

Once everything is OK following script can be used:

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/sh

for i in /dir/*; do   # Checking for file in the folder
  if [ -f "$i" ]; then
 file=$i
export SSHPASS=password   # Password is the login passwd of the SFTP
sshpass -e sftp -oBatchMode=no -b - user@ftp.com
   cd foldername/foldername          # Moving the file to local archive 
   put $file
   bye
!
mv $file /somedir/test
 fi

As I only wanted to initiate the connection when there is file in the folder so I kept it under the if loop. This script can be setup in the cron job to run every 60sec which was my req at that time.

Comments