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 62421 times)

0 Members and 1 Guest are viewing this topic.

Santosh

    Topic Starter


    Rookie

    • Experience: Beginner
    • OS: Windows 10
    I have checked the above code by executing it, it is executing successfully but I am not getting any mail notification for the failure or DOWN URLs.
    And please try to use "CURL -i URL" so that will get the error response and store it in file and send as attachment in a mail.

    Now it is creating NotOKURLs.txt file for the URL which is not live, but along with that if we use attachment as error logs from CURL command.

    Hoping for your quick response, please check why it is not sending the mails, I have set the smtp,sender and recipient properly.

    Hackoo



      Hopeful
    • Thanked: 42
    • Experience: Expert
    • OS: Windows 10
    I have checked the above code by executing it, it is executing successfully but I am not getting any mail notification for the failure or DOWN URLs.
    And please try to use "CURL -i URL" so that will get the error response and store it in file and send as attachment in a mail.

    Now it is creating NotOKURLs.txt file for the URL which is not live, but along with that if we use attachment as error logs from CURL command.

    Hoping for your quick response, please check why it is not sending the mails, I have set the smtp,sender and recipient properly.
    Can you PM me the LogBlat.txt created by this script in order to tell you what's going wrong on your side ?

    Santosh

      Topic Starter


      Rookie

      • Experience: Beginner
      • OS: Windows 10
      I couldnt find the LogBlat.txt file in the respective path. I think it is not creating that.
      And if possible could you please explain the code, coz I have some doubts so.

      Thanks in advance!!!

      Santosh

        Topic Starter


        Rookie

        • Experience: Beginner
        • OS: Windows 10
        Hoping for a quick response as it is like a headache now for me :(

        Hackoo



          Hopeful
        • Thanked: 42
        • Experience: Expert
        • OS: Windows 10
        Hi  ;)
        Give a try for this batch file and modify to your settings !
        Remark : You should modify your smtp of your provider and don't use the smtp of yahoo because it won't let you relay to your mail !
        Code: [Select]
        @echo off
        Title Check URLS & Color 0A
        Set "URL=%~dp0URL.txt"
        If Not Exist "%URL%" Call :Create_URL_File

        Set "Not_OK_URL=%~dp0Not_OK_URL.txt"
        If Exist "%Not_OK_URL%" Del "%Not_OK_URL%"

        :Check_URL
        @for /F "usebackq delims=" %%u in ("%URL%") do (
        curl -I -s "%%u" | find /I "200">nul 2>&1 && (echo "%%u" ==^> is OK) || (echo "%%u" ==^> Not OK)>>"%Not_OK_URL%"
        )
        If Exist "%Not_OK_URL%" Call :Send_Mail_BLAT
        Echo( Done
        Timeout /T 3 /NoBreak>nul
        exit
        ::--------------------------------------------------------------
        :Create_URL_File
        (
        echo http://dummy.restapiexample.com/api/v1/employees
        echo http://dummy.restapiexample.com/api/v1/employee/1
        echo http://dummy.restapiexample.com/api/v1/update/21
        echo https://www.mediafire.com
        echo https://www.Vimeo.com
        echo https://vimeo.com/fr/
        echo https://www.yahoo.com
        echo https://www.google.com
        )>"%URL%"
        Exit /b
        ::--------------------------------------------------------------
        :Send_Mail_BLAT
        cls
        Set BLAT=%~dp0blat.exe
        set LOG_BLAT=%~dp0LOG_BLAT.txt
        Set Sender=-f %username%@hackoo.com
        set Recipient=-to [email protected]
        set Subject=-s "Multi Ping URLS Tester and sending mail with BLAT"
        set SMTP_Server=-server smtp.provider.com
        Set body=-body "App is Down, please find the attachment for the error logs"
        set Message=-bodyF "%Not_OK_URL%"
        set Attachment=-attach "%Not_OK_URL%"
        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
        ::--------------------------------------------------------------

        Santosh

          Topic Starter


          Rookie

          • Experience: Beginner
          • OS: Windows 10
          Thanks for the batch file, it is working as expected. But it is just giving me the mail notification as (" URL " is not ok)
          My requirement is when I do
          Code: [Select]
          CURL -i "URL" It  will give me some output and that output I want to print it in mail for the particular URL is DOWN with the error logs

          Your quick response will be appreciated sir :)

          Hackoo



            Hopeful
          • Thanked: 42
          • Experience: Expert
          • OS: Windows 10
          Thanks for the batch file, it is working as expected. But it is just giving me the mail notification as (" URL " is not ok)
          My requirement is when I do
          Code: [Select]
          CURL -i "URL" It  will give me some output and that output I want to print it in mail for the particular URL is DOWN with the error logs

          Try to modify the script at this line :
          Code: [Select]
          curl -I -s "%%u" | find /I "200">nul 2>&1 && (echo "%%u" ==^> is OK) || (echo "%%u" ==^> Not OK)>>"%Not_OK_URL%"by this one :
          Code: [Select]
          curl -I -s "%%u" | find /I "200">nul 2>&1 && (echo "%%u" ==^> is OK) || (curl -i "%%u")>>"%Not_OK_URL%"

          Santosh

            Topic Starter


            Rookie

            • Experience: Beginner
            • OS: Windows 10
            Thanks alot sir :) It is working fine. But it is just giving me error logs and unable to find which URL/API is down.
            So it will be good while printing error logs before print the URL/API name and respective of the URL/API name and below error logs of that. Then it will be easy to identify which one is down.

            Hackoo



              Hopeful
            • Thanked: 42
            • Experience: Expert
            • OS: Windows 10
            Thanks alot sir :) It is working fine. But it is just giving me error logs and unable to find which URL/API is down.
            So it will be good while printing error logs before print the URL/API name and respective of the URL/API name and below error logs of that. Then it will be easy to identify which one is down.
            Did you mean that you want to get as output like that ?
            Quote
            "http://dummy.restapiexample.com/api/v1/employee/1"
            HTTP/1.1 400 Bad Request
            Access-Control-Allow-Origin: *
            Access-Control-Expose-Headers: Content-Type, X-Requested-With, X-authentication, X-client
            Cache-Control: no-cache
            Content-Type: application/json;charset=utf-8
            Date: Mon, 20 Apr 2020 13:41:55 GMT
            Expires: Thu, 19 Nov 1981 08:52:00 GMT
            Host-Header: c2hhcmVkLmJsdWVob3N0LmNvbQ==
            Pragma: no-cache
            Referrer-Policy:
            Response: 400
            Server: nginx/1.16.0
            Set-Cookie: PHPSESSID=530b13c852bb686a0b664c3b6f75b527; path=/
            Set-Cookie: ezoadgid_133674=-1; Path=/; Domain=restapiexample.com; Expires=Mon, 20 Apr 2020 14:11:54 UTC
            Set-Cookie: ezoref_133674=; Path=/; Domain=restapiexample.com; Expires=Mon, 20 Apr 2020 15:41:54 UTC
            Set-Cookie: ezoab_133674=mod68; Path=/; Domain=restapiexample.com; Expires=Mon, 20 Apr 2020 15:41:54 UTC
            Set-Cookie: active_template::133674=pub_site.1587390114; Path=/; Domain=restapiexample.com; Expires=Wed, 22 Apr 2020 13:41:54 UTC
            Vary: Accept-Encoding
            Vary: Accept-Encoding,X-APP-JSON
            X-Ezoic-Cdn: Miss
            X-Middleton-Response: 400
            X-Sol: pub_site

            "http://dummy.restapiexample.com/api/v1/update/21"
            HTTP/1.1 405 Method Not Allowed
            Access-Control-Allow-Origin: *
            Access-Control-Expose-Headers: Content-Type, X-Requested-With, X-authentication, X-client
            Allow: PUT
            Cache-Control: no-cache
            Content-Type: text/html;charset=UTF-8
            Date: Mon, 20 Apr 2020 13:41:57 GMT
            Expires: Sun, 19 Apr 2020 13:41:57 GMT
            Host-Header: c2hhcmVkLmJsdWVob3N0LmNvbQ==
            Pagespeed: off
            Pragma: no-cache
            Referrer-Policy:
            Response: 405
            Server: nginx/1.16.0
            Set-Cookie: PHPSESSID=37a6badd8b6e46a34353cedc95d7281e; path=/
            Set-Cookie: ezoadgid_133674=-1; Path=/; Domain=restapiexample.com; Expires=Mon, 20 Apr 2020 14:11:57 UTC
            Set-Cookie: ezoref_133674=; Path=/; Domain=restapiexample.com; Expires=Mon, 20 Apr 2020 15:41:57 UTC
            Set-Cookie: ezoab_133674=mod28; Path=/; Domain=restapiexample.com; Expires=Mon, 20 Apr 2020 15:41:57 UTC
            Set-Cookie: active_template::133674=pub_site.1587390117; Path=/; Domain=restapiexample.com; Expires=Wed, 22 Apr 2020 13:41:57 UTC
            Set-Cookie: ezCMPCCS=true; Path=/; Domain=restapiexample.com; Expires=Tue, 20 Apr 2021 13:41:57 GMT
            Vary: Accept-Encoding
            Vary: Accept-Encoding,X-APP-JSON
            X-Ezoic-Cdn: Miss
            X-Middleton-Response: 405
            X-Sol: pub_site

            "https://www.Vimeo.com"
            HTTP/1.1 301 Moved Permanently
            Connection: keep-alive
            Content-Length: 226
            Server: nginx
            Content-Type: text/html; charset=iso-8859-1
            Location: https://vimeo.com/
            Expires: Mon, 15 Apr 2030 19:24:58 GMT
            Via: 1.1 varnish
            Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
            X-Varnish-Cache: 1
            X-VServer: infra-webproxy-a-16
            X-Vimeo-DC: ge
            Via: 1.1 varnish
            Accept-Ranges: bytes
            Date: Mon, 20 Apr 2020 13:42:01 GMT
            Via: 1.1 varnish
            Age: 0
            X-Served-By: cache-bwi5139-BWI, cache-cdg20724-CDG
            X-Cache: MISS, MISS
            X-Cache-Hits: 0, 0
            X-Timer: S1587390122.826389,VS0,VE88
            Vary: Accept-Encoding

            Santosh

              Topic Starter


              Rookie

              • Experience: Beginner
              • OS: Windows 10
              Yes absolutely !!!

              Hackoo



                Hopeful
              • Thanked: 42
              • Experience: Expert
              • OS: Windows 10
              Yes absolutely !!!
              Try llike that instead :
              Code: [Select]
              curl -I -s "%%u" | find /I "200">nul 2>&1 && (echo "%%u" ==^> is OK) || (echo "%%u" & curl -I "%%u")>>"%Not_OK_URL%"

              Santosh

                Topic Starter


                Rookie

                • Experience: Beginner
                • OS: Windows 10
                Thanks alot. But the URL for the first error log it is coming properly on top. And from the second URL, it is just appending from the last line of error log from the first error log. So it is not coming as standard formatting. It should come properly like :

                URL name:
                Error log
                One line gap
                Second URL name:
                Error log

                Santosh

                  Topic Starter


                  Rookie

                  • Experience: Beginner
                  • OS: Windows 10
                  Your quick response will be added more value. It is almost done just to add my previous comment changes.
                  Thanks in advance!!!

                  Hackoo



                    Hopeful
                  • Thanked: 42
                  • Experience: Expert
                  • OS: Windows 10
                  Your quick response will be added more value. It is almost done just to add my previous comment changes.
                  Thanks in advance!!!
                  Show me your code and what did you get as ErrorLog and what did you exactly expect to be ?
                  EDIT : Try like this :
                  Code: [Select]
                  :Check_URL
                  @for /F "usebackq delims=" %%u in ("%URL%") do (
                  curl -I -s "%%u" | find /I "200">nul 2>&1 && (
                  echo "%%u" ==^> is OK
                  ) || (
                  echo ---------------------------------------------------
                  echo "%%u"
                  echo ---------------------------------------------------
                  curl -I -s "%%u"
                  )>>"%Not_OK_URL%"
                  )
                  « Last Edit: April 21, 2020, 01:26:04 AM by Hackoo »

                  Santosh

                    Topic Starter


                    Rookie

                    • Experience: Beginner
                    • OS: Windows 10
                    This is my code:
                    Code: [Select]
                    :Check_URL
                    @for /F "usebackq delims=" %%u in ("%URL%") do (
                     curl -I -s "%%u" | find /I "200">nul 2>&1 && (echo "%%u" ==^> is OK) || (echo "%%u" & curl -i "%%u")>>"%Not_OK_URL%"
                    )
                    If Exist "%Not_OK_URL%" blat -subject "APPLICATIONs DOWN STATUS" -body "app status is down, please check the attachment for the Application DOWN error logs" -attach C:\Not_OK_URL.txt  %Log% -to [email protected]

                    Error logs:
                    Code: [Select]
                    "http://dummy.restapiexample.com/api/v1/employee/1"
                    HTTP/1.1 400 Bad Request
                    Access-Control-Allow-Origin: *
                    Access-Control-Expose-Headers: Content-Type, X-Requested-With, X-authentication, X-client
                    Cache-Control: no-cache
                    Content-Type: application/json;charset=utf-8
                    Date: Mon, 20 Apr 2020 13:41:55 GMT
                    Expires: Thu, 19 Nov 1981 08:52:00 GMT
                    Host-Header: c2hhcmVkLmJsdWVob3N0LmNvbQ==
                    Pragma: no-cache
                    Referrer-Policy:
                    Response: 400
                    Server: nginx/1.16.0
                    Set-Cookie: PHPSESSID=530b13c852bb686a0b664c3b6f75b527; path=/
                    Set-Cookie: ezoadgid_133674=-1; Path=/; Domain=restapiexample.com; Expires=Mon, 20 Apr 2020 14:11:54 UTC
                    Set-Cookie: ezoref_133674=; Path=/; Domain=restapiexample.com; Expires=Mon, 20 Apr 2020 15:41:54 UTC
                    Set-Cookie: ezoab_133674=mod68; Path=/; Domain=restapiexample.com; Expires=Mon, 20 Apr 2020 15:41:54 UTC
                    Set-Cookie: active_template::133674=pub_site.1587390114; Path=/; Domain=restapiexample.com; Expires=Wed, 22 Apr 2020 13:41:54 UTC
                    Vary: Accept-Encoding
                    Vary: Accept-Encoding,X-APP-JSON
                    X-Ezoic-Cdn: Miss
                    X-Middleton-Response: 400
                    X-Sol: pub_site"http://dummy.restapiexample.com/api/v1/update/21"
                    HTTP/1.1 405 Method Not Allowed
                    Access-Control-Allow-Origin: *
                    Access-Control-Expose-Headers: Content-Type, X-Requested-With, X-authentication, X-client
                    Allow: PUT
                    Cache-Control: no-cache
                    Content-Type: text/html;charset=UTF-8
                    Date: Mon, 20 Apr 2020 13:41:57 GMT
                    Expires: Sun, 19 Apr 2020 13:41:57 GMT
                    Host-Header: c2hhcmVkLmJsdWVob3N0LmNvbQ==
                    Pagespeed: off

                    So in the error logs, the second URL is appending from the last line of first error log

                    Instead it should be:
                    URL name
                    Error log
                    One line gap
                    Second URL name
                    Error log


                    Thanks in advance!!!