0

I need to write a python script where I send files to a remote machine, then perfom some operation on them (again with python) and then send them back to my local machine using paramiko, but I haven't able to do that, It just doesn't do anything after I try to connect back to my local machine

On my local machine, I got this

client = paramiko.SSHClient()
client.load_system_host_keys()

client.connect(hostname=IP, username=usrname)

stfp_client = client.open_sftp()
stfp_client.put(source_path, des_path)

stdin, stdout, stderr = client.exec_command("python3 /home/scripts/main.py ")

And on my remote machine, after I perform some operations to my files I got the same code except that the ssh and sftp connection is from my remote to my local machine, but it doesn't send anything, I already debugged it and on the remote side it gets until the client = paramiko.SSHClient() so I wanted to know what am I doing wrong or another way to perform this task. Thank you in advance and I'm sorry for my bad english

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • So what does it do when it gets to the `client = paramiko.SSHClient()`? + Why don't you simply download the files with `.get` after you execute the remote script? – Martin Prikryl Jun 25 '23 at 11:44
  • @MartinPrikryl I suppose It just exits the program because I print the stdout that returns from exec_command and it just prints the debugs that are before the ` paramiko.SSHClient() ` and also because I don't get any results on my local machine. I don't use the ` get ` method because I don't know how long it will take my remote machine (that is actually a virtual machine) to perform these operations because the number of files/directories can vary so I can't estimate how much time wait for the results to use the `get` method – AMLO_TACTICO Jun 25 '23 at 17:08
  • Did you try reading `stderr`? + Do not estimate anything. Just wait for the script to complete. – Martin Prikryl Jun 25 '23 at 18:48
  • @MartinPrikryl You were right! , stderr gave me this: ' "raise SSHException("not a valid " + tag + " private key file") paramiko.ssh_exception.SSHException: not a valid RSA private key file" ' When I try to connect with ssh on the remote side. I already tried to execute it manually from my virtual machine and it does everything correctly so I suppose the problem is trying to connect it back to my local machine. Also, I already considered waiting for the script to complete, but how do I know when the process has finished? Maybe with a flag? – AMLO_TACTICO Jun 26 '23 at 01:01
  • I do not understand. If the remote script works when executed manually (from the remote machine), how can the problem be with connecting back to your local machine? + See [Execute command and wait for it to finish with Python Paramiko](https://stackoverflow.com/q/34181078/850848) (it's really not hard to find...) – Martin Prikryl Jun 26 '23 at 06:12

0 Answers0