Python 脚本打开 bash 提示符并终止脚本

发布于 2024-10-02 10:30:28 字数 253 浏览 0 评论 0原文

我想用 python 编写一个 chroot 包装器。该脚本将复制一些文件,设置一些其他内容,然后执行 chroot,并且应该让我进入 chroot shell。

棘手的部分是我不希望在进入 chroot 后运行任何 python 进程。

换句话说,python 应该完成设置工作,调用 chroot 并终止自身,让我处于 chroot shell 中。当我退出 chroot 时,我应该位于调用 python 脚本时所在的目录中。

这可能吗?

I want to write a chroot wrapper in python. The script will be copying some files, setting up some other stuff and then executing chroot and should land me in a chroot shell.

The tricky part is that I want no python processes running after I am in the chroot.

In other words, python should do the setup work, call chroot and terminate itself, leaving me in a chroot shell. When I exit the chroot, I should be in a directory where I was when I invoked the python script.

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百善笑为先 2024-10-09 10:30:28

我的第一个想法是使用 os.exec*函数。这些将用 chroot 进程(或者您决定使用 exec* 运行的任何进程)替换 Python 进程。

# ... do setup work
os.execl('/bin/chroot', '/bin/chroot', directory_name, shell_path)

(或类似的东西)

My first thought would be to use one of the os.exec* functions. These will replace the Python process with the chroot process (or whatever you decide to run with exec*).

# ... do setup work
os.execl('/bin/chroot', '/bin/chroot', directory_name, shell_path)

(or something like that)

贱贱哒 2024-10-09 10:30:28

或者,您可以为 popen 命令使用一个新线程,以避免阻塞主代码,然后将命令结果传回。

import popen2
import time
result = '!' 
running = False

class pinger(threading.Thread):
    def __init__(self,num,who):
        self.num = num
        self.who = who
        threading.Thread.__init__(self)

    def run(self):
        global result
        cmd = "ping -n %s %s"%(self.num,self.who)
        fin,fout = popen2.popen4(cmd)
        while running:
            result = fin.readline()
            if not result:
                break
        fin.close()

if __name__ == "__main__":
    running = True
    ping = pinger(5,"127.0.0.1")
    ping.start()
    now = time.time()
    end = now+300
    old = result
    while True:
        if result != old:
            print result.strip()
            old = result
        if time.time() > end:
            print "Timeout"
            running = False
            break
        if not result:
            print "Finished"
            break

Alternatively, you can use a new thread for the popen command to avoid blocking the main code then pass the command results back.

import popen2
import time
result = '!' 
running = False

class pinger(threading.Thread):
    def __init__(self,num,who):
        self.num = num
        self.who = who
        threading.Thread.__init__(self)

    def run(self):
        global result
        cmd = "ping -n %s %s"%(self.num,self.who)
        fin,fout = popen2.popen4(cmd)
        while running:
            result = fin.readline()
            if not result:
                break
        fin.close()

if __name__ == "__main__":
    running = True
    ping = pinger(5,"127.0.0.1")
    ping.start()
    now = time.time()
    end = now+300
    old = result
    while True:
        if result != old:
            print result.strip()
            old = result
        if time.time() > end:
            print "Timeout"
            running = False
            break
        if not result:
            print "Finished"
            break
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文