gerty icon indicating copy to clipboard operation
gerty copied to clipboard

Could not match the output for

Open celangoni opened this issue 7 years ago • 0 comments

Hi folks! First of all... OMG, what a amazing piece of software! I passed the last days testing and learning how to operate gerty correctly. At this point I'm able to run backup of all my devices (Cisco, Datacom, Juniper and Huawei). One little problem here is that I don't have a default backup user, so I think thanks that I have the perfect tool for the job. I'm not a perl programmer, but I wrote this piecie of code: package nipbr::DatacomExtras;

use strict;
use warnings;

use JSON;

our $action_handlers_registry =
  { 'create_backup_user' => \&create_backup_user, };

our $retrieve_action_handlers = \&retrieve_action_handlers;

sub retrieve_action_handlers {
    my $ahandler = shift;

    return $action_handlers_registry;
}

sub create_backup_user {
    my $ahandler = shift;
    my $action   = shift;

    my $add_users = $ahandler->device_attr('add_backup_user');
    if ( not defined($add_users) ) {
        return {
            'success' => 0,
            'content' => 'E preciso habilitar a criacao do usuario de backup '
              . 'com o atributo add_backup_user'
        };
    }
    if ( $add_users == 0 ) {
        return {
            'success' => 0,
            'content' => 'E preciso habilitar a criacao do usuario de backup '
              . 'com o atributo add_backup_user'
        };
    }

    my $result = { 'success' => 1, 'content' => '' };
    my $cmd_result;

    $cmd_result = $ahandler->exec_command('show running-config');
    if ( not $cmd_result->{'success'} ) {
        return $cmd_result;
    }

    my $user_exist = 0;
    my @config_lines = split( /\n/o, $cmd_result->{'content'} );

    foreach my $line (@config_lines) {
        if ( $line =~ /^username backup./o ) {

            $user_exist = "1";
        }
    }
    if ( $user_exist == 1 ) {
        return {
            'success' => 0,
            'content' =>
              'Usuário já cadastrado. Por favor remova-o antes de prosseguir'
        };
    }
    $cmd_result = $ahandler->exec_command('configure');
    if ( not $cmd_result->{'success'} ) {
        return $cmd_result;
    }
    $cmd_result = $ahandler->exec_command('username backup access-level 1');
    if ( not $cmd_result->{'success'} ) {
        return $cmd_result;
    }
    $cmd_result = $ahandler->exec_command('username backup password 0 stringforpassword');
    if ( not $cmd_result->{'success'} ) {
        return $cmd_result;
    }
    return {
        'success' => 1,
        'content' =>
          'Usuário cadastrado com sucesso!'
    };
}
1;

The structure is OK, I'm able to run without the exec_command after "if ( $user_exist == 1 ) {". When I try to run the full script I got the following error:

Nov 23 16:39:36 [ERROR] Could not match the output for DM4008: configure DM4008(config)# Nov 23 16:39:36 [WARNING] Action create_backup_user failed for device "DM4008" : Could not match the output for DM4008: configure DM4008(config)# Nov 23 16:39:36 [DEBUG] Wrote failure status file: /opt/gerty/nipbr//status/DM4008.create_backup_user.failure Nov 23 16:39:36 [DEBUG] Disconnecting from DM4008

My devices como from:

[devices switches-datacom-cmd] description = "Switches Datacom" source.type = Gerty::DeviceList::File source.filename = ${datapath}/dispositivos/switches-datacom source.filetype = plain source.delimiter = ; source.fields = SYSNAME, ADDRESS credentials-source = inline cli.auth-username = admin cli.auth-password = switchpassword devclass = nipbr.Datacom-Switches-cmd

My devclass is:

[devclass nipbr.Datacom-Switches-cmd] inherit = Gerty.CiscoLike admin-mode = 0 config-mode.enter = configure config-mode.end = end config-mode.prompt = ^.*# credentials-source = inline cli.init-terminal = +cli.handler-mixins = nipbr::DatacomExtras

In the config-mode.prompt I've tried somethings, but it seens gerty does not giv a shit to what I wrote here.

Thanks a lot for your help and for this amazing program!

celangoni avatar Nov 23 '17 19:11 celangoni