

InterviewSolution
Saved Bookmarks
1. |
Solve : I need to create a batch file, which should give me output and send emails? |
Answer» <html><body><p>I need to create a batch file which will be containing the URLs, which should give me output and send emails for only "DOWN" <a href="https://interviewquestions.tuteehub.com/tag/status-11923" style="font-weight:bold;" target="_blank" title="Click to know more about STATUS">STATUS</a> of the URL's with specific message . Like HTTP/1.1 200ok and HTTP/1.1 503 Service unavailableDear All,<br/><br/>I need to create a batch file, which should <a href="https://interviewquestions.tuteehub.com/tag/first-461760" style="font-weight:bold;" target="_blank" title="Click to know more about FIRST">FIRST</a> 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.<br/><br/>Thanks in advance<br/>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.Yes I am making a request to <a href="/cdn-cgi/l/email-protection">[email protected]</a>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. Quote from: Santosh on April 13, 2020, 07:16:57 AM</p><blockquote>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.<br/></blockquote> Can you post what did you tried as code and some examples of URLs to test them ?Yes I have tried like this, I have a text file containing multiple URLs. EX: URL.txt containing <a href="https://www.google.com">www.google.com</a> and so on...<br/>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.Your quick response will be appreciated. Hi <br/>Since you didn't provide us any code to deal with, i come with my solution !<br/>First open your notepad and copy and paste this code below as<em> <strong>CheckURL.vbs</strong></em><br/><br/><strong>The vbscript : CheckURL.vbs</strong><br/> Code: <a>[Select]</a>Option Explicit<br/>Dim Not_OK_URLS, oARG, OK_URLS<br/>OK_URLS = "OK_URLS.txt"<br/>Not_OK_URLS = "Not_OK_URLS.txt"<br/>Set oARG=WScript.Arguments<br/>If oARG.Count=0 Then WScript.Quit<br/>wscript.echo Check(oARG(0))<br/>'---------------------------------------------------------------------------------------------------<br/>Function Check(URL)<br/> On Error Resume Next<br/> Const WHR_EnableRedirects = 6<br/> Dim h,AllResponseHeaders<br/> Set h = CreateObject("WinHttp.WinHttpRequest.5.1")<br/> h.Option(WHR_EnableRedirects) = False 'disable redirects<br/> h.Open "HEAD", URL , False<br/> h.Send()<br/> 'AllResponseHeaders = h.GetAllResponseHeaders()<br/> 'wscript.echo AllResponseHeaders <br/> If Err.number = 0 Then<br/> Select Case CInt(h.status) <br/> Case 200,201,202,204<br/> Check = h.status & vbTab & h.statusText<br/> Call WriteLog(URL & vbTab & Check & vbCrlf & String(100,"-"),OK_URLS)<br/> Case 404,401,403,412,415,500,501,500,503<br/> Check = h.status & vbTab & h.statusText<br/> Call WriteLog(URL & vbTab & Check & vbCrlf & String(100,"-"),Not_OK_URLS)<br/> Case Else <br/> Check = h.status & vbTab & h.statusText<br/> Call WriteLog(URL & vbTab & Check & vbCrlf & String(100,"-"),Not_OK_URLS)<br/> End Select<br/> Else<br/> Check = "OFFLINE" & vbCrlf &_<br/> "Error Description: " & Err.Description<br/> Call WriteLog(URL & vbTab & Check & vbCrlf & String(100,"-"),Not_OK_URLS)<br/> On Error Goto 0<br/> End IF<br/>End Function<br/>'---------------------------------------------------------------------------------------------------<br/>Sub WriteLog(strText,LogFile)<br/> Const ForAppending = 8<br/> Dim fs,ts<br/> Set fs = CreateObject("Scripting.FileSystemObject")<br/> Set ts = fs.OpenTextFile(LogFile,ForAppending,True)<br/> ts.WriteLine strText<br/> ts.Close<br/>End Sub<br/>'---------------------------------------------------------------------------------------------------<br/>And copy and paste this code too with your notepad in the same <a href="https://interviewquestions.tuteehub.com/tag/folder-246959" style="font-weight:bold;" target="_blank" title="Click to know more about FOLDER">FOLDER</a> as the vbscript above and <a href="https://interviewquestions.tuteehub.com/tag/name-237998" style="font-weight:bold;" target="_blank" title="Click to know more about NAME">NAME</a> it as <em><strong>CheckURL.bat</strong></em><br/><br/><strong>The Batch Script : CheckURL.bat</strong><br/> Code: <a>[Select]</a>echo off<br/>Title Check URLS by Hackoo 2020 & Color 0A<br/>set "URLs=URLs.txt"<br/>Set "OK_URLS=OK_URLS.txt"<br/>Set "Not_OK_URLS=Not_OK_URLS.txt"<br/>If Exist "%OK_URLS%" Del "%OK_URLS%"<br/>If Exist "%Not_OK_URLS%" Del "%Not_OK_URLS%"<br/>Call :Create_URLS_File<br/>for /f "delims=" %%a in ('Type "%URLs%"') do (<br/> echo "%%a" <br/> cscript //NoLogo CheckURL.vbs "%%a"<br/> echo ------------------------------------<br/>)<br/>TimeOut /T 5 /NoBreak>nul<br/>Start "" /MAX "%OK_URLS%"<br/>Start "" /MAX "%Not_OK_URLS%" <br/>Exit<br/>::-------------------------------------------<br/>:Create_URLS_File<br/>(<br/> echo https://www.mediafire.com<br/> echo http://www.hyperdebrid.com<br/> echo http://www.fakirdebrid.net<br/> echo http://www.keepfiles.fr<br/> echo http://www.4shared.com<br/> echo https://1fichier.com<br/> echo https://www.mega.co.nz<br/> echo http://www.mediafire.com<br/> echo https://www.uploaded.net<br/> echo https://www.oboom.com<br/> echo https://www.letitbit.net<br/> echo https://www.keep2share.cc<br/> echo https://alfafile.net<br/> echo https://www.bigfile.to<br/> echo https://www.dailymotion.com<br/> echo https://www.datafile.com<br/> echo https://www.Depfile.com<br/> echo https://www.Dropbox.com<br/> echo https://www.Extmatrix.com<br/> echo https://www.Fboom.me<br/> echo https://www.Filefactory.com<br/> echo https://www.Filesmonster.com<br/> echo https://www.Fshare.vn<br/> echo https://www.Keep2share.com<br/> echo https://www.Mega.nz<br/> echo https://www.Rapidgator.net<br/> echo https://www.Scribd.com<br/> echo https://www.Soundcloud.com<br/> echo https://www.Speedyshare.com<br/> echo https://www.Turbobit.net<br/> echo https://www.Vimeo.com<br/> )>"%URLS%"<br/> ::-------------------------------------------<br/>Now what you have to do, is just to execute the batch file I have just pasted the code in notepad and saved them.<br/>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.<br/><br/>Because I have one URLs.txt file with the required URLs in that already.<br/>And one more thing, we just need to execute the batch file right.I am getting below error:<br/>------------------------------------<br/>"<a href="https://www.Turbobit.net">https://www.Turbobit.net</a>"<br/>Input Error: Can not find script file "C:\WINDOWS\system32\CheckURL.vbs".<br/>------------------------------------<br/>"<a href="https://www.Vimeo.com">https://www.Vimeo.com</a>"<br/>Input Error: Can not find script file "C:\WINDOWS\system32\CheckURL.vbs".<br/>------------------------------------<br/><br/>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.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".<br/>Instead it should send message as a mail notification only if the URL is down with the error output.<strong></strong>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. Quote from: Santosh on April 16, 2020, 04:04:34 AM<blockquote>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.<br/></blockquote> Just be patient i will post you <a href="https://interviewquestions.tuteehub.com/tag/another-876628" style="font-weight:bold;" target="_blank" title="Click to know more about ANOTHER">ANOTHER</a> solution for testing <br/>+Here is an example that i tested on my side. ( I just replied to you here on <a href="https://stackoverflow.com/questions/61204477/output-to-send-email-notification-if-the-url-is-not-live?answertab=active#tab-top">stackoverflow</a> )<br/>You should just change the variable <em><strong>Sender</strong></em>, <em><strong>Recipient</strong></em> and <em><strong>SMTP_Server</strong></em><br/> Code: <a>[Select]</a>echo off<br/>Title Send email notification if the URL is not live with BLAT<br/>Color 0A<br/>set "URLS=%~dp0URLS.txt"<br/><br/>If Not Exist "%URLS%" (<br/> Color 0C & echo(<br/> echo You should provide "%URLS%" with this batch file "%~nx0"<br/> TimeOut /T 10 /NoBreak>nul<br/> Exit<br/>)<br/><br/>Set "Not_OK_URLS=%~dp0Not_OK_URLS.txt"<br/>Set "BLAT=%~dp0blat.exe"<br/>Set "LOG_BLAT=%~dp0LogBlat.txt"<br/><br/>If Exist "%Not_OK_URLS%" Del "%Not_OK_URLS%"<br/>If Exist "%LOG_BLAT%" Del "%LOG_BLAT%"<br/><br/>Setlocal EnableDelayedExpansion<br/>for /f "delims=" %%a in ('Type "%URLS%"') do (<br/> Call :StringFormat "%%a" URL<br/> (ping -n 1 "!URL!" | findstr /r /c:"[0-9] *ms">nul) && (echo %%a is OnLine ==^> Success) || (echo %%a is Dead ==^> FAILURE)>>"%Not_OK_URLS%"<br/>)<br/>If Exist "%Not_OK_URLS%" Call :Mail<br/>REM If Exist "%LOG_BLAT%" Start "" "%LOG_BLAT%"<br/>Exit<br/>::*************************************************************************************<br/>:StringFormat <URL><br/>( <br/> echo Function StringReplace(Str^)<br/> echo Str = Replace(Str,"http://",""^)<br/> echo Str = Replace(Str,"https://",""^)<br/> echo StringReplace = str<br/> echo End Function<br/> echo wscript.echo StringReplace("%~1"^)<br/>)>"%tmp%\%~n0.vbs"<br/>for /f "delims=" %%a in ('Cscript /nologo "%tmp%\%~n0.vbs"') do ( set "%2=%%a" )<br/>If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"<br/>exit /b<br/>::*************************************************************************************<br/>:Mail<br/>cls<br/>Set Sender=-f <a href="/cdn-cgi/l/email-protection">[email protected]</a><br/>set Recipient=-to <a href="/cdn-cgi/l/email-protection">[email protected]</a><br/>set Subject=-s "Multi Ping URLS Tester and sending mail with BLAT"<br/>set SMTP_Server=-server smtp.changeme.com<br/>Set body=-body "App is Down, please find the attachment for the error logs"<br/>set Message=-bodyF "%Not_OK_URLS%"<br/>set Attachment=-attach "%Not_OK_URLS%"<br/>set Log=-log "%LOG_BLAT%"<br/>set Debug=-debug<br/>echo.<br/>echo Please Wait a While ... Sending Mail is in progress ......<br/>%BLAT% %Sender% %Recipient% %Subject% %Message% %SMTP_Server% %Attachment% %Log% %Debug%>nul<br/>Exit /B<br/>::*************************************************************************************</body></html> | |