Example #1
0
 def run(self):
     Analyzer.run(self)
     try:
         if self.data_type == "file":
             """Initialise remote host client and execute actions."""
             filepath = self.get_param('file', None, 'file is missing')
             filename = self.get_param('filename',
                                       basename(filepath)).replace(
                                           " ", "_")
             x = datetime.now()
             r_path = self.remote_path + '/' + filename + x.strftime(
                 "%Y%m%d%H%M%S%f")
             remote = RemoteClient(self.host, self.username,
                                   self.passphrase, self.ssh_key_filepath,
                                   self.remote_path)
             remote.scp.put(filepath, remote_path=r_path)
             self.report({'success': 'file transferred!'})
         else:
             self.error("invalid data type!")
     except (NoValidConnectionsError, SSHException) as e:
         print('SSH Connection refused: Check config in cortex.', e)
     except FileNotFoundError as e:
         print('SSH key not found: Check config in cortex.', e)
     except PermissionError as e:
         print(
             'SSH key incorrect permissions: ensure cortex user can read key specified in Cortex config.',
             e)
     except SCPException as e:
         print('Remote directory does not exist: Check config in cortex.',
               e)
Example #2
0
 def list_files(self) :
     self.conn = RemoteClient._connect(self)
     directory_name = data["LIST_FILES_DIRECTORY_NAME"]
     cmd = "ls -lrth " + directory_name
     stdin, stdout, stderr = self.conn.exec_command(cmd)
     response = stdout.readlines()
     for line in response :
         logger.info(f'{line}')
Example #3
0
 def execute_command(self) :
     self.conn = RemoteClient._connect(self)
     for cmd in self.commands :
         if cmd == "display_file" :
             self.cat_file()
         elif cmd == "create_directory" :
             self.create_directory()
         elif cmd == "list_files" :
             self.list_files()
Example #4
0
 def create_directory(self) :
     self.conn = RemoteClient._connect(self)
     directory_name = data["REMOTE_DIR_NAME"]
     cmd = 'mkdir ' + directory_name
     stdin, stdout, stderr = self.conn.exec_command(cmd)
     if stdout.channel.recv_exit_status() == 0 :
         logger.info("Directory created")
     else :
         logger.error("Directory already exists")
Example #5
0
 def cat_file(self) :
     self.conn = RemoteClient._connect(self)
     file_name = data["REMOTE_FILE_NAME"]
     cmd = 'cat ' + file_name
     stdin, stdout, stderr = self.conn.exec_command(cmd)
     if stdout.channel.recv_exit_status() == 0 :
         response = stdout.readlines()
         for line in response :
             logger.info(f'{line}')
     else :
         logger.error("File does not exist")
Example #6
0
def command_send(device, commands, pause):
    user = config.StarOS.user
    password = config.StarOS.password
    host = device.get('host')
    commands = [command + "\n" for command in commands]

    try:
        with RemoteClient(host, user, password) as ssh:
            ssh.shell(commands, pause=pause, buffer=20000)
    except paramiko.SSHException as sshException:
        print(f'Error: {sshException}')
Example #7
0
def main(hostname, username, ssh_key_file, passwd):

    # create certs for username
    key_file = CertHandler.generate_key(username, passwd)
    csr_file = CertHandler.generate_certificate_signing_request(username, passwd, key_file)

    # transfer csr to server


    # sign csr to create certificate








    remote = RemoteClient(hostname, username, passwd, ssh_key_file)

    # do these commands
    remote.execute_commands(['pwd', 'ls', 'whoami'])

    commands = [
        ''
    ]


    remote.disconnect()
Example #8
0
def run_commands(device, file):
    project_dir = Path().resolve()
    local_path = Path(project_dir, file)
    remote_path = f'/sftp/{file}'
    host = device.get("host")
    hostname = device.get("hostname")

    user = config.StarOS.script_user
    password = config.StarOS.script_password

    try:
        with RemoteClient(host, user, password) as ssh:
            ssh.upload_file(hostname, local_path, remote_path)
            ssh.shell([
                "y\n", f"copy /flash/sftp/{file} /flash/{file}\n", "yes\n",
                "show plugin p2p\n", f"dir /flash | grep {file}\n"
            ],
                      pause=2)
            ssh.disconnect()
    except paramiko.SSHException as sshException:
        print(f'Error: {sshException}')
Example #9
0
def sync_dir(
    local_dir="/home/mike/googledrive/reMarkable",
    remote_dir="/home/root/koreader/01-pdfs",
    retries=100,
):

    fail = 0
    success = 0
    while (success == 0) and (fail < retries):
        print()
        try:
            remote_client = RemoteClient()
            success += 1
        except (
                TimeoutError,
                BadHostKeyException,
                AuthenticationException,
                SSHException,
                SCPException,
                SocketTimeout,
        ) as e:
            if fail < retries:
                print(f"{e} retrying... {retries - fail} left")
                fail += 1
            else:
                break

    local_pdf_files = [f for f in os.listdir(local_dir) if f.endswith(".pdf")]
    remote_files = remote_client.execute_commands([f"cd {remote_dir} && ls"])
    print(f"Local Files {local_pdf_files}")
    print(f"KOR Files {remote_files}")

    print(f"Removing {len(remote_files)} files from reMarkable:{remote_dir}")
    remote_client.execute_commands([f"rm -rf {remote_dir}/*"])

    print(f"Uploading {len(local_pdf_files)} from {local_dir}")
    remote_client.bulk_upload(
        [os.path.join(local_dir, file) for file in local_pdf_files])
Example #10
0
def all_logs(parameter):
    print("______________________________________________\n"
          "STARTING ALL LOG COLLECTION\n"
          "______________________________________________\n")
    racksn = input("Enter the RACK SN: ")
    pxe = menu.pxe_selection()  # must be filled in by user in the menu.py
    pxe_user = "******"
    # pxe_user = menu.pxe_user_selection() #Uncomment this if you want to query user for pxe  user

    remote = RemoteClient(gitServer, pxe, user, pxe_user,
                          ssh_key_filepath, git_ssh_key_filepath,
                          known_hosts_filepath, remote_path, gitServer2)
    # remote.upload_ssh_key() #currently not working properly

    # qpn = input("What is the QPN for the Rack? ")
    print("FINDING INFORMATION ABOUT THIS SN!\n")
    important_info = remote.qpn_finder(racksn)
    # You have to call this Remoteclient again, because the previous method messes with it. Find out more later
    remote = RemoteClient(gitServer, pxe, user, pxe_user,
                          ssh_key_filepath, git_ssh_key_filepath,
                          known_hosts_filepath, remote_path, gitServer2)
    qpn = important_info["RACKPN"]
    model = important_info["MODEL"]
    MBSN = important_info["MLBSN"]
    pdnum = important_info["PDNUM"]
    git_model_path = f"{remote_path}{model}_{racksn}_{MBSN}_{qpn}_logs"
    remote.execute_cmd_git([f"mkdir {git_model_path}"])

    dirs = remote.execute_cmd_pxe([f"grep -rl {pdnum} /RACKLOG/{model}/{year}/*"])
    for dir in dirs:
        dir = dir.replace(f"/RACKLOG/{model}/{year}/", "")
        chassissn = dir[6:20]
        print(f"Using {chassissn}")
        break
    print(f"You have entered:\n"
          f"RACKSN: {racksn}\n"
          f"MLBSN: {MBSN}\n"
          f"MODEL: {model}\n"
          f"QPN: {qpn}\n"
          f"PDNUM: {pdnum}\n"
          f"PXE: {pxe}\n"
          f"CHASSIS SN: {chassissn}\n"
          f"zip: {zip_this}\n"
          )

    tests = ["PRETEST", "RUNIN", "NETTEST", "FST"]
    for test in tests:  # cycle through the tests to find each one.
        # done_flag = False #flag for breaking out of loop
        dirs = []
        SNS = [MBSN, pdnum, chassissn]
        while not dirs:  # so long as the directory array is empty
            for sn in SNS:
                dirs = remote.execute_cmd_pxe([f"find /RACKLOG/{model}/{year}/* -name {sn}"])
                print(sn)
                print(dirs)
                if dirs:  # if something is found
                    break
            print("Broke out of loop")
            print(dirs)

            reg = re.compile(f'/RACKLOG/{model}/{year}/...\d\d/{sn}')
            dirs = [string for string in dirs if re.match(reg, string)]
            dirs.sort(key=lambda date: datetime.datetime.strptime(date, f'/RACKLOG/{model}/{year}/%b%d/{sn}'),
                      reverse=True)
            print(dirs)
            for dir in dirs:
                pass_dir = remote.execute_cmd_pxe([f"find {dir}/{test} -iname *pass*"])
                if pass_dir:
                    remote.execute_cmd_git([f"scp -r {pxe_user}@{pxe}:{dir}/{test} {git_model_path}"])
                    # print (f"scp -r {dir}/{test} {user}@{gitServer2}:{remote_path}{MBSN}_logs/\n")
                    break
                else:
                    print("Nothing was found\n")

        """
        This loop is to clear out the failed logs. 
        It's technically inefficient, but I'm too lazy to change it. 
        And I'd rather copy over too much than too little.
        """

    dirs = remote.execute_cmd_git([f"find {git_model_path} -iname *fail*"])
    for dir in dirs:
        remote.execute_cmd_git([f"rm -rf {dir}"])

    zip = str.lower(input("Do you want to zip?"))
    if zip == "y" or zip == "yes":
        remote.execute_cmd_git([f"cd {git_model_path}; zip -r {remote_path}{model}_{racksn}_{MBSN}_{qpn}_logs.zip ./"])
        cmd = f"scp -r  {user}@{gitServer}:{remote_path}{model}_{racksn}_{MBSN}_{qpn}_logs.zip {local_file_directory}"
    else:
        cmd = f"scp -r  {user}@{gitServer}:{remote_path}{model}_{racksn}_{MBSN}_{qpn}_logs {local_file_directory}"
    print(cmd)
    call(cmd)
Example #11
0
def fai(gui_important_infos):

    """print("______________________________________________\n"
          "STARTING FAI LOG COLLECTION\n"
          "______________________________________________\n")
    racksn = input("Enter the RACK SN: ")
    pxe = menu.pxe_selection()  # must be filled in by user in the menu.py"""

    pxe_user = "******"
    pxe = gui_important_infos["pxe"]
    racksn = gui_important_infos["rack_SN"]
    pw1 = gui_important_infos["gitpw"]
    pw2 = gui_important_infos["pxepw"]
    zip_this = gui_important_infos["zip"]

    """# pxe_user = menu.pxe_user_selection() #Uncomment this if you want to query user for pxe  user
    pw1 = getpass.getpass("What is the Git Password? ")
    pw2 = getpass.getpass("What is the PXE Password? ")"""
    remote = RemoteClient(gitServer, pxe, user, pxe_user,
                          ssh_key_filepath, git_ssh_key_filepath,
                          known_hosts_filepath, remote_path, gitServer2, pw1, pw2)
    # remote.upload_ssh_key() #currently not working properly

    # qpn = input("What is the QPN for the Rack? ")
    print("FINDING INFORMATION ABOUT THIS SN!\n")
    important_info = remote.qpn_finder(racksn)
    print(important_info)
    if not important_info:
        print(important_info)
        tkinter.messagebox.showerror(title="Oh Snap!",
                                     message="I couldn't find anything at this PXE\n"
                                             "Please Pick a different PXE: \n" )
        exit()
        #print("I couldn't find anything at this PXE\n"
        #     "Please Pick a different PXE: \n")
        #pxe = menu.pxe_selection()
        #remote.change_pxe(pxe)
        #important_info = remote.qpn_finder(racksn)

    # You have to call this Remoteclient again, because the previous method messes with it. Find out more later
    remote = RemoteClient(gitServer, pxe, user, pxe_user,
                          ssh_key_filepath, git_ssh_key_filepath,
                          known_hosts_filepath, remote_path, gitServer2, pw1, pw2)
    qpn = important_info["RACKPN"]
    model = important_info["MODEL"]
    MBSN = important_info["MLBSN"]
    #pdnum = important_info["PDNUM"]
    git_model_path = f"{remote_path}{model}_{racksn}_{MBSN}_{qpn}_logs"
    """
    #Below is key exchange. Will likely move it later
    #Currently can't get the key exchange working properly. Working on functions for now. 
    #putting the git key onto the pxe server
    git_key = remote.execute_cmd_git(["cat ~/.ssh/id_rsa.pub"])
    check = remote.execute_cmd_pxe([f"grep {git_key[0]} ~/.ssh/authorized_keys"])
    if not check:
        remote.execute_cmd_pxe([f"echo {git_key[0]} >> ~/.ssh/authorized_keys"])
    #putting the pxe key onto the git server.
    pxe_key = remote.execute_cmd_pxe(["cat ~/.ssh/id_rsa.pub"])
    check = remote.execute_cmd_git([f"grep {git_key[0]} ~/.ssh/authorized_keys"])
    if not check:
        remote.execute_cmd_git([f"echo {pxe_key[0]} >> ~/.ssh/authorized_keys"])
    """

    remote.execute_cmd_git([f"mkdir {git_model_path}"])  # create the log folder in the gitserver
    current_time = datetime.datetime.now()  # find the current time
    year = current_time.year
    print(current_time.month, current_time.day)
    months = {
        "Jan": 1,
        "Feb": 2,
        "Mar": 3,
        "Apr": 4,
        "May": 5,
        "Jun": 6,
        "Jul": 7,
        "Aug": 8,
        "Sep": 9,
        "Oct": 10,
        "Nov": 11,
        "Dec": 12
    }
    months_r = {
        1: "Jan",
        2: "Feb",
        3: "Mar",
        4: "Apr",
        5: "May",
        6: "Jun",
        7: "Jul",
        8: "Aug",
        9: "Sep",
        10: "Oct",
        11: "Nov",
        12: "Dec",
    }

    """
    This loop will find all folders related to the MBSN/SN of the model selected.
    It will then sort them by most recent and then SCP that entire folder over. (Folder cleanup happens next loop)
    """
    #dirs = remote.execute_cmd_pxe([f"grep -rl {pdnum} /RACKLOG/{model}/{year}/*"])
    dirs = remote.execute_cmd_pxe([f"grep -rl {MBSN} /RACKLOG/{model}/{year}/*"])
    for dir in dirs:
        dir = dir.replace(f"/RACKLOG/{model}/{year}/", "")
        chassissn = dir[6:20]
        print(f"Using {chassissn}")
        break
    print(f"You have entered:\n"
          f"RACKSN: {racksn}\n"
          f"MLBSN: {MBSN}\n"
          f"MODEL: {model}\n"
          f"QPN: {qpn}\n"
          #f"PDNUM: {pdnum}\n"
          f"PXE: {pxe}\n"
          f"CHASSIS SN: {chassissn}")

    tests = ["PRETEST", "RUNIN", "NETTEST", "FST"]
    for test in tests:  # cycle through the tests to find each one.
        # done_flag = False #flag for breaking out of loop
        dirs = []
        #SNS = [MBSN, pdnum, chassissn]
        SNS = [MBSN, chassissn] #new dropbox system doesn't have PDNUM
        while not dirs:  # so long as the directory array is empty
            for sn in SNS:
                dirs = remote.execute_cmd_pxe([f"find /RACKLOG/{model}/{year}/* -name {sn}"])
                print(sn)
                print(dirs)
                if dirs:  # if something is found
                    break
            print("Broke out of loop")
            print(dirs)

            reg = re.compile(f'/RACKLOG/{model}/{year}/...\d\d/{sn}')
            dirs = [string for string in dirs if re.match(reg, string)]
            dirs.sort(key=lambda date: datetime.datetime.strptime(date, f'/RACKLOG/{model}/{year}/%b%d/{sn}'),
                      reverse=True)
            print(dirs)
            for dir in dirs:
                pass_dir = remote.execute_cmd_pxe([f"find {dir}/{test} -iname *pass*"])
                if pass_dir:
                    remote.execute_cmd_git([f"scp -r {pxe_user}@{pxe}:{dir}/{test} {git_model_path}"])
                    # print (f"scp -r {dir}/{test} {user}@{gitServer2}:{remote_path}{MBSN}_logs/\n")
                    break
                else:
                    print("Nothing was found\n")

        """
        This loop is to clear out the failed logs. 
        It's technically inefficient, but I'm too lazy to change it. 
        And I'd rather copy over too much than too little.
        """
    
    dirs = remote.execute_cmd_git([f"find {git_model_path} -iname *fail*"])
    for dir in dirs:
        remote.execute_cmd_git([f"rm -rf {dir}"])

    #zip = str.lower(input("Do you want to zip?"))

    file_path=f"{remote_path}{model}_{racksn}_{MBSN}_{qpn}_logs"
    if zip_this == 1:
        remote.execute_cmd_git([f"cd {git_model_path}; zip -r {remote_path}{model}_{racksn}_{MBSN}_{qpn}_logs.zip ./"])
        #cmd = f"scp -r  {user}@{gitServer}:{remote_path}{model}_{racksn}_{MBSN}_{qpn}_logs.zip {local_file_directory}"
        remote.download_file(file_path+".zip", local_file_directory)
    else:
        #cmd = f"scp -r  {user}@{gitServer}:{remote_path}{model}_{racksn}_{MBSN}_{qpn}_logs {local_file_directory}"
        remote.bulk_download_file(file_path,local_file_directory)

    #print(cmd)
    #call(cmd)
    remote.disconnect()
    messagebox.showinfo(title="Success!", message="FAI is done.")
    print("FAI All Done!")
Example #12
0
def main():
    """Initialize remote host client and execute actions."""
    remote = RemoteClient(host, user, ssh_key_filepath, remote_path)
    #upload_files_to_remote(remote)
    execute_command_on_remote(remote)
    remote.disconnect()