It is currently April 28th, 2024, 11:04 am

[SOLVED] Need help with RunCommand to ping a computer in home LAN

Get help with creating, editing & fixing problems with skins
User avatar
nmdelrio
Posts: 74
Joined: June 28th, 2015, 12:51 pm
Location: Paranaque City, Philippines

[SOLVED] Need help with RunCommand to ping a computer in home LAN

Post by nmdelrio »

I am aiming to make a skin to find out if a target computer on my home network is ON or OFF, and send an email if computer is OFF, the latter not being a Rainmeter chore though. I figure pinging it would be a good way. I sought help from ChatGPT 3.5 on iPhone, as I am not good at DOS commands, etc., and here's what I got. (It was interesting to know that ChatGPT can write Rainmeter scripts).

Code: Select all

[Rainmeter]
Update=1000

[MeasureRunScript]
Measure=Plugin
Plugin=RunCommand
Program="check_status.bat"
OutputType=ANSI
OutputFile=#@#status.txt
DynamicVariables=1
UpdateDivider=-1

[MeterDisplay]
[code]Meter=String
MeasureName=MeasureRunScript
FontSize=12
FontColor=255,255,255,255
Text=Status: %1
check_status.bat

Code: Select all

@echo off
ping -n 1 computer_name >nul
if errorlevel 1 (
    echo DOWN > "%APPDATA%\status.txt"
    powershell -ExecutionPolicy Bypass -File send_email.ps1
) else (
    echo UP > "%APPDATA%\status.txt"
)
send_email.ps1

Code: Select all

# PowerShell script to send an email using Gmail's SMTP server
$EmailFrom = "your_gmail@gmail.com"
$EmailTo = "recipient_email@example.com"
$Subject = "Computer DOWN Alert"
$Body = "The computer is DOWN. Please check."

$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($EmailFrom, "your_gmail_password")

$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
Both check_status.bat and send_email.ps1 are saved in skin's directory.

Changed all the necessary paramaters --- computer name, email addresses, etc. in check_status.bat and send_email.ps1.

Run independently, check_status.bat does its job, it writes the target computer's status in the text file status.txt in the %APPDATA% directory. BTW, send_email.ps1 does not do its job, I guess some Gmail security or permissions thing I have figured out yet.

However, when the skin is run, I don't get the results I want. check_status.bat does not create the text file status.txt in the %APPDATA% directory, so I take it that the batch file is not run by the skin. Consequently, MeasureRunScript returns a Number value of -1 and no String value at all in skin's log.

Any ideas? Does Rainmeter need special permissions to run a batch file? Is there another way to achieve what I am aiming at?
Last edited by nmdelrio on November 20th, 2023, 8:51 am, edited 1 time in total.
User avatar
Yincognito
Rainmeter Sage
Posts: 7179
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Need help with RunCommand to ping a computer in home LAN

Post by Yincognito »

If the overhyped, overmarketed and overtrolled ChatGPT really "knew" how to write such a code, IT would have come with a better solution than that, so that our "obsolete" human brains wouldn't have to correct the stupid thing on a forum like this, eh? :sly:

Anyway, besides the short rant against using an allegedly "artificially intelligent" system for something all of us are already perfectly capable to do given our innate ability to think, Rainmeter has a measure that can ping:
https://docs.rainmeter.net/manual/plugins/ping/

As for a basic example of a skin doing something similar to what you're aiming for (with some differences), see here:
https://forum.rainmeter.net/viewtopic.php?t=42401&start=10#p216687

Unfortunately, that topic is very long, marred by the OP's difficulties to understand English and the given explanations, and includes lots of other unrelated questions and code, but it should provide a decent start.

Regarding your Rainmeter code, there are multiple reasons why it doesn't work (hint: it's not because it couldn't work), but first you should decide on whether the "built-in" alternative is more comfortable for you or not...

P.S. The mailing part is up to you though. AFAIK, Rainmeter can't send emails on its own, so probably a PowerShell script is the best way to approach things in that regard.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
nmdelrio
Posts: 74
Joined: June 28th, 2015, 12:51 pm
Location: Paranaque City, Philippines

Re: Need help with RunCommand to ping a computer in home LAN

Post by nmdelrio »

Yincognito wrote: November 16th, 2023, 7:04 pm If the overhyped, overmarketed and overtrolled ChatGPT really "knew" how to write such a code, IT would have come with a better solution than that, so that our "obsolete" human brains wouldn't have to correct the stupid thing on a forum like this, eh? :sly:
Dang, you're right about that. I was just playing around with the ChatGPT app and thought if it could do something for me, all the hype.

Anyway, besides the short rant against using an allegedly "artificially intelligent" system for something all of us are already perfectly capable to do given our innate ability to think, Rainmeter has a measure that can ping:
https://docs.rainmeter.net/manual/plugins/ping/
Aahhh, that's good enough for me. I can do aomething with that. It just didn't occur to me that we have such a plugin. Thanks :D
As for a basic example of a skin doing something similar to what you're aiming for (with some differences), see here:
https://forum.rainmeter.net/viewtopic.php?t=42401&start=10#p216687
Thanks for these directions. too. I will see what I can learn from there.
P.S. The mailing part is up to you though. AFAIK, Rainmeter can't send emails on its own, so probably a PowerShell script is the best way to approach things in that regard.
The mailing thing is not a big deal, can do without it, but will still look into it if I can make PS do it my bidding. :sly:
User avatar
Yincognito
Rainmeter Sage
Posts: 7179
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Need help with RunCommand to ping a computer in home LAN

Post by Yincognito »

nmdelrio wrote: November 17th, 2023, 3:07 am Aahhh, that's good enough for me. I can do aomething with that. It just didn't occur to me that we have such a plugin. Thanks :D

Thanks for these directions. too. I will see what I can learn from there.

The mailing thing is not a big deal, can do without it, but will still look into it if I can make PS do it my bidding. :sly:
Sure thing. The OP in that topic wanted something slightly different than you do, hence making the icons transparent when "off" (aka the ping response time was greater than some adjustable number of milliseconds) and opaque when "on" (aka the ping response time was under that milliseconds value), but that code can be adjusted / changed to suit your needs. Feel free to ask if you have difficulties in doing that - we'd be glad to help with that. Also, if you just want some "live" indicator of those ping states for yourself at the time you're looking at the skin, you don't even need the mailing part. Stuff can also be written in a local file too, if by any chance you want to have some kind of "stats" where you can examine past values of those ping states. ;-)
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
nmdelrio
Posts: 74
Joined: June 28th, 2015, 12:51 pm
Location: Paranaque City, Philippines

Re: Need help with RunCommand to ping a computer in home LAN

Post by nmdelrio »

Yincognito wrote: November 17th, 2023, 3:26 pm Sure thing. The OP in that topic.... ;-)
I decided to to do just a simple ping meter to indicate if the target computer is UP or DOWN. None of the scripting Mr.Ol'BigNutsAIOpenChatGPT has suggested. :D

The intention is just to have a meter to indicate if the target computer is UP or DOWN, pinging it once every hour, and if DOWN play an audible alert every 5 seconds, else do nothing.

If you can bother yourself to look into the scripting, I'd be happy.

Not sure if I need IfCondition Mode=1 in [AudibleAlert].

Here's the Ping part of the skin.

Code: Select all

[Rainmeter]
Update=200

...

[PingMEDIASERVER]
Measure=Plugin
Plugin=PingPlugin
DestAddress=HPMediaServer
IfCondition=#CURRENTSECTION# = 30000
IfTrueAction=!SetOption MeterMEDIASERVERSTATUS Text "MEDIA SERVER STATUS#CRLF#DOWN"
IfFalseAction=!SetOption MeterMEDIASERVERSTATUS Text "MEDIA SERVER STATUS#CRLF#UP"
UpdateRate=18000

[AudibleAlert]
Measure=CALC
Formula=MeasurePingMediaServer
IfCondition=#CURRENTSECTION# = 30000
IfTrueAction=Play #@#\Audio\themediaserverisdown-reverb.wav
UpdateDivider=25

[MeterMEDIASERVERSTATUS]
Meter=STRING
MeterStyle=MeterHOSTNAME|MeterADAPTER_RECEIVE
MeasureName=PingMEDIASERVER
Text=MEDIA SERVER STATUS#CRLF#%1
It would be nice to add an Uptime meter of the target computer, but I don't think Rainmeter can do a remote access of another computer. (BTW FYI the target computer is a old PC (intel Core i7 870) that I converted into a home media server.)
User avatar
Yincognito
Rainmeter Sage
Posts: 7179
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Need help with RunCommand to ping a computer in home LAN

Post by Yincognito »

nmdelrio wrote: November 18th, 2023, 1:12 am I decided to to do just a simple ping meter to indicate if the target computer is UP or DOWN. None of the scripting Mr.Ol'BigNutsAIOpenChatGPT has suggested. :D

The intention is just to have a meter to indicate if the target computer is UP or DOWN, pinging it once every hour, and if DOWN play an audible alert every 5 seconds, else do nothing.

If you can bother yourself to look into the scripting, I'd be happy.

Not sure if I need IfCondition Mode=1 in [AudibleAlert].

Here's the Ping part of the skin.

Code: Select all

[Rainmeter]
Update=200

...

[PingMEDIASERVER]
Measure=Plugin
Plugin=PingPlugin
DestAddress=HPMediaServer
IfCondition=#CURRENTSECTION# = 30000
IfTrueAction=!SetOption MeterMEDIASERVERSTATUS Text "MEDIA SERVER STATUS#CRLF#DOWN"
IfFalseAction=!SetOption MeterMEDIASERVERSTATUS Text "MEDIA SERVER STATUS#CRLF#UP"
UpdateRate=18000

[AudibleAlert]
Measure=CALC
Formula=MeasurePingMediaServer
IfCondition=#CURRENTSECTION# = 30000
IfTrueAction=Play #@#\Audio\themediaserverisdown-reverb.wav
UpdateDivider=25

[MeterMEDIASERVERSTATUS]
Meter=STRING
MeterStyle=MeterHOSTNAME|MeterADAPTER_RECEIVE
MeasureName=PingMEDIASERVER
Text=MEDIA SERVER STATUS#CRLF#%1
It would be nice to add an Uptime meter of the target computer, but I don't think Rainmeter can do a remote access of another computer. (BTW FYI the target computer is a old PC (intel Core i7 870) that I converted into a home media server.)
First, sorry for the delay in responding, had some things to do today and took a little break from the forum related stuff.
You could adjust a bit your code to (yes, IfConditionMode is needed here, since you want to check that condition on every update of AudibleAlert):

Code: Select all

[Rainmeter]
Update=200

...

AccurateText=1
DynamicWindowSize=1
BackgroundMode=2
SolidColor=47,47,47,255

[Variables]
HPMediaServer=www.goxogle.com

[MeterHOSTNAME]
FontColor=255,255,255,255
FontFace=Consolas
FontSize=16
Padding=5,5,5,5

[MeterADAPTER_RECEIVE]
AntiAlias=1

...

[PingMEDIASERVER]
Measure=Plugin
Plugin=PingPlugin
DestAddress=#HPMediaServer#
RegExpSubstitute=1
Substitute="^(0)$":"WAIT (\1 ms)","^(30000)$":"DOWN (\1 ms)","^(\d+)$":"UP (\1 ms)"
OnChangeAction=[PlayStop][!PauseMeasure AudibleAlert][!UnpauseMeasure AudibleAlert][!UpdateMeasure AudibleAlert][!UpdateMeter MeterMEDIASERVERSTATUS][!Redraw]
UpdateRate=18000

[AudibleAlert]
Measure=CALC
Formula=PingMEDIASERVER
IfCondition=(#CURRENTSECTION#=30000)
IfTrueAction=[Play "#@#\Audio\themediaserverisdown-reverb.wav"]
IfConditionMode=1
UpdateDivider=25

[MeterMEDIASERVERSTATUS]
Meter=STRING
MeterStyle=MeterHOSTNAME|MeterADAPTER_RECEIVE
MeasureName=PingMEDIASERVER
Text=MEDIA SERVER STATUS#CRLF#%1
LeftMouseUpAction=[PlayStop][!TogglePauseMeasure AudibleAlert]
The parts between the ... are mainly to add the meter styles I didn't have so the code can be tested, but also to include a HPMediaServer variable where you can store the actual address or IP of your target computer (the HPMediaServer string you had in your code did precisely nothing as it wasn't a valid address / IP). As you can see, I defined the HPMediaServer variable to some easy to change from valid to invalid and viceversa address, but you can define it based on your case.

I replaced your otherwise correct IfCondition with a regular expression substitute doing more or less the same thing, without the need to set the Text in MeterMEDIASERVERSTATUS (that Text option will display the substituted string value of PingMEDIASERVER anyway). The substitute basically replaces 0 with WAIT (0 ms), 30000 with DOWN (30000 ms) and any other N number with UP (N ms) for conveniency - feel free to adjust to your preference or keep the earlier IfCondition if it feels more comfortable. For the record, in the regular expressions ^ means the start of the string, $ means the end of the string, \d means a digit, \d+ means one or more digits, and the ( and ) round brackets capture the enclosed part of the string to substitute so that the said capture can then be referenced in the sustituted string using \1, \2 and so on depending on whether you reference the 1st, the 2nd and so on capture.

I also stopped sound playing, paused and unpaused AudibleAlert on receiving the new ping response value in PingMEDIASERVER's OnChangeAction in order to "reset & resume" both the sound playing and the repeated checking of the said response in AudibleAlert. This suits well with toggling the sound playing on demand via left click in MeterMEDIASERVERSTATUS.

Most of these changes (bar the DestAddress value being an actual address or IP and the IfConditionMode, which are mandatory) are optional and reflect how I would approach this, so it's up to you if you want to keep them or not. Again, sorry for the delay in responding to such a simple question, but occasionally there's real life too, so... :D

P.S. Yeah, Rainmeter can't do remote Uptime, but if you find a way to do it via command prompt, PowerShell or any other actual scripting tools, Rainmeter can display that outcome (if any) via a RunCommand measure. Either that, or updating some (online) webpage from the target computer, that you can then scrape using a WebParser measure from your skin on the main computer. Or, the email method, if you can make it work. Basically, you need to solve the computer to computer communication problem, before Rainmeter skins can handle the other part...
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
nmdelrio
Posts: 74
Joined: June 28th, 2015, 12:51 pm
Location: Paranaque City, Philippines

[SOLVED] Re: Need help with RunCommand to ping a computer in home LAN

Post by nmdelrio »

The parts between the ... are mainly to add the meter styles I didn't have so the code can be tested,
So sorry about the ... but the parts in there are not relevant to thePing measures/meters. The Ping measures/meters are part of a larger skin.

Code: Select all

OnChangeAction=[PlayStop][!PauseMeasure AudibleAlert][!UnpauseMeasure AudibleAlert][!UpdateMeasure AudibleAlert][!UpdateMeter MeterMEDIASERVERSTATUS][!Redraw]
I also stopped sound playing, paused and unpaused AudibleAlert on receiving the new ping response value in PingMEDIASERVER's OnChangeAction in order to "reset & resume" both the sound playing and the repeated checking of the said response in AudibleAlert. This suits well with toggling the sound playing on demand via left click in MeterMEDIASERVERSTATUS.
At first, I was confused on this part (pausing then immediately unpausing the same measure), but after analyzing what it does, I said to myself "Dang! That was smart!" That's something worthwhile learning. Thanks. :thumbup:

The left click action to stop the sound on demand is smart as well to stop a potentially annoying sound over and over. :bow:

I got everything working as intended. :great:

Thanks a lot for the help. And the free learning.
User avatar
Yincognito
Rainmeter Sage
Posts: 7179
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: [SOLVED] Re: Need help with RunCommand to ping a computer in home LAN

Post by Yincognito »

nmdelrio wrote: November 20th, 2023, 8:50 am At first, I was confused on this part (pausing then immediately unpausing the same measure), but after analyzing what it does, I said to myself "Dang! That was smart!" That's something worthwhile learning. Thanks. :thumbup:

The left click action to stop the sound on demand is smart as well to stop a potentially annoying sound over and over. :bow:

I got everything working as intended. :great:

Thanks a lot for the help. And the free learning.
You're welcome, have fun with it and tweak it to your preference. ;-)

For the record, the pausing and unpausing thing is actually a somewhat incomplete kind of "reset", which normally should have been done by disabling and enabling the measure instead (that would have also reset the measure's value to 0 before restarting), as well as starting with it initially disabled. I chose the pausing method to keep it simpler and more logical, considering that the practical outcome is largely the same here.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth