Parsing file, yaml file? Extracting specific sections

prajithp13

Member
Jun 12, 2012
20
0
1
India
cPanel Access Level
Root Administrator
Here is a data file, which I believe is in YAML. I am trying to retrieve just the "sub_domains" section, which doesnt seem to be as easy as I had originally thought. Any help on this would be greatly appreciated!! I have been trying to do this in perl, but its failed.

This is actually from /var/cpanel/userdata/username/main


Code:
---
addon_domains:
somedomain.com: mysubdomain.primarydomain.com
somedomain2.com: mysubdomain2.primarydomain.com
somedomain3.com: mysubdomain3.primarydomain.com
cp_php_magic_include_path.conf: 0
main_domain: primarydomain.com
parked_domains: []

sub_domains:
- mysubdomain.primarydomain.com
- mysubdomain2.primarydomain.com
- mysubdomain3.primarydomain.com
 

KostonConsulting

Well-Known Member
Verifed Vendor
Jun 17, 2010
255
1
68
San Francisco, CA
cPanel Access Level
Root Administrator
cPanel has a set of functions built in to load configuration files. To load up a cPanel based YAML datastore, use Cpanel::CachedDataStore::loaddatastore like so:

Code:
my $username = 'someuser';
my $configFile = '/var/cpanel/userdata/' . $username . '/main';
my $configData = Cpanel::CachedDataStore::loaddatastore( $configFile, 0 );
Then, you can access the different parts of $configData->{data} to get your variables:

Code:
#subdomains:
my @subdomains = $configData->{data}->{sub_domains};
#parked domains:
my @parked_domains = $configData->{data}->{parked_domains};
Though, if you're looking to gather this data, there's probably a better way to get what you want from within cPanel's code base. Look around in /usr/local/cpanel/Cpanel and you'll find tons of useful modules.