Skip to content

Instantly share code, notes, and snippets.

@CHERTS
Last active June 6, 2018 11:07
Show Gist options
  • Save CHERTS/d05caad5db4cc22f3a3c760d25e83287 to your computer and use it in GitHub Desktop.
Save CHERTS/d05caad5db4cc22f3a3c760d25e83287 to your computer and use it in GitHub Desktop.
Implementing netcat in bash
#!/bin/bash
SERVER_ADDR=$1
SERVER_PORT=$2
_command_exists() {
type "${1}" &> /dev/null
}
_nc() {
(echo >/dev/tcp/${1}/${2}) >/dev/null 2>&1
}
NC_ON_BASH=0
if _command_exists nc ; then
NC_BIN=$(which nc)
else
if _command_exists ncat ; then
NC_BIN=$(which ncat)
else
NC_ON_BASH=1
fi
fi
if [[ ${NC_ON_BASH} -eq 1 ]]; then
_nc "${SERVER_ADDR}" "${SERVER_PORT}"
else
${NC_BIN} -z "${SERVER_ADDR}" "${SERVER_PORT}" $@ >/dev/null 2>&1
fi
if [ $? -eq 0 ]; then
echo "Connected"
else
echo "Error: ${SERVER_ADDR}:${SERVER_PORT} not listen."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment