Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: I need to create a batch file, which should give me output and send emails  (Read 61280 times)

0 Members and 1 Guest are viewing this topic.

Santosh

    Topic Starter


    Rookie

    • Experience: Beginner
    • OS: Windows 10
    I need to create a batch file which will be containing the URLs, which should give me output and send emails for only "DOWN" status of the URL's with specific message . Like HTTP/1.1 200ok and HTTP/1.1 503 Service unavailable

    Santosh

      Topic Starter


      Rookie

      • Experience: Beginner
      • OS: Windows 10
      Dear All,

      I need to create a batch file, which should first ping CURL to the different api endpoint URL address, if it gets HTTP bad response from any of them, it should send mail with specific message. Your swift response would be highly appreciated.

      Thanks in advance

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Either your or somebody made a similar request. The forum does not help with jibs that might be in violation of rules. Mass e-mail is considered as a job that might break rules.

      Santosh

        Topic Starter


        Rookie

        • Experience: Beginner
        • OS: Windows 10
        Yes I am making a request to [email protected].

        Santosh

          Topic Starter


          Rookie

          • Experience: Beginner
          • OS: Windows 10
          I have a text file containing multiple API URLs. Now I want to call them one by one in our batch file and execute and send notification via BLAT if it is not pinging. Your response will be so helpfull. PLease help us in resolving this.

          Hackoo



            Hopeful
          • Thanked: 42
          • Experience: Expert
          • OS: Windows 10
          I have a text file containing multiple API URLs. Now I want to call them one by one in our batch file and execute and send notification via BLAT if it is not pinging. Your response will be so helpfull. PLease help us in resolving this.
          Can you post what did you tried as code and some examples of URLs to test them ?

          Santosh

            Topic Starter


            Rookie

            • Experience: Beginner
            • OS: Windows 10
            Yes I have tried like this, I have a text file containing multiple URLs. EX: URL.txt containing www.google.com and so on...
            Now from my batch file I want to call them one by one sequentially, curl -i  %%I in URL.txt, and the output should be redirected to a text file only if it is not pingable.

            Santosh

              Topic Starter


              Rookie

              • Experience: Beginner
              • OS: Windows 10
              Your quick response will be appreciated.

              Hackoo



                Hopeful
              • Thanked: 42
              • Experience: Expert
              • OS: Windows 10
              Hi ;)
              Since you didn't provide us any code to deal with, i come with my solution !
              First open your notepad and copy and paste this code below as CheckURL.vbs

              The vbscript : CheckURL.vbs
              Code: [Select]
              Option Explicit
              Dim Not_OK_URLS, oARG, OK_URLS
              OK_URLS = "OK_URLS.txt"
              Not_OK_URLS = "Not_OK_URLS.txt"
              Set oARG=WScript.Arguments
              If oARG.Count=0 Then WScript.Quit
              wscript.echo Check(oARG(0))
              '---------------------------------------------------------------------------------------------------
              Function Check(URL)
              On Error Resume Next
              Const WHR_EnableRedirects = 6
              Dim h,AllResponseHeaders
              Set h = CreateObject("WinHttp.WinHttpRequest.5.1")
              h.Option(WHR_EnableRedirects) = False 'disable redirects
              h.Open "HEAD", URL , False
              h.Send()
              'AllResponseHeaders = h.GetAllResponseHeaders()
              'wscript.echo AllResponseHeaders
              If Err.number = 0 Then
              Select Case CInt(h.status)
              Case 200,201,202,204
              Check = h.status & vbTab & h.statusText
              Call WriteLog(URL & vbTab & Check & vbCrlf & String(100,"-"),OK_URLS)
              Case 404,401,403,412,415,500,501,500,503
              Check = h.status & vbTab & h.statusText
              Call WriteLog(URL & vbTab & Check & vbCrlf & String(100,"-"),Not_OK_URLS)
              Case Else
              Check = h.status & vbTab & h.statusText
              Call WriteLog(URL & vbTab & Check & vbCrlf & String(100,"-"),Not_OK_URLS)
              End Select
              Else
              Check = "OFFLINE" & vbCrlf &_
              "Error Description: " & Err.Description
              Call WriteLog(URL & vbTab & Check & vbCrlf & String(100,"-"),Not_OK_URLS)
              On Error Goto 0
              End IF
              End Function
              '---------------------------------------------------------------------------------------------------
              Sub WriteLog(strText,LogFile)
              Const ForAppending = 8
              Dim fs,ts
              Set fs = CreateObject("Scripting.FileSystemObject")
              Set ts = fs.OpenTextFile(LogFile,ForAppending,True)
              ts.WriteLine strText
              ts.Close
              End Sub
              '---------------------------------------------------------------------------------------------------

              And copy and paste this code too with your notepad in the same folder as the vbscript above and name it as CheckURL.bat

              The Batch Script : CheckURL.bat
              Code: [Select]
              @echo off
              Title Check URLS by Hackoo 2020 & Color 0A
              set "URLs=URLs.txt"
              Set "OK_URLS=OK_URLS.txt"
              Set "Not_OK_URLS=Not_OK_URLS.txt"
              If Exist "%OK_URLS%" Del "%OK_URLS%"
              If Exist "%Not_OK_URLS%" Del "%Not_OK_URLS%"
              Call :Create_URLS_File
              @for /f "delims=" %%a in ('Type "%URLs%"') do (
              echo "%%a"
              cscript //NoLogo CheckURL.vbs "%%a"
              echo ------------------------------------
              )
              TimeOut /T 5 /NoBreak>nul
              Start "" /MAX "%OK_URLS%"
              Start "" /MAX "%Not_OK_URLS%"
              Exit
              ::-------------------------------------------
              :Create_URLS_File
              (
                  echo https://www.mediafire.com
                  echo http://www.hyperdebrid.com
                  echo http://www.fakirdebrid.net
                  echo http://www.keepfiles.fr
                  echo http://www.4shared.com
                  echo https://1fichier.com
                  echo https://www.mega.co.nz
                  echo http://www.mediafire.com
                  echo https://www.uploaded.net
                  echo https://www.oboom.com
                  echo https://www.letitbit.net
                  echo https://www.keep2share.cc
                  echo https://alfafile.net
                  echo https://www.bigfile.to
                  echo https://www.dailymotion.com
                  echo https://www.datafile.com
                  echo https://www.Depfile.com
                  echo https://www.Dropbox.com
                  echo https://www.Extmatrix.com
                  echo https://www.Fboom.me
                  echo https://www.Filefactory.com
                  echo https://www.Filesmonster.com
                  echo https://www.Fshare.vn
                  echo https://www.Keep2share.com
                  echo https://www.Mega.nz
                  echo https://www.Rapidgator.net
                  echo https://www.Scribd.com
                  echo https://www.Soundcloud.com
                  echo https://www.Speedyshare.com
                  echo https://www.Turbobit.net
                  echo https://www.Vimeo.com
               )>"%URLS%"
               ::-------------------------------------------

              Now what you have to do, is just to execute the batch file  ;D

              Santosh

                Topic Starter


                Rookie

                • Experience: Beginner
                • OS: Windows 10
                I have just pasted the code in notepad and saved them.
                Tried to execute but it is giving me error as cannot find Ok_URLs.txt file. So I didnt understood that. Can you please explain.

                Because I have one URLs.txt file with the required URLs in that already.
                And one more thing, we just need to execute the batch file right.

                Santosh

                  Topic Starter


                  Rookie

                  • Experience: Beginner
                  • OS: Windows 10
                  I am getting below error:
                  ------------------------------------
                  "https://www.Turbobit.net"
                  Input Error: Can not find script file "C:\WINDOWS\system32\CheckURL.vbs".
                  ------------------------------------
                  "https://www.Vimeo.com"
                  Input Error: Can not find script file "C:\WINDOWS\system32\CheckURL.vbs".
                  ------------------------------------

                  And along with this it is giving me error as Ok_URL.txt and NotOk_URL.txt not found. Please check the attachment of error.

                  Santosh

                    Topic Starter


                    Rookie

                    • Experience: Beginner
                    • OS: Windows 10
                    Ok Now I am able to successfully execute the script. But after the successfull execution it is just creating text files for "Ok URLs and NotOk URLs".
                    Instead it should send message as a mail notification only if the URL is down with the error output.

                    Santosh

                      Topic Starter


                      Rookie

                      • Experience: Beginner
                      • OS: Windows 10
                      Your response will be appreciated. As your code works fine, small change is required that instead of saving the output to a file, it should send a mail notifiaction using BLAT with the error logs. I have Blat configuration ready only thing is I am confused where I can include in the above code to get the updates only when server is down and not available.

                      Hackoo



                        Hopeful
                      • Thanked: 42
                      • Experience: Expert
                      • OS: Windows 10
                      Your response will be appreciated. As your code works fine, small change is required that instead of saving the output to a file, it should send a mail notifiaction using BLAT with the error logs. I have Blat configuration ready only thing is I am confused where I can include in the above code to get the updates only when server is down and not available.
                      Just be patient i will post you another solution for testing  ;) :)
                      @+

                      Hackoo



                        Hopeful
                      • Thanked: 42
                      • Experience: Expert
                      • OS: Windows 10
                      Here is an example that i tested on my side. ( I just replied to you here on stackoverflow )
                      You should just change the variable SenderRecipient and SMTP_Server
                      Code: [Select]
                      @echo off
                      Title Send email notification if the URL is not live with BLAT
                      Color 0A
                      set "URLS=%~dp0URLS.txt"

                      If Not Exist "%URLS%" (
                      Color 0C & echo(
                      echo You should provide "%URLS%" with this batch file "%~nx0"
                      TimeOut /T 10 /NoBreak>nul
                      Exit
                      )

                      Set "Not_OK_URLS=%~dp0Not_OK_URLS.txt"
                      Set "BLAT=%~dp0blat.exe"
                      Set "LOG_BLAT=%~dp0LogBlat.txt"

                      If Exist "%Not_OK_URLS%" Del "%Not_OK_URLS%"
                      If Exist "%LOG_BLAT%" Del "%LOG_BLAT%"

                      Setlocal EnableDelayedExpansion
                      @for /f "delims=" %%a in ('Type "%URLS%"') do (
                      Call :StringFormat "%%a" URL
                      (ping -n 1 "!URL!" | findstr /r /c:"[0-9] *ms">nul) && (echo %%a is OnLine ==^> Success) || (echo %%a is Dead  ==^> FAILURE)>>"%Not_OK_URLS%"
                      )
                      If Exist "%Not_OK_URLS%" Call :Mail
                      REM If Exist "%LOG_BLAT%" Start "" "%LOG_BLAT%"
                      Exit
                      ::*************************************************************************************
                      :StringFormat <URL>

                          echo Function StringReplace(Str^)
                          echo    Str = Replace(Str,"http://",""^)
                          echo    Str = Replace(Str,"https://",""^)
                          echo    StringReplace = str
                          echo End Function
                          echo wscript.echo StringReplace("%~1"^)
                      )>"%tmp%\%~n0.vbs"
                      for /f "delims=" %%a in ('Cscript /nologo "%tmp%\%~n0.vbs"') do ( set "%2=%%a" )
                      If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
                      exit /b
                      ::*************************************************************************************
                      :Mail
                      cls
                      Set Sender=-f [email protected]
                      set Recipient=-to [email protected]
                      set Subject=-s "Multi Ping URLS Tester and sending mail with BLAT"
                      set SMTP_Server=-server smtp.changeme.com
                      Set body=-body "App is Down, please find the attachment for the error logs"
                      set Message=-bodyF "%Not_OK_URLS%"
                      set Attachment=-attach "%Not_OK_URLS%"
                      set Log=-log "%LOG_BLAT%"
                      set Debug=-debug
                      echo.
                      echo             Please Wait a While ... Sending Mail is in progress ......
                      %BLAT% %Sender% %Recipient% %Subject% %Message% %SMTP_Server% %Attachment% %Log% %Debug%>nul
                      Exit /B
                      ::*************************************************************************************