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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Shohreh
Posts: 33
Joined: 26 Feb 2020 08:05

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

#1 Post by Shohreh » 24 Sep 2022 10:38

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.
Last edited by Shohreh on 24 Sep 2022 13:15, edited 1 time in total.

batmanbatmanbatmen
Posts: 9
Joined: 23 Sep 2022 22:13

Re: Read grep's output and send email?

#2 Post by batmanbatmanbatmen » 24 Sep 2022 11:30

curl https://www.acme.com |find "something"

Shohreh
Posts: 33
Joined: 26 Feb 2020 08:05

Re: Read grep's output and send email?

#3 Post by Shohreh » 24 Sep 2022 11:35

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).

batmanbatmanbatmen
Posts: 9
Joined: 23 Sep 2022 22:13

Re: Read grep's output and send email?

#4 Post by batmanbatmanbatmen » 24 Sep 2022 12:36

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

Shohreh
Posts: 33
Joined: 26 Feb 2020 08:05

Re: Read grep's output and send email?

#5 Post by Shohreh » 24 Sep 2022 13:13

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"

Post Reply