【问题标题】:Can I ssh to a server and then do sshfs on the server with paramiko?我可以 ssh 到服务器,然后使用 paramiko 在服务器上执行 sshfs 吗?
【发布时间】:2015-04-23 23:52:50
【问题描述】:

我尝试通过 ssh 连接到服务器,然后使用 paramiko 在服务器上使用 sshfs 安装工作区。

我的代码:

import sys
sys.stderr = open('/dev/null')
import paramiko as pm
sys.stderr = sys.__stderr__
import os

class AllowAllKeys(pm.MissingHostKeyPolicy):
    def missing_host_key(self, client, hostname, key):
        return

HOST = '172.29.121.238'
USER = 'root'
PASSWORD = 'test'

client = pm.SSHClient()
client.load_system_host_keys()
client.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
client.set_missing_host_key_policy(AllowAllKeys())
client.connect(HOST,username=USER,password=PASSWORD)

stdin,stdout,stderr = client.exec_command('sshfs username@server:/source_path /destination_path')

如果我手动操作,我应该被要求输入服务器上的用户名密码。

但是,在这里我没有从标准输出中得到任何信息:

>>> stdout.read()
b''

从 stderr 我得到对等方重置连接

>>> stderr.read()
b'read: Connection reset by peer\n'

但在此之后,如果我执行“pwd”,它将起作用。如果我执行 "cd /" ,它将不起作用。

有人可以帮我吗?

谢谢

【问题讨论】:

    标签: python ssh paramiko sshfs


    【解决方案1】:

    您必须使用invoke_shell 打开一个交互式shell。通过使用exec_command,一旦命令执行完毕,通道将被关闭,无法重用

    【讨论】:

      【解决方案2】:

      我想通了。我们必须通过invoke_shell()创建一个通道,然后使用send和recv()

      channel = client.invoke_shell()
      channel.send()
      channel.recv(9999)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-10
        • 1970-01-01
        • 1970-01-01
        • 2012-08-30
        • 1970-01-01
        • 2011-01-25
        相关资源
        最近更新 更多