Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将文件写入chroot环境_Python_Linux_Shell_Chroot - Fatal编程技术网

Python 将文件写入chroot环境

Python 将文件写入chroot环境,python,linux,shell,chroot,Python,Linux,Shell,Chroot,我正在尝试将数据写入chroot环境中的文件。因为我不是root用户,所以我和chroot通信的唯一方法就是使用schroot命令 目前,我正在使用以下技巧来编写数据 $ schroot -c chroot_session -r -d /tmp -- bash -c "echo \"$text\" > file.txt" 但我相信,如果文本有一些特殊字符、引号等,这会让我非常难过。那么,有什么更好的方法可以将$text发送到chroot呢。我很可能会通过python脚本使用上述命令。有更

我正在尝试将数据写入chroot环境中的文件。因为我不是root用户,所以我和chroot通信的唯一方法就是使用schroot命令

目前,我正在使用以下技巧来编写数据

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

但我相信,如果文本有一些特殊字符、引号等,这会让我非常难过。那么,有什么更好的方法可以将$text发送到chroot呢。我很可能会通过python脚本使用上述命令。有更简单的方法吗?

您可以使用python将$text写入文件(python有权写入),
然后将该文件复制到file.txt,这有点像黑客,但是

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

我想到了。但我是非特权用户。我无法写入/var/lib/schroot/sessions:(.我在你的答案中遗漏了什么吗?超级答案。这正是我想要的,某种管道技术。非常感谢。我如何将文件从父环境复制到Chroot?我是非特权用户,这就是使用schroot的全部目的。