作者:高度的寂寞 | 来源:互联网 | 2023-06-19 17:42
问题:我可以这样做:self.sftp.put(sourceFilePath,final_destination,use_sudo = True)我可以制作文件夹,但不能制作文件?我是否需要在para
问题:
我可以这样做:
self.sftp.put(sourceFilePath,final_destination,use_sudo = True)
我可以制作文件夹,但不能制作文件?我是否需要在paramiko中明确调用sudo或设置一些内容?我应该将文件复制到允许的空间和chowning吗?有没有办法让paramikko sudoer不使用键或不得不乱用ssh.exec_command(“sudo mv”)?我错过了什么?
代码:
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= listingSkufinal_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
追溯:
# 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
我看过的其他帖子:
> 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)
有些帖子有些陈旧,但似乎表明paramiko没有实现它? Fabric已实现版本,但我不确定是否添加了更多依赖项.
(Pdb) import pkg_resources
(Pdb) pkg_resources.get_distribution('paramiko').version
'1.13.0'
解决方法:
1)Cron任务将文件夹从/ home / user / Desktop移动到/ var / www
2)以root用户身份登录. (明显的安全问题)
在主机服务器上运行sudo passwd root后,我现在可以使用root将文件传输到/ var / www.
我还添加了用户到www-data并递归地追踪文件和目录,但我认为设置root密码就可以了.
编辑不要做上述事情:
更改权限和/或所有权
在linux上:如果你可以ssh:
ls -ld / path / to / location
查看谁拥有该目录并具有r / w权限.
bob@bob-p7-1298c:~$ls -ld /var/www
drwxr-xr-x 3 root root 4096 Sep 24 10:39 /var/www
然后看看使用:
> usermod
> addgroup
> useradd
> chown
> chmod
给用户r / w权限.
这可以通过以下方式完成:
>更改谁拥有该目录
>将用户添加到目录组
>创建新组并更改目录上的组
>改变主人
>更改所有者,组或公共的r / w权限.
看到:
> 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