通过Paramiko连接到ssh服务器时,使用SSHClient
时阅读stdios非常容易。
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('127.0.0.1', username='rusername', password='password')
stdin, stdout, stderr = client.exec_command(cmd)
我可以通过Channel
访问使用过的client.get_transport()._channels
(可能有更好的方法吗?)
是否可以在此频道上打开更多管道?我在ssh服务器上调用Python脚本,所以我可以在服务器端脚本中打开fd进行编写。
在我完成的小测试中, Channel.makefile()
似乎与另一个管道/ fd无关。我可能会误解文档。
答案 0 :(得分:0)
要在远程服务器上打开文件(用于读取或写入),您可以使用open_sftp
:
sftp = client.open_sftp()
with sftp.open("/tmp/somefile", "w") as outfile:
outfile.write("my data")