Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? Should I be copying the file to a permissable space and chowning? Is there a way to give paramikko sudoer without using keys or having to mess around with 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:

  • http://www.lag.net/paramiko/docs/paramiko.SFTPClient-class.html#put
  • I'm trying to understand why I'm getting a "Permission Denied" error when using paramiko 1.7.6
  • 'Put' in SFTP using PAramiko
  • IOError: [Errno 13] Permission denied:
  • IOError: [Errno 13] Permission denied
  • Why am I getting IOError: [Errno 13] Permission denied?
  • Python - IOError: [Errno 13] Permission denied:
  • using shutil.copyfile I get a Python IOError: [Errno 13] Permission denied:
  • https://github.com/fabric/fabric/issues/257
  • https://github.com/fabric/fabric/issues/828
  • https://github.com/fabric/fabric/issues/257
  • http://code.activestate.com/recipes/576810-copy-files-over-ssh-using-paramiko/
  • How to run sudo with paramiko? (Python)

Some of the posts are sort of old, but seemed to indicate that paramiko doesn't have it implemented? Fabric has a version implemented, but I am not sure about adding more dependencies.

(Pdb) import pkg_resources
(Pdb) pkg_resources.get_distribution('paramiko').version
'1.13.0'
like image 861
jmunsch Avatar asked Apr 17 '14 22:04

jmunsch


People also ask

How to fix errno 13 permission denied in Python?

The PermissionError: [errno 13] permission denied error occurs when you try to access a file from Python without having the necessary permissions. To fix this error, use the chmod or chown command to change the permissions of the file so that the right user and/or group can access the file.

Can t open file errno 13 permission denied?

How to Fix the IOError: [Errno 13] Permission denied in Python. To fix this, you need to enter the right path to the file you want to access, not the folder. Let's say we have two files in the Test_folder . After providing the path of the file, the error is resolved.


2 Answers

I had the same error for 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

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')
like image 145
YanBir Avatar answered Oct 16 '22 16:10

YanBir


1) Cron task to move folders from /home/user/Desktop to /var/www

2) Login as 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.

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.

Edit dont do the above:

Change the permissions, and/or ownership

On linux: If you can ssh in:

ls -ld /path/to/location

to see who owns the directory and has r/w permissions.

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.

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.

See:

  • https://askubuntu.com/questions/6723/change-folder-permissions-and-ownership
  • https://askubuntu.com/questions/19898/whats-the-simplest-way-to-edit-and-add-files-to-var-www
like image 28
jmunsch Avatar answered Oct 16 '22 15:10

jmunsch