Search code examples
pythonubuntusshvmwareparamiko

Access VM shared folder via ssh (python parametiko) not working


I have some trouble with parametiko...

Context : I am working on windows 10, and I have a virtual machine (ubuntu 20) installed using VMware. On my windows, I have a folder "ubuntu_shared" that I have shared with my VM. On my windows cmd line, if I run

ssh <user>@<host>
cd /mnt/hgfs/ubuntu_shared

it works fine.

But if I run with python the same process:

k = paramiko.RSAKey.from_private_key_file(LOCAL_SSH_PRIVATE_KEY)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

num_tries = 0
ssh_connected = False
while not ssh_connected and num_tries < 10:
    try:
        ssh.connect(hostname=VM_HOST, username=VM_USER, allow_agent=False, pkey=k)
        ssh_connected = True
        print(f"connected via ssh to {VM_USER}@{VM_HOST}")
    except Exception as e:
        print("still trying...")
        num_tries += 1
        time.sleep(1)
if not ssh_connected:
    ssh.close()
    raise Exception(f"failed to connect : {e}")

command = "cd /mnt/hgfs/ubuntu_shared"
stdin, stdout, stderr = ssh.exec_command(command)
if stderr:
    print(stderr.read().decode())

which outputs :

sh: line 0: cd: /mnt/hgfs/ubuntu_shared: Not a directory

What am I doing wrong on python ? doesn't seem like it's different is it ? Thanks in advance :)


Solution

  • OK I actually found a way to fix the issue so I'll answer my own question if ever it can help someone.

    It seem that when using paramiko I need to set the permissions to the wanted folder first.

    Adding this once the ssh is connected fixed it for me :

    sftp_client = ssh.open_sftp()
    sftp_client.chmod(r"/mnt/hgfs/ubuntu_shared", 7700)