Avatar of atorex
atorex
 asked on

Windows script to Bash conversion

I'm convering a Windows sceript in to bash, due to my limitations in bash scripting I need some help in converting the following. the below is checking if Tomcat Web server is running and if not wayting till it is, I could do netstat and grep port 8080 but cant get the rest of the loop.

this is the windows script

  :: Test to see if WebServer is running before starting PA.
  :startWebServerStat
  netstat -na | find "0.0.0.0:8080" > WebServerStat.out

  set WebServerStat=
  for /f %%e in (WebServerStat.out) do set WebServerStat=%%e
  del /q/f WebServerStat.out

  if not defined WebServerStat (
    echo.
    echo Webserver is starting please wait...
    echo Waiting 5 seconds before next check.
    for /l %%r in (1,1,5) do @ping 127.0.0.1 -n 2 -w 1000 > nul
    goto startWebServerStat

I would apreciate any help,
Thanks.
Shell ScriptingLinux

Avatar of undefined
Last Comment
atorex

8/22/2022 - Mon
Deepak Vasudevan

Ping will not check the services. Check out

#!/bin/bash
# Script to check important ports on remote webserver
# Copyright (c) 2009 blogama.org
# This script is licensed under GNU GPL version 2.0 or above
# ---------------------------------------------------------------------
 
### This script does a verification on port 25, 53, 80 and 443 ###
### After 2 failed check it will send a mail notification ###
 
######To be modified######
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="admin@example.com"
##########
######End to be modified######
 
 
######Do not make modifications below######
### Binaries ###
MAIL=$(which mail)
TELNET=$(which telnet)
DIG=$(which dig)
###Change dir###
cd $WORKDIR
###Restore when problem fix###
if [ $1 ]; then
  if [ $1=="fix" ]; then
    rm server_problem*.txt
	exit 1;
  fi
fi
###Check if already notified###
if [ -f server_problem.txt ]; then
  exit 1;
fi
 
###Test 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 #remove file if problem fixed
    rm -rf server_problem_first_time_smtp.txt
  fi
else #Connection failure
  if [ -f server_problem_first_time_smtp.txt ]; then #Second time, send notification below
    echo "SMTP PORT NOT CONNECTING" >> server_problem.txt
	rm -rf server_problem_first_time_smtp.txt
  else #First notification
    > server_problem_first_time_smtp.txt
  fi
fi
 
###Test 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 #remove file if problem fixed
    rm -rf server_problem_first_time_http.txt
  fi
else #Connection failure
  if [ -f server_problem_first_time_http.txt ]; then #Second time, send notification below
    echo "HTTP PORT NOT CONNECTING" >> server_problem.txt
	rm -rf server_problem_first_time_http.txt
  else #First notification
    > server_problem_first_time_http.txt
  fi
fi
 
###Test 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 #remove file if problem fixed
    rm -rf server_problem_first_time_https.txt
  fi
else #Connection failure
  if [ -f server_problem_first_time_https.txt ]; then #Second time, send notification below
    echo "HTTPS PORT NOT CONNECTING" >> server_problem.txt
	rm -rf server_problem_first_time_https.txt
  else #First notification
    > server_problem_first_time_https.txt
  fi
fi
 
 
 
###Test DNS###
$DIG $DOMAINTOCHECKDNS @$DNSSERVERIP | grep $ANSWERIP
 
if [ "$?" -ne "1" ]; then #Ok
  echo "PORT CONNECTED"
  if [ -f server_problem_first_time_dns.txt ]; then #remove file if problem fixed
    rm -rf server_problem_first_time_dns.txt
  fi
else #Connection failure
  if [ -f server_problem_first_time_dns.txt ]; then #Second time, send notification below
    echo "DNS PORT NOT CONNECTING" >> server_problem.txt
	rm -rf server_problem_first_time_dns.txt
  else #First notification
    > server_problem_first_time_dns.txt
  fi
fi
 
###Send mail notification after 2 failed check###
if [ -f server_problem.txt ]; then
  $MAIL -s "Server problem" $EMAIL < /root/server_problem.txt
fi

Open in new window

Code Courtesy: http://www.howtoforge.com/simple-bash-script-to-monitor-your-webserver-remotely-on-different-ports
ASKER CERTIFIED SOLUTION
Tintin

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
atorex

ASKER
Thanks for the help, as always!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23