스크립트 · 2 min read · Jan 20, 2026
다양한 포트에서 웹 서버를 원격으로 모니터링하는 간단한 Bash 스크립트
다양한 포트에서 웹 서버를 원격으로 모니터링하는 간단한 Bash 스크립트
간단한 bash 스크립트로 다양한 포트(여기서는 smtp, dns, http 및 https이지만 사용자 정의 가능)에서 웹 서버를 모니터링합니다. 이와 같은 프로그램이 100개 이상 존재하지만 메모리 사용량이 적은 것을 원했습니다. 또한, 한 번만 알림을 받고 싶었고, 알림은 제 휴대폰으로 SMS로 수신됩니다. 이전에 사용하던 소프트웨어는 문제가 해결되거나 모니터링을 중지할 수 있을 때까지 매 분마다 알림을 받았기 때문에 꽤 성가셨습니다.
소프트웨어 설치
mail, dig 및 telnet이 설치되어 있어야 합니다.
스크립트
참고: 스크립트는 다음을 수행할 때까지 계속되지 않습니다:
./whatever_you_called_this_script fix이것은 단 한 번의 알림만 받기 위해 의도적으로 설정되었습니다…
#!/bin/bash
# 원격 웹 서버의 중요한 포트를 확인하는 스크립트
# 저작권 (c) 2009 blogama.org
# 이 스크립트는 GNU GPL 버전 2.0 이상에 따라 라이센스가 부여됩니다.
# ---------------------------------------------------------------------
### 이 스크립트는 포트 25, 53, 80 및 443을 확인합니다 ###
### 2번의 실패한 확인 후 이메일 알림을 보냅니다 ###
######수정할 부분######
WORKDIR="/root"
###HTTP###
HTTPSERVERIP="192.168.1.106"
HTTPSERVERPORT="80"
##########
###HTTPS###
HTTPSSERVERIP="192.168.1.106"
HTTPSSERVERPORT="443"
##########
###MAIL###
SMTPSERVERIP="192.168.1.106"
SMTPSERVERPORT="25"
##########
###DNS###
DNSSERVERIP="192.168.1.106"
DOMAINTOCHECKDNS="example.com"
ANSWERIP="192.168.1.106"
#########
###NOTIFICATIONS###
EMAIL="[email protected]"
##########
######수정할 부분 끝######
######아래는 수정하지 마십시오######
### 바이너리 ###
MAIL=$(which mail)
TELNET=$(which telnet)
DIG=$(which dig)
###디렉토리 변경###
cd $WORKDIR
###문제가 해결되면 복원###
if [ $1 ]; then
if [ $1=="fix" ]; then
rm server_problem*.txt
exit 1;
fi
fi
###이미 알림을 받았는지 확인###
if [ -f server_problem.txt ]; then
exit 1;
fi
###SMTP 테스트###
(
echo "quit"
) | $TELNET $SMTPSERVERIP $SMTPSERVERPORT | grep Connected > /dev/null 2>&1
if [ "$?" -ne "1" ]; then #Ok
echo "PORT CONNECTED"
if [ -f server_problem_first_time_smtp.txt ]; then #문제가 해결되면 파일 제거
rm -rf server_problem_first_time_smtp.txt
fi
else #연결 실패
if [ -f server_problem_first_time_smtp.txt ]; then #두 번째, 아래에 알림 전송
echo "SMTP PORT NOT CONNECTING" >> server_problem.txt
rm -rf server_problem_first_time_smtp.txt
else #첫 번째 알림
> server_problem_first_time_smtp.txt
fi
fi
###HTTP 테스트###
(
echo "quit"
) | $TELNET $HTTPSERVERIP $HTTPSERVERPORT | grep Connected > /dev/null 2>&1
if [ "$?" -ne "1" ]; then #Ok
echo "PORT CONNECTED"
if [ -f server_problem_first_time_http.txt ]; then #문제가 해결되면 파일 제거
rm -rf server_problem_first_time_http.txt
fi
else #연결 실패
if [ -f server_problem_first_time_http.txt ]; then #두 번째, 아래에 알림 전송
echo "HTTP PORT NOT CONNECTING" >> server_problem.txt
rm -rf server_problem_first_time_http.txt
else #첫 번째 알림
> server_problem_first_time_http.txt
fi
fi
###HTTPS 테스트###
(
echo "quit"
) | $TELNET $HTTPSSERVERIP $HTTPSSERVERPORT | grep Connected > /dev/null 2>&1
if [ "$?" -ne "1" ]; then #Ok
echo "PORT CONNECTED"
if [ -f server_problem_first_time_https.txt ]; then #문제가 해결되면 파일 제거
rm -rf server_problem_first_time_https.txt
fi
else #연결 실패
if [ -f server_problem_first_time_https.txt ]; then #두 번째, 아래에 알림 전송
echo "HTTPS PORT NOT CONNECTING" >> server_problem.txt
rm -rf server_problem_first_time_https.txt
else #첫 번째 알림
> server_problem_first_time_https.txt
fi
fi
###DNS 테스트###
$DIG $DOMAINTOCHECKDNS @$DNSSERVERIP | grep $ANSWERIP
if [ "$?" -ne "1" ]; then #Ok
echo "PORT CONNECTED"
if [ -f server_problem_first_time_dns.txt ]; then #문제가 해결되면 파일 제거
rm -rf server_problem_first_time_dns.txt
fi
else #연결 실패
if [ -f server_problem_first_time_dns.txt ]; then #두 번째, 아래에 알림 전송
echo "DNS PORT NOT CONNECTING" >> server_problem.txt
rm -rf server_problem_first_time_dns.txt
else #첫 번째 알림
> server_problem_first_time_dns.txt
fi
fi
###2번의 실패한 확인 후 이메일 알림 전송###
if [ -f server_problem.txt ]; then
$MAIL -s "서버 문제" $EMAIL < /root/server_problem.txt
fi
실행 가능하게 만들기:
chmod +x whatever_you_called_this_script크론탭에 추가하기:
crontab -e* * * * * /path/to/whatever_you_called_this_script >/dev/null 2>&1새 게시물을 받은 편지함에서 받기
스팸은 없습니다. 언제든지 구독 해지 가능합니다.