2

I am new to this and need some advice on resolving my problem. I have a python script on a remote server. I want to execute that script from my local system and then transfer the result file generated on the remote server to my local system.

So, I would be grateful if someone can help me with a script or single command that can do this.

I tried a script but when I am in the remote server via SSH, the second line to execute the python script does not work. I also tried using nohup but still, it is not working

Below are the steps that need to be followed

  • ssh
  • python script.py
  • Then 'exit' from ssh
  • scp <output_file>
1
  • "I tried a script but when I am in the remote server via SSH, the second line to execute the python script does not work" – Read the beginning of this answer. Commented Apr 10, 2022 at 21:44

1 Answer 1

1

You can do this with

ssh user@server "python script.py"
scp user@server:/remote/file.txt /local/directory

This executes the script on the remote machine, providing you have the proper ip address and filepaths, and will then copy the file from the remote machine to yours.

You could also execute the script using a cron job on the remote machine if you would like to look into it

EDIT: There is no need to actually exit ssh, becuase this ssh command executes the command, but does not establish a running shell, you can test this by running ssh localhost ls

5
  • Thank you! I tried running the first step, I am getting some script related error as below (I already installed netaddr): from netaddr import IPAddress ImportError: No module named netaddr
    – Pawan
    Commented Apr 9, 2022 at 23:28
  • try logging in and running your script normally
    – puma
    Commented Apr 9, 2022 at 23:37
  • If I execute the python script by logging in to the server directly, then it is running fine. But when I am trying to execute via my local system using the command you provided, it is giving the same error
    – Pawan
    Commented Apr 9, 2022 at 23:54
  • Try creating script on the remote machine with #!/bin/bash python script.py and execute the script
    – puma
    Commented Apr 9, 2022 at 23:58
  • I created a test script that worked fine but with my existing script it is throwing the error "No module named netaddr"
    – Pawan
    Commented Apr 11, 2022 at 19:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .