【问题标题】:mount via paramiko fails "No such file or directory"通过 paramiko 挂载失败“没有这样的文件或目录”
【发布时间】:2020-08-07 02:36:01
【问题描述】:

我正在使用 python 的 paramiko 来操作访问远程 linux 机器。我的命令“挂载设备目录”失败并显示“没有这样的文件或目录”,即使我远程使用它后完全相同的命令成功(通过 ssh 连接,而不是通过 paramiko)。

我尝试将 /etc/fstab 更改为某些值,同样的情况。一旦我通过 ssh 键入它 - 好的,通过 paramiko 的相同命令 - 上面的错误消息。

有什么想法吗?

命令示例(从原点更改最少):

        import paramiko
        self.ssh = paramiko.SSHClient()
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.ssh.connect('192.168.1.1', username='root', password='passwd')
        stdin, stdout, stderr = self.ssh.exec_command("/bin/mount /dev/sda1")

给我一​​个错误:

 mount /dev/sda1 failed: mount: mounting /dev/sda1 on /media/card failed: No such file or directory

/etc/fstab 中的内容:

/dev/sda1       /media/card          vfat      fmask=0000,dmask=0000  0  0

当然,/media/card 目录是存在的。再次,我可以通过 ssh 手动使用上述命令,它按预期工作。

更新。 同时我尝试了python的fabric库(建立在paramiko上),正如Python - How do I authenticate SSH connection with Fabric module?中描述的那样

c = fabric.Connection(host = '192.168.1.1', user = "root", connect_kwargs={'password': 'passwd'})  
c.run("/bin/mount /dev/sda1")  

直接给我与 paramiko 完全相同的错误消息。

更新2。好吧,作为一种解决方法,我使用直接 ssh 调用安装驱动器,如下面的 cmets 建议。在我完成任何必要的代码后,我尝试使用“正常”paramiko 调用卸载驱动器:

self.ssh.exec_command("/bin/umount /dev/sda1")

它有效。所以现在我完全迷路了,上面的安装失败了,但卸载正在工作。这真的很奇怪..

更新3。我尝试将 LD_LIBRARY_PATH 额外设置为安装库的位置,它需要 libm.so.6 和 libc.so.6,两者都位于 /lib 中,例如:

self.ssh.exec_command("export LD_LIBRARY_PATH=/lib:/usr/lib && /bin/mount /dev/sda1")

还是没有成功。

【问题讨论】:

  • 你能分享命令和你的 paramiko 代码吗?
  • 为了清楚起见,我在 /etc/fstab 中添加了最小示例以及错误和行
  • 我对@9​​87654329@ 没有足够的经验来给出明确的答案。 --- 但很可能根本原因与这里相同:Getting “sh: sesu: not found” error, when trying to run sesu command using Python Paramiko exec_command --- 测试做ssh root@192.168.1.1 mount /dev/sda1
  • 我不是指mount 的路径(您的编辑/bin/mount)——我指的是一般的配置文件的效果。您是否按照我的建议测试了ssh 命令?
  • 即使您的新代码显示 IP 192.168.1.1,而您的 ssh 测试使用 192.168.201.220

标签: python paramiko


【解决方案1】:

我能够让它工作(初稿。另外,我是 python 新手)。无论如何,这是我的代码片段。

对我来说最大的问题是,Windows 主机名中的反斜杠似乎有 4->1 的要求。

确保首先从 Windows PC 共享。在这种情况下,我的计算机/共享名称是“COMP_NAME/SHARE_NAME” 提供的用户名/密码是您访问共享的窗口凭据。

import sys
import paramiko
import constant


### START ###############################################################################
# connect to a GW device
# GW: hostname to connect to
# return: client connection object 
def connectToClient(GW):

    try:
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(GW, username=constant.GW_USER, password=constant.GW_PASS)
    except:
        print("Unexpected error:", sys.exc_info()[0])
        return None

    return client
### END ################################################################################


### START ###############################################################################
# execute a command on the remote device
# client: client connection object to the GW
# cmd: the command to execute
#   eg. 'ls -l'
# return: nothing (TODO: maybe return error info)
def exec(client, cmd):
    stdin, stdout, stderr = client.exec_command(cmd)

    for line in stdout:
        print(line.strip('\n'))

    #for line in stderr:
    #    print(line.strip('\n'))

    return
### END #################################################################################

# other stuff
# .
# .
# .

##########################################
# Start - upload the self extracting file to the GW
##########################################
#create the mount point
exec(client, "sudo mkdir /mnt/remote_files")

#mount the source directory (4 to 1 for the back slash chars in the UNC address ...)
exec(client, "sudo mount -t cifs -o username=oxxxxxxp,password=cxxxxxxxxx0 \\\\\\\\COMP_NAME\\\\SHARE_NAME /mnt/remote_files")

#copy the script file
exec(client, "cp /mnt/remote_files/selfextract.bsx rtls/scripts/selfextract.bsx")

#unmount the remote source
exec(client, "sudo umount /mnt/remote_files")

##########################################
# Done - upload the self extracting file to the GW
##########################################

# other stuff
# .
# .
# .

希望这对某人有所帮助..

拍拍

【讨论】:

    猜你喜欢
    • 2013-01-13
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多