Python script opens bash prompt ending script

I want to write a chroot shell in python. The script will copy some files, configure some other things, and then execute chroot and land in the chroot shell.

The tricky part is that I don't want any python processes to execute after I am in chroot.

In other words, python has to do the setup work, call chroot and exit, leaving me in the chroot shell. When I exit chroot, I should be in the directory where I was when I called the python script.

Is it possible?

+3
source share
2 answers

os.exec*. Python chroot ( , exec*).

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

( - )

+2

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
0

Source: https://habr.com/ru/post/1775073/


All Articles