Secure Copy File from remote server via scp and os module in Python

后端 未结 1 1710
遇见更好的自我
遇见更好的自我 2021-01-13 02:29

I\'m pretty new to Python and programming. I\'m trying to copy a file between two computers via a python script. However the code

os.system(\"ssh \" + hostna         


        
1条回答
  •  北海茫月
    2021-01-13 02:43

    I think the easiest (to avoid having to enter a password) and most secure way to go about this is to first set public/private key authentication. Once that is done, and you can log in to the remote system by doing ssh user@hostname, the following bash command would do the trick:

    scp some/complete/path/to/file user@remote_system:some/remote/path
    

    The corresponding Python code would be:

    import subprocess
    
    filepath = "some/complete/path/to/file"
    hostname = "user@remote_system"
    remote_path = "some/remote/path"
    
    subprocess.call(['scp', filepath, ':'.join([hostname,remote_path])])
    

    0 讨论(0)
提交回复
热议问题