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.

Parse yaml using perl

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

  1. prajithp13

    prajithp13 Member

    Joined:
    Jun 12, 2012
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    India
    cPanel Access Level:
    Root Administrator
    I am trying to developing some whm/cpanel plugin for my company, but I need to parse some of your default yaml configuration file using a perl script, I am not fully qualified in perl, so can you please assist me to parse yaml in perl. If you can provide me a sample perl script for this?
     
  2. Mario-cPanel

    Mario-cPanel Administrator
    Staff Member

    Joined:
    Oct 4, 2007
    Messages:
    72
    Likes Received:
    50
    Trophy Points:
    8
    Location:
    Houston, Texas, United States
    cPanel Access Level:
    Website Owner
    With the help of our product evangelist Infopro :) we were able to track down the following helpful information to get you started in the right direction:

    This link to our documentation should be helpful:
    Abstract | Standardized Hooks

    More in depth docs on same topic here:
    Basic Usage | Standardized Hooks

    A post by one of our Technical Product Specialist with useful links as well:
    http://forums.cpanel.net/f42/example-custom-cpanel-module-257271.html

    We don't have any sample perl scripts that we are able to provide. The above information will get you headed in the right direction with what your looking to accomplish.
     
  3. cPanelKenneth

    cPanelKenneth cPanel Development
    Staff Member

    Joined:
    Apr 7, 2006
    Messages:
    4,461
    Likes Received:
    22
    Trophy Points:
    38
    cPanel Access Level:
    Root Administrator
    A quick example (not tested):

    Code:
    #!/usr/bin/perl
    
    use YAML::Syck;
    use strict;
    use warnings;
    use Data::Dumper;
    
    my $file_path = '/var/cpanel/userdata/user/main';
    
    if( -e $file_path && ! -z _ ){
        my $data = Load( $file_path );
        print Dumper $data;
    }
    else{
        print "File does not exist or is empty\n";
    }
    
    You should consult the documentation for YAML and YAML::Syck:

    YAML::Syck - search.cpan.org
    YAML - search.cpan.org
     
  4. 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
    I'd definitely recommend using YAML::Syck. Here's a quick example with Cpanel::PublicAPI:

    Code:
    #!/usr/bin/perl
    
    #add /usr/local/cpanel/ to module search path
    BEGIN{ unshift(@INC, '/usr/local/cpanel'); }
    
    use strict;
    use Cpanel::PublicAPI ();
    use Data::Dumper ();
    use IPC::Open3 ();
    use JSON::Syck ();
    
    #Ensure access hash exists
    my $theHash;
    if ( !$ENV{'REMOTE_USER'} || $ENV{'REMOTE_USER'} eq 'root' ) {
        $theHash = "/root/.accesshash";
    }
    else {
        $theHash = $ENV{'HOME'} . "/.accesshash";
    }
    
    unless ( -f $theHash ) {
        my $pid = IPC::Open3::open3( my $wh, my $rh, my $eh, '/usr/local/cpanel/whostmgr/bin/whostmgr setrhash' );
        waitpid( $pid, 0 );
    }
    
    #get accesshash
    open( my $hash_fh, "<", $theHash ) || die "Cannot open access hash: " . $theHash;
    $accesshash = do { local $/; <$hash_fh>; };
    close($hash_fh);
    $accesshash =~ s/\n//g;
    
    my $func = 'listaccts';
    my $username = 'root';
    
    my $publicAPI = Cpanel::PublicAPI->new(
                usessl     => 1,
                user       => $username,
                accesshash => $accesshash
    );
    
    my $response = $publicAPI->whm_api( $func, $formdata, 'json' );
    
    #as easy as calling Load() on JSON string
    my $jsonObj = JSON::Syck::Load($response);
    
    print "JSON OBJECT: " . Data::Dumper::Dumper($jsonObj);
    
     
  5. Webinx

    Webinx Member

    Joined:
    Sep 20, 2012
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Website Owner
    Is that suitable compared to what mario gave?
     

Share This Page