import os from pathlib import Path import subprocess PORT = 22 USER = "mspe" ROOT = "public_html" PROTOCOL = "https" DIR = False class PubNix: def __init__( self, dn, ssh: str = None, port: int = PORT, user: str = USER, root: str = ROOT, dir: bool = DIR, web: str = None, sub: bool = None, ): self.dn = dn self.ssh = f"{ssh}.{self.dn}" if ssh else self.dn self.port = port self.user = user self.root = root self.web = f"{web}.{self.dn}" if web else self.dn if sub: self.fqdn = f"{self.user}.{self.web}" self.context = None else: self.fqdn = self.web self.context = f"~{self.user}" self.path = (Path(self.root) / self.fqdn) if dir else self.root self.target = f"{self.user}@{self.ssh}:{self.path}" self.url = [f"{PROTOCOL}:", "", self.fqdn] if self.context: self.url.append(self.context) self.url = "/".join(self.url) def capturun(self, *args): return self.run(*args, capture_output=True) def run(self, *args, **kwargs): return subprocess.run( [ "ssh", "-o", "LogLevel Error", "-p", str(self.port), f"{self.user}@{self.ssh}", "--", *args, ], **kwargs, ) def disk_free(self) -> str: process = self.capturun("df", "-h", os.curdir) return process.stdout.decode("UTF-8").strip() def os(self) -> str: ps = self.capturun("cat", "/etc/os-release") if ps.returncode == 0: for line in ps.stdout.decode("UTF-8").strip().split(os.linesep): if "PRETTY_NAME" in line: return line.split("=")[1].split('"')[1] else: return self.capturun("uname", "-sr").stdout.decode("UTF-8").strip() def __str__(self) -> str: """Return target & url.""" return os.linesep.join([self.target, self.url])