Skip to content

Instantly share code, notes, and snippets.

@programminghoch10
Last active May 14, 2022 11:00
Show Gist options
  • Save programminghoch10/762b834c2265bbd7f56df03114ffa913 to your computer and use it in GitHub Desktop.
Save programminghoch10/762b834c2265bbd7f56df03114ffa913 to your computer and use it in GitHub Desktop.
scp compliant copy without encryption using netcat
#!/bin/bash
echo "Arguments: "
echo "${@}"
set -x #explain steps for debug purposes
set -e #exit when any command executed fails unexpectedly
IFS=$'\n'
#ensure our background tasks get stopped too
#trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT
trap '[ -n "$(jobs -r -p)" ] && kill $(jobs -r -p) 2>/dev/null' SIGINT SIGTERM EXIT
PORT="2020"
TIMEOUT="5" #timeout in s
HOSTNAME="$(hostname)" #hostname used for identifying this PC in the network
SSH="ssh" #ssh command for initiating connection
#SSH_PRECOMMAND="[ -z $(command -v nc) ] && exit 1; "
isremotehost() {
HOST="$1"
[[ "$HOST" != *':'* ]] && return 1
[[ "$HOST" == "/"* || "$HOST" == "./"* ]] && return 1
return 0
}
islocalfile() {
isremotehost "$@" || return 0 && return 1
}
[ $# -gt 2 ] && {
SOURCE="$1"
TARGET="${@: -1}"
$0 "$SOURCE" "$TARGET"
shift
$0 "$@"
exit $?
}
SOURCE="$1"
TARGET="$2"
islocalfile "$TARGET" && [ -f "$TARGET" ] && echo "Automatically overwriting $TARGET"
islocalfile "$SOURCE" && islocalfile "$TARGET" && {
echo "Both files are local, copying normally"
cp -f "$SOURCE" "$TARGET"
exit $?
}
SOURCE_FILESPEC="$SOURCE"
isremotehost "$SOURCE" && {
SOURCE_FILESPEC="${SOURCE##*:}"
SOURCE_HOST="${SOURCE%:*}"
}
TARGET_FILESPEC="$TARGET"
isremotehost "$TARGET" && {
TARGET_FILESPEC="${TARGET##*:}"
[ -z "$TARGET_FILESPEC" ] && TARGET_FILESPEC="."
TARGET_HOST="${TARGET%:*}"
}
SEND_HOST="$HOSTNAME"
isremotehost "$SOURCE" && {
SEND_HOST="${SOURCE_HOST##*@}"
SEND_HOST_SSH="$SOURCE_HOST"
}
RECV_HOST="$HOSTNAME"
isremotehost "$TARGET" && {
RECV_HOST="${TARGET_HOST##*@}"
RECV_HOST_SSH="$TARGET_HOST"
}
NC_TAR_SOURCE=("cd" "$(dirname "$SOURCE_FILESPEC")" "&&" "tar" "cf" "-" "$(basename "$SOURCE_FILESPEC")")
NC_UNTAR_TARGET=("tar" "xfv" "-" "-C" "$(dirname "$TARGET_FILESPEC")")
declare -p NC_TAR_SOURCE
declare -p NC_UNTAR_TARGET
NC_SEND=("${NC_TAR_SOURCE[@]}" "|" "nc" "-w" "$TIMEOUT" "$RECV_HOST" "$PORT")
NC_RECV=("nc" "-l" "-p" "$PORT" "|" "${NC_UNTAR_TARGET[@]}")
declare -p NC_SEND
declare -p NC_RECV
if [ "$RECV_HOST" = "$HOSTNAME" ]; then
eval "${NC_RECV[@]}" "&"
else
$SSH "$RECV_HOST_SSH" "${NC_RECV[@]}" &
fi
sleep 5
if [ "$SEND_HOST" = "$HOSTNAME" ]; then
eval "${NC_SEND[@]}" "&"
else
$SSH "$SEND_HOST_SSH" "${NC_SEND[@]}" &
fi
wait
@programminghoch10
Copy link
Author

NCCP

A scp command line compliant copy tool for copying files without encryption using netcat and initiating the connection using ssh.

Install

Download the script and run it.
You can save it into your home folder and add

alias nccp="bash ~/nccp.sh"

to your .bash_aliases for easy access.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment