Synolog NAS Control

Started by KeithW, June 25, 2024, 07:26:11 PM

Previous topic - Next topic

KeithW

Greetings, thought I would just float this out for any of the admins & wizards...

I had a UPS crap out and 3 bad power outages that caused some damage and sent me into repair mode.  With a new UPS and the hardware replaced and recoveries done I am now looking to attempt something.

I got a Synology DS223, 2 drive NAS unit.  Also a new UPS unit.  I can script a shutdown command file to shutdown other systems on my net when a power failure occurs.  If I get a separate UPS with the right capabilities I can have it put the NAS in safe mode and shut it down gracefully.

If I am at the system when the power fails I can log into the NAS Control software and shut the NAS down BUT Synology does not appear to have a tool to do the same and the UPS I have is big enough to handle all the hardware I am running so I do not want to buy another UPS unit.

I figure this is a HUGE longshot... BUT Has anybody attempted to write a script that would log into a NAS (preferably Synology) and trigger a graceful shutdown?

Keith

td

I am unfamiliar with your NAS but it should have some form of programmable interface. Most peripheral devices do and they are often HTTP protocol based. You could check the manufacturer's Website and you might find something that can be scripted with WinBatch. Most HTTP protocols can be.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

kdmoyers

I have several Synology nas boxen, but have not thought of this idea -- it's a good one.

This google search brings up several rabbit holes to go down.
"synology nas shutdown script"

None of these people are talking about a winbatch script, but you gotta start somewhere.

The most interesting one seems to be
https://www.c-amie.co.uk/technical/how-to-shutdown-a-synology-dsm-6-nas-using-a-script/
The mind is everything; What you think, you become.

spl

Quote from: kdmoyers on June 26, 2024, 01:39:57 PMThe most interesting one seems to be
https://www.c-amie.co.uk/technical/how-to-shutdown-a-synology-dsm-6-nas-using-a-script/


I was looking at it just before this was posted. Article seems to be based on a connection between the NAS and UPS, and discusses WOL capabilities. Nice that he showed script in Powershell which is cross-platform.
Stan - formerly stanl [ex-Pundit]

td

It would appear that the NAS is running Debian-flavored Linux. The script examples are calling plink.exe to issue a remote Debian bash shell command to shut down the OS. It is easily done with plink.exe* and straight WinBatch.

Nice find kdmoyers.

*Plink.exe (PuTTy) is a free telnet/SSH command line client for Windows.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

bottomleypotts

Actually Synology has an API. It's just so poorly documented.



#DefineFunction UDF_synLogin(obj,dev,port,user,pass)
em=ErrorMode(@OFF)
url=`http://`:dev:`:`:port:`/webapi/auth.cgi?api=SYNO.API.Auth&version=3&method=login&account=`:user:`&passwd=`:pass:`&session=Winbatch&format=cookie`
obj.Open(`GET`,url,@FALSE)
obj.SetRequestHeader(`Content-Type`,`application/x-www-form-urlencoded`)
obj.Send(``)
obj.WaitForResponse()
ret=obj.ResponseText
ErrorMode(em)
Return ret
#EndFunction

#DefineFunction UDF_synShutdown(obj,dev,port,sid)
em=ErrorMode(@OFF)
url=`http://`:dev:`:`:port:`/webapi/auth.cgi`
obj.Open(`POST`,url,@FALSE)
cookieHeader=`sid=`:sid
obj.SetRequestHeader(`Cookie`,cookieHeader)
obj.Send(`api=SYNO.Core.System&method=shutdown&version=1`)
obj.WaitForResponse()
ret=obj.ResponseText
ErrorMode(em)
Return ret
#EndFunction


AddExtender(`ilcjs44i.dll`,0,`ilcjs64i.dll`) ; Load the JSON extender

synObj=CreateObject(`WinHttp.WinHttpRequest.5.1`)

synDev=`192.168.19.109`
synPort=`5000`
synUser=`itsupport`
synPass=`@KyaYxZ$UYbaP6`

Ret=UDF_synLogin(synObj,synDev,synPort,synUser,synPass)

If !jsValid(Ret) Then   Exit

j=jsParse(Ret)

synJson=jsConMap(j,@JsonValue)
synData=jsConMap(synJson[`data`],@JsonValue)

synSID=synData[`sid`]

jsConClose()

Ret=UDF_synShutdown(synObj,synDev,synPort,synSID)

Exit

kdmoyers

Wow, this is great Potts!
So much to learn about!
-Kirby
The mind is everything; What you think, you become.

kdmoyers

The mind is everything; What you think, you become.

td

Have to agree with Kirby. Nice script Potts. Hopefully, the OP will find it useful.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

KeithW

bottomleypotts,

way above my pay grade, but your script works perfectly !!   Thank-you !!!

If you have a Synology NAS, edit the username/password entries and away you go...  I did have to
upgrade to 2024A since I did not have the JSON Extender and not sure when it came about, but a very
small price to pay to get access to this script !!!

Thanx again
Keith