file transfer shell script
Name: Backup Shell Script
Language: /bin/bash
API: OpenGL
Platform: Linux/Solaris
functions.sh
#!/bin/bash
#
# utility functions
#
# - gets included in other scripts
# as it is a list of kewl functions
# - they should go in $HOME/bin to be
# found by some of the other scripts
# that I have writen
#
# Version 1.0 - Corey Auger
# corey@coreyauger.com
#
# set the path like a good little monkey
PATH=/bin:/usr/bin:/sbin:/usr/sbin
#===========================================================
sf_sendFiles() # function to send files to a host and port
#===========================================================
# arg1 HOST = The name of the listening host to send to
# arg2 PORT = The port number to try to send data on
# {arg3, arg4 ..} FILES = all the files to send to the host
{
if [ $# -lt 2 ]; then # wrong number of arguments ?
# print an error and return an error status
echo "Usage: host port {files}"
return 1
fi
SF_HOST=$1; shift # host argument ( in theory )
SF_PORT=$1; shift # port argument ( in theory )
# all other arguments are files
SF_FILES=$@
# this is the actual file transfer line... we first tar all the
# files and then pipe it into a writing netcat connection
tar cpz $SF_FILES | nc -w 1 $SF_HOST $SF_PORT
return $? # return the error code
}
#===========================================================
#=============================================================
sf_recieveFiles()
#=============================================================
# arg1 PORT = The port that we want to bind a listening conn
# arg2 FILE = The tar file that we want to name all the files
{
if ! [ $# -eq 1 ]; then # check the # of args
# print error and exit with return code
echo "Usage: port"
return 1
fi
# tell netcat to listen on the PORT for 50 seconds in the
# background and to write its info to the file name given
# by the second argument
nc -vlp $1 | tar xzvp
return $? # return the error code
}
#=============================================================
filetransfer.sh
#!/bin/bash
#
# filetransfer - sweet file transfer program
#
# - used as a server and client
# - Client: tar there files and send it
# it down the pipe to the server on the
# port of your choice.
# - Server: listen for connections on a
# port ... decompress the tar file when
# it is recieved.
#
# Version 1.0 - Corey Auger
# corey@coreyauger.com
#
# set the path like a good little monkey
PATH=/bin:/usr/bin:/sbin:/usr/sbin
. $HOME/bin/functions # import my kewl functions
if [ $# -lt 2 ]; then # wrong number of arguments ?
# print an error and return an error status
echo "Usage recieve file: $0 r (port)"
echo "Usage send files: $0 s (host) (port) {files}"
exit 1
fi
if [ $1 == 'r' ]; then # check if server mode
shift # get rest of arguments
sf_recieveFiles $@ # send them to function
elif [ $1 == 's' ]; then # check if client mode
shift # get rest of arguments
echo "$@"
sf_sendFiles $@ # send arguments to function
else # first arg was wrong
echo "Usage recieve file: $0 r (port)"
echo "Usage send files: $0 s (host) (port) {files}"
exit 1
fi
if ! [ $? ]; then
echo "Error occurd, in send or recieve"
exit $?
fi
exit 0 # exit
|