Найти - Пользователи
Полная версия: Помогите составить регексп для парсинга конфига ISPmanager
Начало » Data Mining » Помогите составить регексп для парсинга конфига ISPmanager
1
agryn
Есть конфиг ISPmanager
DbServer "MySQL" {
	Owner __Owner__
	AccessType allow
	ChangePassword 2012-07-23
	Hostname localhost
	Password qXb8fMwe
	SockPath /var/lib/mysql/mysql.sock
	Type mysql
	User root
}
Регулярку пробую начать хоть так:
re.search(r'DbServer\s"MySQL"\s+
\{
(\s+.+\n)*
\}', data, re.VERBOSE))
Что то подцепило но все равно не то. В идеале нужно извлечь значения ключей Hostname, Password, Type, User
порядок ключей может быть разным. В принципе если кому известно что за формат конфига (и какую либу юзать) тоже буду благодарен.
Вот пример большей части конфига сначала конфига:
Option NoMysqlQuota
#Option DisableSecurePhpBin
Option LogLevel 9
DNS named
Mirror http://ru.download.ispsystem.com/
extaction mindterm /mancgi/mindterm
ip 194.44.0.219 shared _
ip 194.44.0.229 shared _
PluginSource http://download.ispsystem.com/plugins/ispmgr_plugins.xml
PluginSource http://usings.ru/isp_plugins/ispmgr_plugins.xml
path db4  /usr/bin/db_dump -p /etc/sasldb2
path DefaultHomeDir  /var/www
ApacheVersion  2
Web  web
WebModules  apache
path httpd.conf  /etc/httpd/conf/httpd.conf
path httpd  /usr/sbin/apachectl
path apachectl  /usr/sbin/apachectl start
Service "HTTP" {
	Confname httpd
	Monitored NO
	Process httpd
	Start /etc/rc.d/init.d/httpd start
	Stop /etc/rc.d/init.d/httpd stop
	Type www
}
Service "FTP" {
	Confname proftpd
	Monitored NO
	Process proftpd
	Start /etc/rc.d/init.d/proftpd start
	Stop /etc/rc.d/init.d/proftpd stop
	Type ftp
}
Service "POP3" {
	Confname dovecot
	Monitored NO
	Process dovecot
	Start /etc/rc.d/init.d/dovecot start
	Stop /etc/rc.d/init.d/dovecot stop
	Type pop3
}
Service "MySQL" {
	Confname mysqld
	Monitored NO
	Process mysqld
	Start /etc/rc.d/init.d/mysqld start
	Stop /etc/rc.d/init.d/mysqld stop
	Type mysql
	Ip /var/lib/mysql/mysql.sock
}
Service "DNS" {
	Confname named
	Monitored NO
	Process named
	Start /etc/rc.d/init.d/named start
	Stop /etc/rc.d/init.d/named stop
	Type dns
}
ApacheProcName  httpd
ApacheUid  48
ApacheGid  48
Option Agree
path genericstable  /etc/mail/generic
path accessdb  /etc/mail/access
path php-configs  /etc/php.d
path phpbinary  /usr/bin/php-cgi
path php.ini  /etc/php.ini
path php-extensions  /usr/lib64/php/modules
path dovecot.passwd  /etc/dovecot/dovecot.passwd
path dovecotpw-bin  /usr/bin/doveadm pw
POP3  dovecot
path iptables  /etc/sysconfig/iptables
DbServer "MySQL" {
	Owner __Owner__
	AccessType allow
	ChangePassword 2012-07-23
	Hostname localhost
	Password qXb8fMwe
	SockPath /var/lib/mysql/mysql.sock
	Type mysql
	User root
}
SupportCenterServer  MySQL
py.user.next
#!/usr/bin/env python3
 
import re
 
with open('file.txt') as fin:
    text = fin.read()
 
pat_sec = r'(\S+) "([^"]+)" {([^}]+)}'
pat_kv = r'(\S+)\s+(\S+)'
 
seclst = re.findall(pat_sec, text)
 
for stype, sname, sbody in seclst:
 
    blst = re.findall(pat_kv, sbody)
 
    sectype = 'dbserver'
    secname = 'mysql'
    seckeys = ('hostname',
               'password',
               'type',
               'user')
 
    if stype.lower() == sectype and \
       sname.lower() == secname:
 
        print(stype, sname)
        for i in blst:
            print(i)
 
        bodydct = {k.lower(): v for k, v in blst}
        for k in seckeys:
            print(k, '->', bodydct[k])

[guest@localhost reconf]$ ./reconf.py 
DbServer MySQL
('Owner', '__Owner__')
('AccessType', 'allow')
('ChangePassword', '2012-07-23')
('Hostname', 'localhost')
('Password', 'qXb8fMwe')
('SockPath', '/var/lib/mysql/mysql.sock')
('Type', 'mysql')
('User', 'root')
hostname -> localhost
password -> qXb8fMwe
type -> mysql
user -> root
[guest@localhost reconf]$
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB