christophergault198/BambuHMI

sftp not working correctly

AskAlice opened this issue · 0 comments

def upload_files():
if messagebox.askokcancel("Confirmation", "This will install all the files within /functions/Installed_machineMacros/macros & sh folders to the printer."):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(PRINTER_IP, username=PRINTER_USERNAME, password=PRINTER_PASSWORD) # Update with your server's details
sftp = ssh.open_sftp()
# Upload files from /functions/Installed_machineMacros/macros to /mnt/sdcard/x1plus/macros/
macros_source = 'functions/Installed_machineMacros/macros'
macros_target = '/mnt/sdcard/x1plus/macros/'
for file in os.listdir(macros_source):
sftp.put(os.path.join(macros_source, file), os.path.join(macros_target, file))
# Upload files from /functions/Installed_machineMacros/sh to /usr/bin
sh_source = 'functions/Installed_machineMacros/sh'
sh_target = '/usr/bin'
for file in os.listdir(sh_source):
sftp.put(os.path.join(sh_source, file), os.path.join(sh_target, file))
messagebox.showinfo("Success", "Files uploaded successfully!")
except Exception as e:
messagebox.showerror("Error", f"Failed to upload files: {e}")
finally:
if ssh:
ssh.close()

I had to patch with this, however it adds a dep of sshpass

def upload_files():
    if messagebox.askokcancel("Confirmation", "This will install all the files within /functions/Installed_machineMacros/macros & sh folders to the printer."):

        try:
            # Upload files from /functions/Installed_machineMacros/macros to /mnt/sdcard/x1plus/macros/
            macros_source = './functions/Installed_machineMacros/macros'
            macros_target = '/mnt/sdcard/x1plus/'
            #template in PRINTER_IP and PRINTER_USERNAME
            subprocess.run(['sshpass', '-p', PRINTER_PASSWORD, 'scp', '-r', macros_source, f'{PRINTER_USERNAME}@{PRINTER_IP}:{macros_target}'], check=True)
            # Upload files from /functions/Installed_machineMacros/sh to /usr/bin
            sh_source = 'functions/Installed_machineMacros/sh'
            sh_target = '/usr/bin'
            for file in os.listdir(sh_source):
                subprocess.run(['sshpass', '-p', PRINTER_PASSWORD, 'scp', os.path.join(sh_source,file), f'{PRINTER_USERNAME}@{PRINTER_IP}:{sh_target}'], check=True)
            messagebox.showinfo("Success", "Files uploaded successfully!")
        except Exception as e:
            messagebox.showerror("Error", f"Failed to upload files: {e}")