The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Parsing file, yaml file? Extracting specific sections

Discussion in 'cPanel Developers' started by prajithp13, Jun 13, 2012.

  1. prajithp13

    prajithp13 Member

    Joined:
    Jun 12, 2012
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    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
     
  2. KostonConsulting

    KostonConsulting Well-Known Member

    Joined:
    Jun 17, 2010
    Messages:
    255
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    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.
     
Loading...

Share This Page