Python 脚本不处理 lftp 和 sftp 并调用实用程序



PYTHONG脚本的一部分:

try:
print('Enter remote FTP user password:')
self.sudo_call(['lftp','sftp://{}@{}'.format(username, address),'-e','"put','/home/rfts/.ssh/id_rsa.pub"'],
user='rfts',
stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as ex:
output = ex.output.decode('utf-8')
raise Exception('Could not copy public key to remote user: '
'{}'.format(output))

运行时,它会在/var/log/secure 中创建这个

Nov 25 15:07:02 owl-pc104-red sudo:      iao : TTY=pts/1 ; PWD=/Owl/menus ; USER=rfts ; COMMAND=/usr/bin/lftp sftp://sftpuser5@192.168.11.153 -e "put /home/rfts/.ssh/id_rsa.pub"

完全使用安全日志中的那个可以:

/usr/bin/lftp sftp://sftpuser5@192.168.11.153 -e "put /home/rfts/.ssh/id_rsa.pub"

但是,pythong 脚本不起作用。它要求输入 SFTP 密码,但随后给出错误:

"Could not copy public key to remote user: File name missed. Try 'help put' for more information."

这是sudo_call方法

def sudo_call(self, call_list, *args, capture=True,
user=None, group=None, **kwargs):
self.cache_sudo()
command = ['sudo']
# Run as the input user and group if specified.
if user is not None:
command.extend(['-u', user])
if group is not None:
command.extend(['-g', group])
method = subprocess.check_output if capture else subprocess.check_call
return method(command + call_list, *args, **kwargs)

我对你的sudo_call方法是什么样子做了一些假设,但问题似乎在于你如何在这里拆分命令行:

self.sudo_call(['lftp','sftp://{}@{}'.format(username, address),'-e','"put','/home/rfts/.ssh/id_rsa.pub"'],
user='rfts',
stderr=subprocess.STDOUT)

您已将"put /home/rfts/.ssh/id_rsa.pub"拆分为两个参数:

  • '"put'
  • '/home/rfts/.ssh/id_rsa.pub"'

如果要在命令行上执行相同的操作,它将如下所示:

/usr/bin/lftp sftp://sftpuser5@192.168.11.153 -e '"put ' '/home/rfts/.ssh/id_rsa.pub"'

您可能会遇到相同的错误。 您需要将命令作为单个参数传递给 lftp(并且需要避免双引号(:

self.sudo_call(['lftp','sftp://{}@{}'.format(username, address),'-e','put /home/rfts/.ssh/id_rsa.pub'],
user='rfts',
stderr=subprocess.STDOUT)

相关内容

  • 没有找到相关文章

最新更新