Sample Unix/Linux sftp shell scripting to read and xml, find data file, and transfer to another server

Posted by Jiltin     22 January, 2009    1,352 views   

The following linux bash script shows sample sftp shell script to read and xml, find data file, and transfer to another server.

You must have set the ssh trust setup between the servers. By setting up the trust, you do not need to input a password.

#!/bin/sh
#**************************************************************************************************
#
# Script Name: sftpdata.sh
#
# Developed by Jay
#**************************************************************************************************
# Sample Script unix linux sftp – reading xml and send the data file from one server to another
# Parameter: The first parameter must be the XML Filename
#
# Usage: sftpdata.sh abcd.xml
#**************************************************************************************************
#
SFTPUID="<uid>"
SFTPPWD="<pwd>"
SFTPSVR="<server>"

LOCATION="/home/notesbit/test"                                                                                                                                  # change to correct position
XMLFILE="${LOCATION}/$1"                                                                                                                                                        # It should be XMLFILE="${LOCATION}/$1"

#echo "XMLFILE=${XMLFILE}"                                                                                                                                              #Test to see the filename

DATLIST=`grep -i "\.dat" ${XMLFILE} ‘ sed -e ‘s/<[^>]*//g‘ ‘  sed ’s/>//g’`             # This removes xml tags and gets only the *.dat file in a list

for filename in ${DATLIST}                                                                                                                                                                                                              # List is read in loop
do

sftp -b -oPort=22 ${SFTPUID}@${SFTPSVR} << EOF
put $filename
echo $?                 # this gives return status
quit
EOF

done

If you do not setup any trust, you need to use expect utility. If you do not have “expect” utility, please install it in the linux machine.

yum install expect

will install the utility. You can modify the following script suitably to replace

#!/usr/bin/expect
spawn  sftp -b batchfile user@ipaddress
expect "password:"
send "userpassword\n";
interact

Following Google Searches Lead To This Post: oracle 10gR2 “fedora 11″

Post to Twitter  Post to Delicious  Post to Digg    Post to StumbleUpon

Categories : Scripts Unix Tags : , , , ,

Comments

No comments yet.


Leave a comment

(required)

(required)