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'
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.
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.
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')
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.
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:
See:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With