Skip to content

Commit

Permalink
FTP timeout and log (#3)
Browse files Browse the repository at this point in the history
* Updated lsb release output

Signed-off-by: ojullien <[email protected]>

* 3.5.0-20200410

Signed-off-by: ojullien <[email protected]>
  • Loading branch information
ojullien committed Apr 10, 2020
1 parent 0ee32ca commit 725c6d8
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/sys/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v3.4.0-20200323
v3.5.0-20200410
2 changes: 1 addition & 1 deletion src/sys/constant.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ Constant::trace() {
FileSystem::checkDir "\tApp directory:\t\t${m_DIR_APP}" "${m_DIR_APP}"
FileSystem::checkFile "\tLog file is:\t\t${m_LOGFILE}" "${m_LOGFILE}"
String::notice "Distribution"
(( m_OPTION_DISPLAY )) && lsb_release --all
(( m_OPTION_DISPLAY )) && lsb_release --description --codename
return 0
}
73 changes: 54 additions & 19 deletions src/sys/ftp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@
## @license MIT <https://github.com/ojullien/bash-sys/blob/master/LICENSE>
## -----------------------------------------------------------------------------

# Copies files over using FTP.
# Copies files over using FTP. Show all responses from the remote server, as well as report on data transfer statistics.
# @param $1 = FTP Host
# $2 = FTP User
# $3 = FTP User password
# $4 = Source file name
# $5 = destination directory
# $6 = local directory
FTP::put() {
# $7 = ftp error file
# $8 = log file
FTP::verbosePut() {

# Parameters
if (($# != 6)) || [[ -z "$1" ]] || [[ -z "$2" ]] || [[ -z "$3" ]] || [[ -z "$4" ]] || [[ -z "$5" ]] || [[ -z "$6" ]]; then
String::error "Usage: FTP::put <FTP Host> <FTP User> <FTP User password> <Source file name> <destination directory> <local directory>"
if (($# != 8)) || [[ -z "$1" ]] || [[ -z "$2" ]] || [[ -z "$3" ]] || [[ -z "$4" ]] || [[ -z "$5" ]] || [[ -z "$6" ]] || [[ -z "$7" ]] || [[ -z "$8" ]]; then
String::error "Usage: FTP::put <FTP Host> <FTP User> <FTP User password> <Source file name> <destination directory> <local directory> <ftp error file> <log file>"
return 1
fi

# Init
local sHost="$1" sUser="$2" sPass="$3" sFile="$4" sDestDir="$5" sLocalDir="$6"
local sHost="$1" sUser="$2" sPass="$3" sFile="$4" sDestDir="$5" sLocalDir="$6" sErrorFile="$7" sLogFile="$8"
local -i iReturn=1 iWordCount=0

# Do the job
if ((m_OPTION_LOG)); then

ftp -pin "${sHost}" <<END_SCRIPT >> "${m_LOGFILE}" 2> ftp.err.$$
ftp -vpin "${sHost}" <<END_SCRIPT >> "${sLogFile}" 2> "${sErrorFile}"
quote USER ${sUser}
quote PASS ${sPass}
binary
Expand All @@ -38,9 +38,44 @@ put ${sFile}
close
quit
END_SCRIPT
iReturn=$?

# Check error
# If the command times out, it exit with status 124.
if ((!iReturn)); then
iWordCount=$(wc --bytes < ${sErrorFile})
if ((iWordCount)); then
iReturn=1
else
iReturn=0
fi
fi

else
ftp -pin "${sHost}" <<END_SCRIPT 2> ftp.err.$$
return ${iReturn}
}

# Copies files over using FTP.
# @param $1 = FTP Host
# $2 = FTP User
# $3 = FTP User password
# $4 = Source file name
# $5 = destination directory
# $6 = local directory
# $7 = ftp error file
FTP::put() {

# Parameters
if (($# != 7)) || [[ -z "$1" ]] || [[ -z "$2" ]] || [[ -z "$3" ]] || [[ -z "$4" ]] || [[ -z "$5" ]] || [[ -z "$6" ]] || [[ -z "$7" ]]; then
String::error "Usage: FTP::put <FTP Host> <FTP User> <FTP User password> <Source file name> <destination directory> <local directory> <ftp error file>"
return 1
fi

# Init
local sHost="$1" sUser="$2" sPass="$3" sFile="$4" sDestDir="$5" sLocalDir="$6" sErrorFile="$7"
local -i iReturn=1 iWordCount=0

# Do the job
ftp -pin "${sHost}" <<END_SCRIPT 2> "${sErrorFile}"
quote USER ${sUser}
quote PASS ${sPass}
binary
Expand All @@ -50,18 +85,18 @@ put ${sFile}
close
quit
END_SCRIPT

fi
iReturn=$?

# Check error
iWordCount=$(wc --bytes < ftp.err.$$)
if ((iWordCount)); then
iReturn=1
else
iReturn=0
# If the command times out, it exit with status 124.
if ((!iReturn)); then
iWordCount=$(wc --bytes < ${sErrorFile})
if ((iWordCount)); then
iReturn=1
else
iReturn=0
fi
fi

[[ -f ftp.err.$$ ]] && rm --force ftp.err.$$ 2>/dev/null;

return ${iReturn}
}
102 changes: 102 additions & 0 deletions src/sys/ftpt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
## -----------------------------------------------------------------------------
## Linux Scripts.
## FTP functions with timeout
##
## @package ojullien\bash\sys
## @license MIT <https://github.com/ojullien/bash-sys/blob/master/LICENSE>
## -----------------------------------------------------------------------------

# Copies files over using FTP. Show all responses from the remote server, as well as report on data transfer statistics.
# @param $1 = FTP Host
# $2 = FTP User
# $3 = FTP User password
# $4 = Source file name
# $5 = destination directory
# $6 = local directory
# $7 = ftp error file
# $8 = log file
FTP::verbosePut() {

# Parameters
if (($# != 8)) || [[ -z "$1" ]] || [[ -z "$2" ]] || [[ -z "$3" ]] || [[ -z "$4" ]] || [[ -z "$5" ]] || [[ -z "$6" ]] || [[ -z "$7" ]] || [[ -z "$8" ]]; then
String::error "Usage: FTP::put <FTP Host> <FTP User> <FTP User password> <Source file name> <destination directory> <local directory> <ftp error file> <log file>"
return 1
fi

# Init
local sHost="$1" sUser="$2" sPass="$3" sFile="$4" sDestDir="$5" sLocalDir="$6" sErrorFile="$7" sLogFile="$8"
local -i iReturn=1 iWordCount=0

# Do the job
timeout 30 ftp -vpin "${sHost}" <<END_SCRIPT >> "${sLogFile}" 2> "${sErrorFile}"
quote USER ${sUser}
quote PASS ${sPass}
binary
cd ${sDestDir}
lcd ${sLocalDir}
put ${sFile}
close
quit
END_SCRIPT
iReturn=$?

# Check error
# If the command times out, it exit with status 124.
if ((!iReturn)); then
iWordCount=$(wc --bytes < ${sErrorFile})
if ((iWordCount)); then
iReturn=1
else
iReturn=0
fi
fi

return ${iReturn}
}

# Copies files over using FTP.
# @param $1 = FTP Host
# $2 = FTP User
# $3 = FTP User password
# $4 = Source file name
# $5 = destination directory
# $6 = local directory
# $7 = ftp error file
FTP::put() {

# Parameters
if (($# != 7)) || [[ -z "$1" ]] || [[ -z "$2" ]] || [[ -z "$3" ]] || [[ -z "$4" ]] || [[ -z "$5" ]] || [[ -z "$6" ]] || [[ -z "$7" ]]; then
String::error "Usage: FTP::put <FTP Host> <FTP User> <FTP User password> <Source file name> <destination directory> <local directory> <ftp error file>"
return 1
fi

# Init
local sHost="$1" sUser="$2" sPass="$3" sFile="$4" sDestDir="$5" sLocalDir="$6" sErrorFile="$7"
local -i iReturn=1 iWordCount=0

# Do the job
timeout 30 ftp -pin "${sHost}" <<END_SCRIPT 2> "${sErrorFile}"
quote USER ${sUser}
quote PASS ${sPass}
binary
cd ${sDestDir}
lcd ${sLocalDir}
put ${sFile}
close
quit
END_SCRIPT
iReturn=$?

# Check error
# If the command times out, it exit with status 124.
if ((!iReturn)); then
iWordCount=$(wc --bytes < ${sErrorFile})
if ((iWordCount)); then
iReturn=1
else
iReturn=0
fi
fi

return ${iReturn}
}

0 comments on commit 725c6d8

Please sign in to comment.