Skip to content

Instantly share code, notes, and snippets.

@SeqviriouM
Created October 24, 2013 15:00
Show Gist options
  • Save SeqviriouM/7138790 to your computer and use it in GitHub Desktop.
Save SeqviriouM/7138790 to your computer and use it in GitHub Desktop.
Homework_2_Runlevel
import re,tarfile
runlevel = re.compile("env DEFAULT_RUNLEVEL=\d*$")
initdefault = re.compile(".*:initdefault:.*$")
# Open archive
tar_archive = tarfile.open("etc.tar.gz", "r:gz")
# Extract necessary file
file = tar_archive.extractfile("etc/init/rc-sysinit.conf")
# Try to open file inittab
try:
init_file = tar_archive.extractfile('etc/inittab')
init = True
except Exception:
init = False
# Variable to safe runlevel
level = 0
if (init):
# If file inittab exists
for line in init_file:
if initdefault.search(line) != None:
level = line[3]
else:
# Search in file rc-sysinit.conf
for line in file:
if runlevel.search(line) != None:
level = line[len(line)-2]
# Print result
print str(level)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment