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'