Page 1 of 1

[SOLVED] Read grep's output and send email?

Posted: 24 Sep 2022 10:38
by Shohreh
Hello,

I need to check for a pattern in a web page, and send an e-mail depending on the output from grep which can be either 0/1 depending on if the pattern is found.

Does someone know of
1. a way to do this in CMD
2. a single EXE email app to send e-mail through my ISP's SMTP server?

Code: Select all

wget -qO - https://www.acme.com | grep -c "Something" | <magic>
Thank you.

Re: Read grep's output and send email?

Posted: 24 Sep 2022 11:30
by batmanbatmanbatmen
curl https://www.acme.com |find "something"

Re: Read grep's output and send email?

Posted: 24 Sep 2022 11:35
by Shohreh
Thanks, although curl's output is full of HTML while grep only returns 0/1.

Code: Select all

@echo off
echo Check something
wget -qO - https://www.acme.com | grep -c "something"
The next step is to 1) check the result, and if it's 1, send an email (eg. mail.exe -s "Found" -to me@isp.com smtp.isp.com).

Re: Read grep's output and send email?

Posted: 24 Sep 2022 12:36
by batmanbatmanbatmen
curl https://www.acme.com |find "something"
if %errorlevel% == 0 call :Email2Adm
goto :eof

:Email2Adm
echo "Something find"
cscript sendm.vbs
goto :eof



Send email can use vbs.
For your reference:
=================
File name: sendm.vbs

Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).

Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM

Set objArgs = Wscript.Arguments

' FmMail = objArgs(0)
' Tomail = objArgs(1)


Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Email Subject"
objMessage.From = norelply@isp.com
objMessage.To = me@isp.com

objMessage.TextBody = "Text body"


'==This section provides the configuration information for the remote SMTP server.

Set objConfig = objMessage.Configuration

objConfig.Fields("http://schemas.microsoft.com/cdo/config ... /sendusing") = 2

'Name or IP of Remote SMTP Server
objConfig.Fields("http://schemas.microsoft.com/cdo/config ... smtpserver") = "smtp.isp.com"

'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objConfig.Fields("http://schemas.microsoft.com/cdo/config ... thenticate") = cdoBasic

'Server port (typically 25)
objConfig.Fields("http://schemas.microsoft.com/cdo/config ... serverport") = 25

'Use SSL for the connection (False or True)
objConfig.Fields("http://schemas.microsoft.com/cdo/config ... smtpusessl") = False

'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objConfig.Fields("http://schemas.microsoft.com/cdo/config ... iontimeout") = 60

objConfig.Fields.Update

'==End remote SMTP server configuration section==

objMessage.Send

Re: Read grep's output and send email?

Posted: 24 Sep 2022 13:13
by Shohreh
Thanks for the tip on ERRORLEVEL.

Code: Select all

@echo off
echo Looking for Something
wget -qO - https://www.acme.com | grep -c "Something"
IF %ERRORLEVEL% NEQ 0 (echo Not found) ELSE (echo Found)
I found a simpler way to send an e-mail:

Code: Select all

mailsend.exe -t me@acme.com -f me@acme.com -smtp smtp.isp.com -sub "Test message" -M "Some test"