Skip to content

Instantly share code, notes, and snippets.

@AlbinoDrought
Created March 28, 2019 21:25
Show Gist options
  • Save AlbinoDrought/0a04a03ab2562aa83d5deaa89c6b71e8 to your computer and use it in GitHub Desktop.
Save AlbinoDrought/0a04a03ab2562aa83d5deaa89c6b71e8 to your computer and use it in GitHub Desktop.
The most terrifying healthcheck for an SMB server
#!/bin/sh
if [ -z "$2" ]; then
echo "usage: [host] [share] [protocol?=smb2] [port?=8080]"
echo "example: localhost bar"
exit 1
fi
HOST=$1
SHARE=$2
PROTOCOL=${3:-smb2}
PORT=${4:-8080}
ROOT=//$HOST/$SHARE
Check () {
echo "this is an smb healthcheck file" > test.foo
# make remote dir
echo "Making remote dir"
smbclient -N $ROOT -m $PROTOCOL -c "mkdir .smb-healthcheck" || return 1
# upload file
echo "Uploading"
smbclient -N $ROOT -m $PROTOCOL --directory .smb-healthcheck -c "put test.foo" || return 2
# download file
echo "Downloading"
smbclient -N $ROOT -m $PROTOCOL --directory .smb-healthcheck -c "get test.foo test.pulled" || return 3
echo "Comparing"
if ! cmp -s test.foo test.pulled; then
echo "downloaded file does not match"
return 4
fi
rm test.pulled
# delete uploaded file
echo "Deleting uploaded file"
smbclient -N $ROOT -m $PROTOCOL --directory .smb-healthcheck -c "rm test.foo" || return 5
# delete created directory
echo "Deleting created directory"
smbclient -N $ROOT -m $PROTOCOL -c "rmdir .smb-healthcheck" || return 6
# ensure directory is deleted
echo "Checking if directory deleted"
(smbclient -N $ROOT -m $PROTOCOL -c "cd .smb-healthcheck" && return 7) || return 0
echo "everything ok"
}
GOOD_RESPONSE="HTTP/1.1 200 OK\r\nConnection: keep-alive\r\n\r\nOK\r\n"
BAD_RESPONSE="HTTP/1.1 500 OK\r\nConnection: keep-alive\r\n\r\nNOT OK\r\n"
while true
do
Check
if [ $? -eq 0 ]; then
RESPONSE="$GOOD_RESPONSE"
else
RESPONSE="$BAD_RESPONSE"
fi
echo -en "$RESPONSE" | nc -l "$PORT"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment