如何在 Python 3.0 中建立 SSH 连接?我想在设置了无密码 SSH 的远程计算机上保存文件。
最佳答案
我建议将 ssh 作为子进程调用。它可靠且便携。
import subprocess
proc = subprocess.Popen(['ssh', 'user@host', 'cat > %s' % filename],
stdin=subprocess.PIPE)
proc.communicate(file_contents)
if proc.retcode != 0:
...
您不得不担心引用目标文件名。如果您想要更大的灵 active ,您甚至可以这样做:
import subprocess
import tarfile
import io
tardata = io.BytesIO()
tar = tarfile.open(mode='w:gz', fileobj=tardata)
... put stuff in tar ...
proc = subprocess.Popen(['ssh', 'user@host', 'tar xz'],
stdin=subprocess.PIPE)
proc.communicate(tardata.getvalue())
if proc.retcode != 0:
...
关于python - 与 Python 3.0 的 SSH 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/953477/