Created
December 23, 2020 02:30
-
-
Save ijortengab/5cfd10e2036c2d730aed41858ddeaf58 to your computer and use it in GitHub Desktop.
Shell Script ini ditujukan bagi anda yang menggunakan Nginx dan mau mengecek setiap virtual host yang terdapat pada directive server_name.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# @filename: check-nginx-server-name.sh | |
# @version: 1.0 | |
# @release-date: 20201223 | |
# @author: IjorTengab <[email protected]> | |
# | |
# Shell Script ini ditujukan bagi anda yang menggunakan Nginx dan mau mengecek | |
# setiap virtual host yang terdapat pada directive server_name. | |
# | |
# Fitur: | |
# - mengecek http dan https dari tiap virtual host. | |
# - mengecek certificate SSL nyasar karena autoconfigure nginx oleh certbot | |
# | |
# Terdapat kejadian fitur autoconfigure nginx pada certbot nyasar ke | |
# server_name lain. Seperti contoh pada bagian "Masalah dan Solusi" dari | |
# artikel berjudul "Dari http ke https menggunakan Let's Encrypt dan DNS DigitalOcean" | |
# | |
# Link: http://ijortengab.id/blog/2020/10/29/dari-http-ke-https-menggunakan-lets-encrypt-dan-dns-digitalocean/#masalah-dan-solusi | |
# | |
# Script ini bertujuan untuk menge-scan tiap server_name pada tiap file | |
# configuration, dan kemudian melakukan test koneksi http dan https menggunakan | |
# curl dan melaporkan apabila ada konfigurasi nginx yang perlu diperbaiki secara | |
# manual. | |
# | |
# Cara menggunakan: | |
# | |
# - Edit variable $DIR sesuai dengan lokasi konfigurasi nginx. | |
# - Execute ./check-nginx-server-name.sh | |
# | |
# Contoh output: | |
# | |
# ``` | |
# root@server:~/gists# ./check-nginx-server-name.sh | |
# Server name systemix.id. | |
# Execute: curl -sS -v -o /dev/null http://systemix.id/ 2>&1 | |
# HTTP/1.1 301 Moved Permanently | |
# Execute: curl -sS -v -o /dev/null https://systemix.id/ 2>&1 | |
# Error. curl: (51) SSL: no alternative certificate subject name matches target host name 'systemix.id' | |
# SSL Certificate for host name: ui.systemix.id. | |
# Error occured. Find files contains systemix.id. Execute: grep -R -l -E "server_name.*[^.]systemix.id" /etc/nginx/sites-enabled | |
# /etc/nginx/sites-enabled/systemix.id | |
# Server name *.systemix.id. | |
# Execute: curl -sS -v -o /dev/null http://www.systemix.id/ 2>&1 | |
# HTTP/1.1 301 Moved Permanently | |
# Execute: curl -sS -v -o /dev/null https://www.systemix.id/ 2>&1 | |
# HTTP/1.1 200 OK | |
# Server name ui.web.id. | |
# Execute: curl -sS -v -o /dev/null http://ui.web.id/ 2>&1 | |
# HTTP/1.1 301 Moved Permanently | |
# Execute: curl -sS -v -o /dev/null https://ui.web.id/ 2>&1 | |
# HTTP/1.1 200 OK | |
# Server name *.ui.web.id. | |
# Execute: curl -sS -v -o /dev/null http://www.ui.web.id/ 2>&1 | |
# Error. curl: (6) Could not resolve host: www.ui.web.id | |
# Execute: curl -sS -v -o /dev/null https://www.ui.web.id/ 2>&1 | |
# Error. curl: (6) Could not resolve host: www.ui.web.id | |
# Error occured. Find files contains *.ui.web.id. Execute: grep -R -l -E "server_name.*\*\.ui.web.id" /etc/nginx/sites-enabled | |
# /etc/nginx/sites-enabled/ui.web.id | |
# Server name client1.systemix.id. | |
# Execute: curl -sS -v -o /dev/null http://client1.systemix.id/ 2>&1 | |
# HTTP/1.1 301 Moved Permanently | |
# Execute: curl -sS -v -o /dev/null https://client1.systemix.id/ 2>&1 | |
# HTTP/1.1 200 OK | |
# Server name *.client1.systemix.id. | |
# Execute: curl -sS -v -o /dev/null http://www.client1.systemix.id/ 2>&1 | |
# HTTP/1.1 301 Moved Permanently | |
# Execute: curl -sS -v -o /dev/null https://www.client1.systemix.id/ 2>&1 | |
# HTTP/1.1 200 OK | |
# Server name ui.systemix.id. | |
# Execute: curl -sS -v -o /dev/null http://ui.systemix.id/ 2>&1 | |
# HTTP/1.1 301 Moved Permanently | |
# Execute: curl -sS -v -o /dev/null https://ui.systemix.id/ 2>&1 | |
# HTTP/1.1 200 OK | |
# Server name sirip-2014.ui.systemix.id. | |
# Execute: curl -sS -v -o /dev/null http://sirip-2014.ui.systemix.id/ 2>&1 | |
# HTTP/1.1 301 Moved Permanently | |
# Execute: curl -sS -v -o /dev/null https://sirip-2014.ui.systemix.id/ 2>&1 | |
# HTTP/1.1 502 Bad Gateway | |
# Server name *.ui.systemix.id. | |
# Execute: curl -sS -v -o /dev/null http://www.ui.systemix.id/ 2>&1 | |
# HTTP/1.1 301 Moved Permanently | |
# Execute: curl -sS -v -o /dev/null https://www.ui.systemix.id/ 2>&1 | |
# Error. curl: (51) SSL: no alternative certificate subject name matches target host name 'www.ui.systemix.id' | |
# SSL Certificate for host name: systemix.id. | |
# Error occured. Find files contains *.ui.systemix.id. Execute: grep -R -l -E "server_name.*\*\.ui.systemix.id" /etc/nginx/sites-enabled | |
# /etc/nginx/sites-enabled/100-ui.systemix.id | |
# Server name cms1.client2.systemix.id. | |
# Execute: curl -sS -v -o /dev/null http://cms1.client2.systemix.id/ 2>&1 | |
# HTTP/1.1 301 Moved Permanently | |
# Execute: curl -sS -v -o /dev/null https://cms1.client2.systemix.id/ 2>&1 | |
# HTTP/1.1 200 OK | |
# Server name cms3.client2.systemix.id. | |
# Execute: curl -sS -v -o /dev/null http://cms3.client2.systemix.id/ 2>&1 | |
# HTTP/1.1 301 Moved Permanently | |
# Execute: curl -sS -v -o /dev/null https://cms3.client2.systemix.id/ 2>&1 | |
# HTTP/1.1 200 OK | |
# Server name client2.systemix.id. | |
# Execute: curl -sS -v -o /dev/null http://client2.systemix.id/ 2>&1 | |
# HTTP/1.1 301 Moved Permanently | |
# Execute: curl -sS -v -o /dev/null https://client2.systemix.id/ 2>&1 | |
# HTTP/1.1 404 Not Found | |
# Server name *.client2.systemix.id. | |
# Execute: curl -sS -v -o /dev/null http://www.client2.systemix.id/ 2>&1 | |
# HTTP/1.1 301 Moved Permanently | |
# Execute: curl -sS -v -o /dev/null https://www.client2.systemix.id/ 2>&1 | |
# HTTP/1.1 404 Not Found | |
# Server name bta.systemix.id. | |
# Execute: curl -sS -v -o /dev/null http://bta.systemix.id/ 2>&1 | |
# HTTP/1.1 301 Moved Permanently | |
# Execute: curl -sS -v -o /dev/null https://bta.systemix.id/ 2>&1 | |
# HTTP/1.1 404 Not Found | |
# Server name *.bta.systemix.id. | |
# Execute: curl -sS -v -o /dev/null http://www.bta.systemix.id/ 2>&1 | |
# HTTP/1.1 301 Moved Permanently | |
# Execute: curl -sS -v -o /dev/null https://www.bta.systemix.id/ 2>&1 | |
# HTTP/1.1 404 Not Found | |
# Server name developer.web.id. | |
# Execute: curl -sS -v -o /dev/null http://developer.web.id/ 2>&1 | |
# HTTP/1.1 301 Moved Permanently | |
# Execute: curl -sS -v -o /dev/null https://developer.web.id/ 2>&1 | |
# HTTP/1.1 200 OK | |
# Server name *.developer.web.id. | |
# Execute: curl -sS -v -o /dev/null http://www.developer.web.id/ 2>&1 | |
# HTTP/1.1 404 Not Found | |
# Execute: curl -sS -v -o /dev/null https://www.developer.web.id/ 2>&1 | |
# HTTP/1.1 200 OK | |
# Server name ijortengab.id. | |
# Execute: curl -sS -v -o /dev/null http://ijortengab.id/ 2>&1 | |
# HTTP/1.1 200 OK | |
# Execute: curl -sS -v -o /dev/null https://ijortengab.id/ 2>&1 | |
# HTTP/1.1 200 OK | |
# Server name biography.id. | |
# Execute: curl -sS -v -o /dev/null http://biography.id/ 2>&1 | |
# HTTP/1.1 200 OK | |
# Execute: curl -sS -v -o /dev/null https://biography.id/ 2>&1 | |
# Error. curl: (51) SSL: no alternative certificate subject name matches target host name 'biography.id' | |
# SSL Certificate for host name: ui.systemix.id. | |
# Error occured. Find files contains biography.id. Execute: grep -R -l -E "server_name.*[^.]biography.id" /etc/nginx/sites-enabled | |
# /etc/nginx/sites-enabled/biography.id | |
# Server name *.biography.id. | |
# Execute: curl -sS -v -o /dev/null http://www.biography.id/ 2>&1 | |
# HTTP/1.1 200 OK | |
# Execute: curl -sS -v -o /dev/null https://www.biography.id/ 2>&1 | |
# Error. curl: (51) SSL: no alternative certificate subject name matches target host name 'www.biography.id' | |
# SSL Certificate for host name: ui.systemix.id. | |
# Error occured. Find files contains *.biography.id. Execute: grep -R -l -E "server_name.*\*\.biography.id" /etc/nginx/sites-enabled | |
# /etc/nginx/sites-enabled/biography.id | |
# Server name jagomatematika.com. | |
# Execute: curl -sS -v -o /dev/null http://jagomatematika.com/ 2>&1 | |
# HTTP/1.1 301 Moved Permanently | |
# Execute: curl -sS -v -o /dev/null https://jagomatematika.com/ 2>&1 | |
# HTTP/1.1 200 OK | |
# Server name *.jagomatematika.com. | |
# Execute: curl -sS -v -o /dev/null http://www.jagomatematika.com/ 2>&1 | |
# HTTP/1.1 301 Moved Permanently | |
# Execute: curl -sS -v -o /dev/null https://www.jagomatematika.com/ 2>&1 | |
# HTTP/1.1 200 OK | |
# Server name bank.ijortengab.id. | |
# Execute: curl -sS -v -o /dev/null http://bank.ijortengab.id/ 2>&1 | |
# HTTP/1.1 200 OK | |
# Execute: curl -sS -v -o /dev/null https://bank.ijortengab.id/ 2>&1 | |
# Error. curl: (51) SSL: no alternative certificate subject name matches target host name 'bank.ijortengab.id' | |
# SSL Certificate for host name: ui.systemix.id. | |
# Error occured. Find files contains bank.ijortengab.id. Execute: grep -R -l -E "server_name.*[^.]bank.ijortengab.id" /etc/nginx/sites-enabled | |
# /etc/nginx/sites-enabled/bank.ijortengab.id | |
# ``` | |
# | |
# Dari contoh diatas terlihat bahwa server_name `systemix.id` menggunakan ssl | |
# certificate untuk server_name `ui.systemix.id`. | |
# Variable. | |
# Nginx direktori. | |
DIR=/etc/nginx/sites-enabled | |
# Define. | |
normal="$(tput sgr0)" | |
red="$(tput setaf 1)" | |
yellow="$(tput setaf 3)" | |
magenta="$(tput setaf 5)" | |
gray="$(tput setaf 243)" | |
green="$(tput setaf 2)" | |
# Functions | |
inArray () { | |
local e match="$1" | |
shift | |
for e; do [[ "$e" == "$match" ]] && return 0; done | |
return 1 | |
} | |
lines=$(grep --no-filename -R -E "^\s*server_name" "$DIR" | sed "s/server_name//g" | sed "s/;//g") | |
lines=($lines) | |
server_names=() | |
for line in "${lines[@]}" | |
do | |
if inArray $line "${server_names[@]}";then | |
continue | |
elif [[ $line == "_" ]];then | |
continue | |
else | |
server_names+=($line) | |
fi | |
done | |
for server_name in "${server_names[@]}" | |
do | |
# Replace asterix (*) with `www`. | |
host=$(echo $server_name| sed 's/*/www/') | |
printf "Server name ${yellow}$server_name${normal}.\n" | |
# HTTP | |
printf " Execute: ${gray}curl -sS -v -o /dev/null http://"$host'/ 2>&1'"${normal}\n" | |
output=$(curl -sS -v -o /dev/null http://$host/ 2>&1) | |
# echo "$output" | |
ok200=$(echo "$output" | grep '< HTTP/.*\s200\s.*') | |
httpcode=$(echo "$output" | grep -E '< HTTP/.*\s\S+\s.*' | sed 's/< //') | |
if [[ ! $ok200 == "" ]];then | |
printf " ${green}$httpcode${normal}\n" | |
elif [[ ! $httpcode == "" ]];then | |
echo " "$httpcode | |
fi | |
curlerror=$(echo "$output" | grep 'curl: ') | |
if [[ ! $curlerror == "" ]];then | |
error=1 | |
printf " ${red}Error. ${normal}""$curlerror""\n" | |
fi | |
# Reset | |
unset output | |
unset ok200 | |
unset httpcode | |
unset curlerror | |
# HTTPS | |
printf " Execute: ${gray}curl -sS -v -o /dev/null https://"$host'/ 2>&1'"${normal}\n" | |
output=$(curl -sS -v -o /dev/null https://$host/ 2>&1) | |
# echo "$output" | |
ok200=$(echo "$output" | grep '< HTTP/.*\s200\s.*') | |
httpcode=$(echo "$output" | grep -E '< HTTP/.*\s\S+\s.*' | sed 's/< //') | |
if [[ ! $ok200 == "" ]];then | |
printf " ${green}$httpcode${normal}\n" | |
elif [[ ! $httpcode == "" ]];then | |
echo " "$httpcode | |
fi | |
curlerror=$(echo "$output" | grep 'curl: ') | |
curlerror51=$(echo "$output" | grep 'curl: (51)') | |
if [[ ! $curlerror == "" ]];then | |
printf " ${red}Error. ${normal}""$curlerror""\n" | |
error=1 | |
fi | |
if [[ ! $curlerror51 == "" ]];then | |
host_in_cert=$(echo "$output" | grep -E 'subject:.*' | awk '{print $3}' | sed 's/CN=//') | |
printf " SSL Certificate for host name: ${yellow}"$host_in_cert"${normal}.\n" | |
fi | |
wildcard=$(echo $server_name | grep -E '^\*') | |
if [[ $error == 1 && ! $wildcard == "" ]]; then | |
_server_name=$(echo $server_name | sed 's/^*.//') | |
printf " Error occured. Find files contains $server_name. Execute: ${gray}grep -R -l -E \"server_name.*\\*\\.$_server_name\" ""$DIR""${normal}\n" | |
files=$(grep -R -l -E "server_name.*\*\."$_server_name "$DIR") | |
files=($files) | |
for file in "${files[@]}" | |
do | |
printf " ${magenta}$file${normal}\n" | |
done | |
fi | |
if [[ $error == 1 && $wildcard == "" ]]; then | |
_server_name=$(echo $server_name | sed 's/^*.//') | |
printf " Error occured. Find files contains $server_name. Execute: ${gray}grep -R -l -E \"server_name.*[^.]$_server_name\" ""$DIR""${normal}\n" | |
files=$(grep -R -l -E "server_name.*[^.]"$server_name "$DIR") | |
files=($files) | |
for file in "${files[@]}" | |
do | |
printf " ${magenta}$file${normal}\n" | |
done | |
fi | |
# Reset | |
unset output | |
unset ok200 | |
unset httpcode | |
unset curlerror | |
unset curlerror51 | |
unset error | |
unset wildcard | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment