The Artima Developer Community
Sponsored Link

Python Answers Forum
Python coding question ( os.chroot() )

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
TomK K

Posts: 3
Nickname: devyn
Registered: May, 2004

Python coding question ( os.chroot() ) Posted: Mar 26, 2006 7:44 PM
Reply to this message Reply
Advertisement
Hi,

I'm trying to return from a chrooted environment. 'sys.exit()' does exit but also seams to exit the program. (I just started looking into Python just a few days ago but I can't find anything close to what I want to do googling the web.) Any way to exit the chrooted environment and remain in the program to execute more commands?

The effect I need is this:

[root@lhs python]# ls
python.py
[root@lhs python]# chroot /home/devyn
bash-3.00# ls
bin dev etc home lib pass-devyn.txt root devyn-dir usr var
bash-3.00# exit
exit
[root@lhs python]# ls
python.py
[root@lhs python]#

Thanks,
Dev


# chroot
# except OSError, (errno, msg):

import os
import sys

root="/home/devyn/"

try:
if os.path.exists(root):
print "[0]The path '%s' exists. Going to 'chroot' to it... :)" % (root)
print "[0-0]FILES: %s " % (os.listdir('/'))
os.chroot (root)
else:
print "[0]Looks like we are already chrooted to '%s'. :P" % (root)
print "[0]CWD: %s" % (os.getcwd())
print "[0-1]FILES: %s " % (os.listdir('/'))
except Exception, e:
print "[0]Could not chroot"
print "[0]Problem: %s" %(e)

try:
if os.path.exists(root):
try:
os.chroot (root)
except Exception, e:
print "[1][WARNING] oOps! Problem in 'execWithRedirect' %s : %s : %s : %s : [EXCEPTION]%s" %(command, argv, root, newPgrp, e)
pass
else:
print "[1]Looks like we are already chrooted to '%s'. :P" % (root)


print "[1]CWD: %s" % (os.getcwd())
except Exception, e:
print "[1]Problem: %s" %(e)

try:
print "[3] ---------------------- BEFORE ------------------------ "
os.chroot ("/")
print "[3]CWD: %s" % (os.getcwd())
print "[3]FILES: %s " % (os.listdir('/'))
except Exception, e:
print "[3]Problem: %s" % (e)

try:
print "[4] ---------------------- AFTER ------------------------ "
sys.exit(0)
print "[4] Using _exit to exit 'chroot'ed environment."
os.fork()
print "[4]CWD: %s" % (os.getcwd())
print "[4]FILES: %s " % (os.listdir('/'))
except Exception, e:
print "[4]Problem: %s" % (e)


The output of the script above is as follows:

[root@lhs python]# python python.py
[0]The path '/home/devyn/' exists. Going to 'chroot' to it...
[0]CWD: /root/python
[1]Looks like we are already chrooted to '/home/devyn/'. :P
[1]CWD: /root/python
[3] ---------------------- BEFORE ------------------------
[3]CWD: /root/python
[3]FILES: ['home', '.canna', '.gtkrc', 'pass-devyn.txt', '.emacs', 'dev', '.xemacs', 'lib', '.bashrc', 'bin', 'devyn-dir', '.ed2k_gui', 'root', '.zshrc', 'usr', 'etc', '.bash_logout', '.bash_profile', 'var', '.kde']
[4] ---------------------- AFTER ------------------------
[4]Problem:
[root@lhs python]#

Topic: How do I call JUnit test cases from Python? Previous Topic   Next Topic Topic: LOGO in python?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use