Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import sys
- import requests
- import json
- import time
- import warnings
- from datetime import date
- warnings.filterwarnings("ignore", category=DeprecationWarning)
- import telnetlib
- TELNET_PORT = 23
- def pass_of_the_day():
- def gcd(a, b):
- return a if not b else gcd(b, a % b)
- curdate = date.today()
- month, day = curdate.month, curdate.day
- return f"{month:x}{month:02}-{day:02x}{gcd(month, day):02}"
- def enable_telnet(nas_ip):
- url = f"http://{nas_ip}:5000/webman/start_telnet.cgi"
- try:
- res = requests.get(url)
- response = res.json()
- if res.status_code == 200:
- response = res.json()
- if "success" in response:
- return response["success"]
- else:
- print(f"WARNING: got unexpected response from NAS:\n"
- f"{json.dumps(response, indent=4)}")
- return False
- else:
- print(f"ERROR: NAS returned http error {res.status_code}")
- return False
- except Exception as e:
- print(f"ERROR: got exception {e}")
- return False
- def telnet_try_login(telnet, password):
- # Wait for login prompt
- telnet.read_until(b"login: ")
- telnet.write("root".encode("ascii") + b'\n')
- # Wait for password prompt
- telnet.read_until(b"Password: ")
- telnet.write(password.encode("ascii") + b'\n')
- login_succeeded = True
- for i in range(5):
- # skip the remote side wrong password delay and crap it sends
- line = telnet.read_until(b'\n', 1)
- if not line or line == b"\r\n":
- continue
- if b"Login incorrect" in line:
- login_succeeded = False
- break
- # consider NAS telnet prompt as successful login
- if len(line) > 30:
- break
- return login_succeeded
- def exec_cmd_via_telnet(host, port, command):
- no_rtc_pass = "101-0101"
- try:
- telnet = telnetlib.Telnet(host, port, timeout=10)
- print(f"INFO: connected via telnet to {host}:{port}")
- rc = telnet_try_login(telnet, pass_of_the_day())
- if not rc:
- print("INFO: password of the day didn't work, retrying with "
- "the 'no RTC' password")
- rc = telnet_try_login(telnet, no_rtc_pass)
- if rc:
- print("INFO: telnet login successful")
- else:
- print("ERROR: telnet login failed")
- return False
- # flush lengthy NAS telnet prompt
- telnet.read_until(b"built-in shell (ash)", 1)
- # Run the command
- telnet.write(command.encode("ascii") + b'\n')
- time.sleep(1)
- telnet.write(b"exit\n") # Close the session
- # Read output (if any)
- #output = telnet.read_all().decode("ascii")
- print("INFO: command executed. Telnet session closed.")
- #print("DEBUG: output:\n", output)
- except Exception as e:
- print("Telnet error:", e)
- return False
- return True
- def main():
- if len(sys.argv) != 2:
- print(f"Usage:\npython3 {sys.argv[0]} <NAS_IP>")
- return -1
- nas_ip = sys.argv[1]
- rc = enable_telnet(nas_ip)
- if rc:
- print("INFO: successfully enabled telnet on NAS")
- else:
- print("ERROR: failed to enable telnet, stopping")
- return -1
- rc = exec_cmd_via_telnet(nas_ip, TELNET_PORT,
- "while true; do touch /tmp/installable_check_pass; sleep 1; done &")
- return 0 if rc else -1
- if __name__ == "__main__":
- exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement