Skip to content

Instantly share code, notes, and snippets.

@Kanst
Created October 24, 2013 11:02
Show Gist options
  • Save Kanst/7135168 to your computer and use it in GitHub Desktop.
Save Kanst/7135168 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
#! -*- coding: utf-8 -*-
import re, tarfile
tar = tarfile.open('etc.tar.gz', 'r:gz')
# Открываем rc-sysinit.conf
rc_file = tar.extractfile('etc/init/rc-sysinit.conf').readlines()
# Пытаемся открыть inittab
try:
tab_file = tar.extractfile('etc/inittab').readlines()
find_inittab = True
except:
find_inittab = False
# Шаблоны для поиска runlevel
tf = re.compile('^[^#][^:]*:([0-6sS]):initdefault:')
ff = re.compile('^env.*(?<=\t| )DEFAULT_RUNLEVEL=\d$')
# Поиск runlevel в rc-sysinit.conf
for x in rc_file:
if re.match(ff,x):
runlevel = x.split('=')[1]
# Поиск в inittab, если он есть
if find_inittab:
for x in tab_file:
if re.match(tf,x) != None:
runlevel = re.search(tf,x).groups()[0]
tar.close()
out_file =open('output.txt', 'w')
out_file.write(str(runlevel))
out_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment