來源自 這裡
Question:
As title.
Solution:
方法一:
If you want the simple approach, this should work. You'll want to ".close()" the file first so you know it's flushed to disk from Python. Also you will need to generate (on the source machine) and install (on the destination machine) an ssh key beforehand so that the scp automatically gets authenticated with your public ssh key (in other words, so your script doesn't ask for a password). Below is the sample code to comply with your request:
References:
* [Linux 小學堂] SSH 免密碼登入
* 鳥哥的 Linux - 檔案異地直接複製: scp
* Python module - os: os.system(command)
方法二:
To do this in Python (i.e. not wrapping scp through subprocess.Popen or similar) with the Paramiko library, you would do something like this:
If you want to execute command in remote server, below is the sample code:
Question:
As title.
Solution:
方法一:
If you want the simple approach, this should work. You'll want to ".close()" the file first so you know it's flushed to disk from Python. Also you will need to generate (on the source machine) and install (on the destination machine) an ssh key beforehand so that the scp automatically gets authenticated with your public ssh key (in other words, so your script doesn't ask for a password). Below is the sample code to comply with your request:
- import os
- os.system("scp FILE USER@SERVER:PATH")
- #e.g. os.system("scp foo.bar joe@srvr.net:/path/to/foo.bar")
* [Linux 小學堂] SSH 免密碼登入
* 鳥哥的 Linux - 檔案異地直接複製: scp
* Python module - os: os.system(command)
方法二:
To do this in Python (i.e. not wrapping scp through subprocess.Popen or similar) with the Paramiko library, you would do something like this:
- import os
- import paramiko
- ssh = paramiko.SSHClient()
- ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
- ssh.connect(server, username=username, password=password)
- sftp = ssh.open_sftp()
- sftp.put(localpath, remotepath)
- sftp.close()
- ssh.close()
- ssh = paramiko.SSHClient()
- ssh.connect(server, username=username, password=password)
- ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd_to_execute)
沒有留言:
張貼留言