Skip to content

Instantly share code, notes, and snippets.

@aliaksandr-master
Last active September 13, 2015 17:01
Show Gist options
  • Save aliaksandr-master/b9edbc0cc1bc0631317d to your computer and use it in GitHub Desktop.
Save aliaksandr-master/b9edbc0cc1bc0631317d to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
DATE=`date +%Y-%m-%d--%H-%M-%S`
MAINDIR=`pwd`
THIS_SHELL_FILE_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
function simLink() {
local destFile=$1
local soruceFile=$2
if [ -L ${destFile} ];
then
echo ">> File '${destFile}' is EXISTS ... will do nothing"
else
if [ -f ${destFile} ];
then
echo ">> ERROR! >> File ${destFile} is EXISTS! and this is regular"
exit;
else
ln -s ${soruceFile} ${destFile}
echo ">> File '${destFile}' Created as symLink of '${2}'"
fi
fi
}
# use:
# $ defaultVal nameOfvar defaultValue
function defaultVal() {
local varname=$1
local defValue=$2
if [ -z "${!varname}" ]; then
eval $varname="$defValue"
fi
}
function readWithDefault() {
local message=$1
local varname=$2
local defaultValue=$3
local val
echo -n ${message}
read val
if [ -z "$val" ]; then
val=${defaultValue}
fi
eval $varname="$val"
}
function checkGoodEndOfPriviousCommand() {
local errorCode=$1
if [ "$?" -ne "0" ]; then
killall ssh
exit $errorCode
fi
}
function killSshTunnel() {
local port=$1
local sshPid=`ps aux | fgrep "L$port" | fgrep -v grep | awk '{print $2}'`
if [[ -n "${sshPid}" ]]; then
kill -9 ${sshPid}
fi
}
function sshProxyConnectWithRemoteServer() {
local sshProxyKey=$1
local sshProxyPort=$2
local sshProxyUser=$3
local sshProxyHost=$4
local sshServerHost=$5
local sshServerKey=$6
local sshServerUser=$7
local sshServerPath=$8
killSshTunnel $sshProxyPort
# echo ">>> Start Tunnel"
ssh -v -o StrictHostKeyChecking=no -i $sshProxyKey -L$sshProxyPort:$sshServerHost:22 -N -f $sshProxyUser@$sshProxyHost
checkGoodEndOfPriviousCommand 1
# echo ">>> Test Connection"
ssh -v -p $sshProxyPort -o StrictHostKeyChecking=no -i $sshServerKey $sshServerUser@localhost "test -d $sshServerPath || mkdir -p $sshServerPath"
checkGoodEndOfPriviousCommand 1
}
function uploadOneFileToServer() {
local localPath=$1
local remotePath=$2
local $sshPort=$3
local $sshServerKey=$4
local $serverUser=$5
# echo ">>> Uploading $(basename ${localPath})"
scp -q -P $sshPort -i $sshServerKey $localPath $serverUser@localhost:$remotePath
checkGoodEndOfPriviousCommand 1
}
function downloadOneFileFromServer() {
local localPath=$1
local remotePath=$2
local $sshPort=$3
local $sshServerKey=$4
local $serverUser=$5
# echo ">>> Download $(basename ${localPath})"
scp -q -P $sshPort -i $sshServerKey $serverUser@localhost:$remotePath $localPath
checkGoodEndOfPriviousCommand 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment