0

我正在尝试将数据写入 chroot 环境中的文件。由于我是非 root 用户,因此我与 chroot 通信的唯一方法是使用 schroot 命令。

目前我正在使用以下技巧来写入数据。

$ schroot -c chroot_session -r -d /tmp -- bash -c "echo \"$text\" > file.txt"

但我敢肯定,如果文本有一些特殊字符、引号等,这会让我很伤心。所以有什么更好的方法将 $text 发送到 chroot。很可能我将通过 python 脚本使用上述命令。有没有更简单的方法?

4

2 回答 2

1

有点骇人听闻,但是……</p>

c = ConfigParser.RawConfigParser()
c.readfp(open(os.path.join('/var/lib/schroot/session', chroot_session), 'r'))
chroot_basedir = c.get(chroot_session, 'mount-location')
with open(os.path.join(chroot_basedir, '/tmp/file.txt'), 'w') as fp:
    fp.write(text)

好的,所以特权不会让您通过除 之外的任何方法进入schroot,对吧?

p = subprocess.Popen(['schroot', '-c', name, '-r', 'tee', '/tmp/file.txt'],
                     stdin=subprocess.PIPE,
                     stdout=open('/dev/null', 'w'),
                     stderr=sys.stderr)
p.stdin.write(text)
p.stdin.close()
rc = p.wait()
assert rc == 0
于 2011-02-02T06:38:04.540 回答
0

你可以使用python将$text写入文件(python有写入权限),
然后将该文件复制到file.txt

于 2011-02-02T06:37:08.687 回答