Skip to content

Instantly share code, notes, and snippets.

@HorlogeSkynet
Created May 23, 2022 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HorlogeSkynet/60bf83f4bb4bec75096094495e15fd3e to your computer and use it in GitHub Desktop.
Save HorlogeSkynet/60bf83f4bb4bec75096094495e15fd3e to your computer and use it in GitHub Desktop.
A Python script to programmatically shutdown a Buffalo TeraStation 3210DN NAS
#!/usr/bin/env python3
"""
A Python script to programmatically shutdown a Buffalo TeraStation 3210DN NAS.
Written by Samuel FORESTIER for Stefano PICA.
Inspired from : <https://gist.github.com/HorlogeSkynet/703c1546ae2e70fc6cf6f04b8eb5a9d1>.
===
Usage documentation :
1. Install dependency : `pip3 install requests`
2. Configure credentials : Add to your `~/.netrc` something like :
'''
machine your.nas.ip.address (or host-name)
login admin
password A_VERY_SECURE_ADMIN_PASSWORD
'''
3. Run the script : `python3 TeraStation_Shutdown.py your.nas.ip.address` (or host-name)
"""
import argparse
import netrc
import sys
import time
import urllib3
from urllib3.exceptions import InsecureRequestWarning
import requests
from requests.exceptions import RequestException
__author__ = "Samuel FORESTIER"
__copyright__ = "Copyright 2020-2022, TeraStation 3210DN shutdown script"
__license__ = "MIT"
__status__ = "Production"
__date__ = "2022-05-23"
__version__ = "v1.0.2"
def main():
"""Simple entry point"""
# Retrieve NAS's IP address/host-name from CLI.
parser = argparse.ArgumentParser(description="Buffalo TeraStation 3210DN NAS shutdown script")
parser.add_argument("hostname", help="NAS IP address (or resolvable host-name)")
args = parser.parse_args()
# Retrieve NAS credentials from `~/.netrc` file.
try:
net_rc = netrc.netrc()
except netrc.NetrcParseError as netrc_error:
print(f"[-] Couldn't parse netrc data : {netrc_error.msg}", file=sys.stderr)
sys.exit(1)
(nas_username, _, nas_password) = net_rc.authenticators(args.hostname)
if not nas_username:
print(f"[-] Couldn't retrieve NAS credentials for {args.hostname}", file=sys.stderr)
sys.exit(1)
# Hide urllib3 warning messages about unsigned certificate.
urllib3.disable_warnings(InsecureRequestWarning)
base_url = "https://{}/".format(args.hostname)
# Create a new `Requests` session.
with requests.Session() as session:
# Fake the authentication process.
try:
response = session.post(
base_url + "nasapi/",
headers={"Referer": base_url + "login.html", "X-Requested-With": "XMLHttpRequest"},
json={
"jsonrpc": "2.0",
"method": "Auth.login",
"params": {"username": nas_username, "password": nas_password, "timeout": "0"},
"id": str(int(time.time())),
},
timeout=5,
verify=False,
)
response.raise_for_status()
except RequestException as requests_error:
print(
f"[-] Couldn't authenticate on the TeraStation : {requests_error}", file=sys.stderr
)
sys.exit(1)
json_response = response.json()
if "result" not in json_response or "sid" not in json_response["result"]:
print(
f"[-] Couldn't authenticate on the TeraStation, got : {json_response}",
file=sys.stderr,
)
sys.exit(1)
print("[+] Successfully authenticated on the TeraStation.")
try:
response = session.post(
base_url + "nasapi/",
headers={"Referer": base_url + "detail.html", "X-Requested-With": "XMLHttpRequest"},
json={
"jsonrpc": "2.0",
"method": "System.shutdown",
"params": {"sid": json_response["result"]["sid"]},
"id": str(int(time.time())),
},
timeout=5,
verify=False,
)
response.raise_for_status()
except RequestException as requests_error:
print(
f"[-] Couldn't send the shutdown procedure on the TeraStation : {requests_error}",
file=sys.stderr,
)
sys.exit(1)
if response.json()["result"] is not None:
print(
"[-] An unknown error occurred while shutting down the TeraStation...",
file=sys.stderr,
)
sys.exit(1)
print("[+] The shutdown procedure has been successfully started on the TeraStation.")
if __name__ == "__main__":
main()
@go2tom42
Copy link

This also works on a Buffalo TeraStation TS51210RH with no modifications

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment