Question

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:

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'
Was it helpful?

Solution 2

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:

OTHER TIPS

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

I was getting the "permission denied" error on Windows myself. 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. 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. Changing the filename before put() fixed this for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top