mirror of https://github.com/cowrie/cowrie.git
140 lines
3.2 KiB
Bash
Executable File
140 lines
3.2 KiB
Bash
Executable File
#!/bin/bash
|
|
#Change the below to yes if you are using authbind to listen to port 22
|
|
AUTHBIND_ENABLED=no
|
|
|
|
#Change the below to yes to activate the "cowrie-env" virtual environment
|
|
VIRTUALENV_ENABLED=yes
|
|
|
|
#Change the below to -n to disable daemonizing (for instance when using supervisor)
|
|
DAEMONIZE=""
|
|
|
|
################################################################################
|
|
## don't modify below here ##
|
|
################################################################################
|
|
|
|
|
|
find_cowrie_directory() {
|
|
# Determine Cowrie directory
|
|
if [[ "$0" = /* ]]
|
|
then
|
|
COWRIEDIR=$(dirname $0)/..
|
|
else
|
|
COWRIEDIR=$(dirname $PWD/$0)/..
|
|
fi
|
|
COWRIEDIR=$(cd ${COWRIEDIR} && pwd -P 2>/dev/null || pwd)
|
|
}
|
|
|
|
activate_venv() {
|
|
# Activate Python virtual environment
|
|
VENV="$1"
|
|
if [ ! -d "$VENV" ]
|
|
then
|
|
echo "The specified virtualenv \"$VENV\" was not found!"
|
|
exit 1
|
|
fi
|
|
if [ ! -f "$VENV/bin/activate" ]
|
|
then
|
|
echo "The specified virtualenv \"$VENV\" was not found!"
|
|
exit 2
|
|
fi
|
|
echo "Activating virtualenv \"$VENV\""
|
|
. $VENV/bin/activate
|
|
}
|
|
|
|
cowrie_status() {
|
|
# Print status
|
|
PID=$(cat ${PIDFILE} 2>/dev/null || echo "")
|
|
if [ -n "$PID" ]; then
|
|
if ps -p "$PID" 2>&1 >/dev/null; then
|
|
echo "cowrie is running (PID: ${PID})."
|
|
else
|
|
echo "cowrie is not running (PID: ${PID})."
|
|
echo "Removing stale PID file"
|
|
rm -f ${PIDFILE}
|
|
fi
|
|
else
|
|
echo "cowrie is not running."
|
|
fi
|
|
}
|
|
|
|
cowrie_start() {
|
|
# Start Cowrie
|
|
COWRIEARGS="$*"
|
|
TWISTEDARGS="${DAEMONIZE} ${XARGS} --umask 0077 --pidfile ${PIDFILE}"
|
|
|
|
# For Docker log to stdout, for non-Docker log to file
|
|
if [ "$DOCKER" = "yes" ]
|
|
then
|
|
TWISTEDARGS="${TWISTEDARGS} -l -"
|
|
else
|
|
TWISTEDARGS="${TWISTEDARGS} -l log/cowrie.log"
|
|
fi
|
|
|
|
if [ "$VIRTUALENV_ENABLED" = "yes" ]
|
|
then
|
|
activate_venv "cowrie-env"
|
|
fi
|
|
|
|
echo "Starting cowrie: [twistd ${TWISTEDARGS} cowrie ${COWRIEARGS}]..."
|
|
if [ "$AUTHBIND_ENABLED" = "no" ]
|
|
then
|
|
twistd ${TWISTEDARGS} ${COWRIEARGS} cowrie
|
|
else
|
|
authbind --deep twistd ${TWISTEDARGS} ${COWRIEARGS} cowrie
|
|
fi
|
|
}
|
|
|
|
cowrie_stop () {
|
|
# Stop Cowrie
|
|
PID=$(cat ${PIDFILE} 2>/dev/null || echo "")
|
|
if [ -n "$PID" ]; then
|
|
echo "Stopping cowrie..."
|
|
if kill -TERM $PID; then
|
|
echo -n
|
|
else
|
|
echo "Removing stale PID file"
|
|
rm -f ${PIDFILE}
|
|
fi
|
|
else
|
|
echo "cowrie is not running."
|
|
fi
|
|
}
|
|
|
|
cowrie_usage() {
|
|
echo "usage: $0 <start|stop|status>"
|
|
}
|
|
|
|
################################################################################
|
|
## Main script
|
|
################################################################################
|
|
|
|
if [ "$#" = 0 ]
|
|
then
|
|
cowrie_usage
|
|
exit 1
|
|
fi
|
|
|
|
find_cowrie_directory $0
|
|
cd ${COWRIEDIR}
|
|
export PYTHONPATH=${PYTHONPATH}:${COWRIEDIR}
|
|
PIDFILE=var/run/cowrie.pid
|
|
set -e
|
|
|
|
key=$1
|
|
shift 1
|
|
case $key in
|
|
stop)
|
|
cowrie_stop $*
|
|
;;
|
|
start)
|
|
cowrie_start $*
|
|
;;
|
|
status)
|
|
cowrie_status $*
|
|
;;
|
|
*)
|
|
cowrie_usage
|
|
exit 1
|
|
;;
|
|
esac
|