2

I trying to ssh to a server and then mount a workspace with sshfs on the server using paramiko.

The code I have:

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')

If I do it manually, I should be asked for the password of username on server.

However, here I am getting nothing from stdout:

>>> stdout.read()
b''

From stderr I am getting connection reset by peer

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

But after this, if I do "pwd", it will work. If I do "cd /" , it won't work.

Can someone help me here?

Thanks

luke wang
  • 21
  • 4

2 Answers2

0

you will have to open an interactive shell using invoke_shell. By using exec_command once the command finishes execution, the channel will be closed and cannot be reused

Anesh
  • 194
  • 1
  • 13
0

I figured it out. We have to create a channel through invoke_shell() and then use send and recv()

channel = client.invoke_shell()
channel.send()
channel.recv(9999)
luke wang
  • 21
  • 4