简体   繁体   English

Paramiko Python:IOError:[Errno 13] 权限被拒绝

[英]Paramiko Python: IOError: [Errno 13] Permission denied

The Question:问题:

Can I do something like:我可以做这样的事情:

self.sftp.put(sourceFilePath, final_destination, use_sudo=True)

I can make folders, but not files?我可以创建文件夹,但不能创建文件? Do I need to explicitly call sudo or set something in paramiko?我是否需要明确调用 sudo 或在 paramiko 中设置一些东西? Should I be copying the file to a permissable space and chowning?我应该将文件复制到允许的空间并进行 chowning 吗? Is there a way to give paramikko sudoer without using keys or having to mess around with ssh.exec_command("sudo mv") ?有没有办法让 paramikko sudoer 不使用密钥或不必弄乱ssh.exec_command("sudo mv") What am I missing?我错过了什么?

The Code:编码:

class Ssh(object):

    def __init__(self):
        super(Ssh, self).__init__()

    def setup(self):
        '''Setup connection'''
        try:
            # DEBUG
            paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG)
            #set username & password
            username = 'sgdevbox'
            password = MainFrame.ssh_pass
            host = '192.168.0.170'
            port = 22
            self.transport = paramiko.Transport((host, port))
            self.transport.connect(username = username, password = password)
            self.sftp = paramiko.SFTPClient.from_transport(self.transport)
            print(self.sftp.sock)
        except Exception, e:
            print(traceback.format_exc())

    def putFiles(self, sources, listingSku):
        '''
        Upload images to server along with all currentItemInfo, plus initials and date
        Basically build the auction and put it into the queue for verification
        '''
        print('\n# Ssh.putFiles() #')
        if isinstance(sources, unicode):
            sources = {sources,'True'}
        try:
            self.setup()
            destination = '/var/www'
            cwd = os.getcwd()
            for source in sources:
                filename = os.path.split(source)[-1]
                destinationFolder = listingSku
                final_path = posixpath.join(destination,destinationFolder)

                try:
                    self.sftp.mkdir(final_path, mode=777)
                except:
                    print(traceback.format_exc())
                final_destination = posixpath.join(final_path, filename)
                sourceFilePath = os.path.join(cwd,source)
                print('\n# Source Path: {}\n# Destination Path: {}\n\n'.format(sourceFilePath,final_destination))
                self.sftp.put(sourceFilePath, final_destination)
        except Exception, e:
            print(traceback.format_exc())
        return

The Traceback:追溯:

# Source Path: C:\A\Long\Path\622-402_01.JPEG
# Destination Path: /var/www/WOOBLE-WAMBLER-SPAM-1235/622-402_01.JPEG


DEBUG:paramiko.transport.sftp:[chan 1] open('/var/www/WOOBLE-WAMBLER-SPAM-1235/622-402_01_swatch.JPEG', 'wb')
Traceback (most recent call last):
  File "display_image.py", line 67, in putFiles
    self.sftp.put(sourceFilePath, final_destination)
  File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 565, in put
    fr = self.file(remotepath, 'wb')
  File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 245, in open
    t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
  File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 635, in _request
    return self._read_response(num)
  File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 682, in _read_response
    self._convert_status(msg)
  File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 710, in _convert_status
    raise IOError(errno.EACCES, text)
IOError: [Errno 13] Permission denied

Other posts I've looked at:我看过的其他帖子:

Some of the posts are sort of old, but seemed to indicate that paramiko doesn't have it implemented?有些帖子有点旧,但似乎表明 paramiko 没有实现它? Fabric has a version implemented, but I am not sure about adding more dependencies. Fabric 实现了一个版本,但我不确定是否要添加更多依赖项。

(Pdb) import pkg_resources
(Pdb) pkg_resources.get_distribution('paramiko').version
'1.13.0'

1) Cron task to move folders from /home/user/Desktop to /var/www 1) 将文件夹从 /home/user/Desktop 移动到 /var/www 的 Cron 任务

2) Login as root. 2) 以 root 身份登录。 ( obvious security issues ) (明显的安全问题)

After running sudo passwd root on the host server I am now able to transfer files to /var/www with root.在主机服务器上运行sudo passwd root ,我现在可以使用 root 将文件传输到 /var/www。

I had also added user to www-data And recursively chowned the files and directories, but I think setting up a root password did the trick.我还将用户添加到 www-data 并递归地 chown 文件和目录,但我认为设置 root 密码可以解决问题。

Edit dont do the above:编辑不要执行上述操作:

Change the permissions, and/or ownership更改权限和/或所有权

On linux: If you can ssh in:在 linux 上:如果您可以通过 ssh 登录:

ls -ld /path/to/location

to see who owns the directory and has r/w permissions.查看谁拥有该目录并具有 r/w 权限。

bob@bob-p7-1298c:~$ ls -ld /var/www
drwxr-xr-x 3 root root 4096 Sep 24 10:39 /var/www

Then look into using:然后看看使用:

  • usermod
  • addgroup
  • useradd
  • chown
  • chmod

to give the user r/w permissions.授予用户 r/w 权限。

This can be done by:这可以通过以下方式完成:

  • changing who owns the directory更改谁拥有目录
  • adding a user to the group of the directory将用户添加到目录组
  • creating a new group and changing the group on the directory创建一个新组并更改目录中的组
  • changing the owner改变所有者
  • Changing the r/w permissions of owner,group, or public.更改所有者、组或公共的 r/w 权限。

See:看:

I had the same error for sftp.get我对 sftp.get 有同样的错误

I tried to do:我试图做:

sftp.get('/remote_server/readme.txt', localpath='C:\\Users\\user1\\Desktop')

Got the error above: [Errno 13] Permission denied得到上面的错误:[Errno 13] 权限被拒绝

The fix is that we need to specify the whole path include the file name.解决方法是我们需要指定包含文件名的整个路径。

sftp.get('/remote_server/readme.txt', localpath='C:\\Users\\user1\\Desktop\\readme.txt')

I was getting the "permission denied" error on Windows myself.我自己在 Windows 上收到“权限被拒绝”错误。 My code looked like this -我的代码看起来像这样 -

sftp.put(sftp_local_path + "\\filename.txt", sftp_remote_path)

Then I changed it like below -然后我像下面这样改变它 -

with pysftp.Connection(host=sftp_host,username=sftp_user_id, password=sftp_pwd, port=sftp_port, cnopts=cnopts ) as sftp:
    with sftp.cd(sftp_remote_path):
        sftp.put(sftp_local_path + "\\filename.txt")

and I was able to upload the file successfully.我能够成功上传文件。

I had the same error with a very similar traceback when using pysftp to put() a file.使用 pysftp 来 put() 文件时,我遇到了相同的错误和非常相似的回溯。 It turned out I was trying to put() a file that had the same filename as a file that already existed in that sftp directory.结果我试图 put() 一个文件,该文件与该 sftp 目录中已经存在的文件具有相同的文件名。 Changing the filename before put() fixed this for me.在 put() 之前更改文件名为我解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM