zen0 has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, I have a bunch of old freebsd machines. I'm tring to send them somme commands with Net::Appliance:session module and get the results. I've custom phasebook called FreeBSD411. That's the code:
my $s = Net::Appliance::Session->new({ personality => "FreeBSD411", add_library => "./PhaseBooks", transport => 'SSH', host => $host, #privileged_paging => 1, # only if using ASA/PIX OS 7+ # and there are other behaviour options, +see below connect_options => { opts => [ '-p', '22', # connect to non-standard port on +remote host #'-o', 'CheckHostIP=no', # don't check host IP in known_h +osts file ], }, log_config => { dispatchers => ['screen','file'], screen => { class => 'Log::Dispatch::Screen', min_level => 'warning', }, file => { class => 'Log::Dispatch::File', min_level => 'debug', filename => '/var/log/myapp.log', mode => 'append', format => '[%d] %m', }, }, }); $s->set_global_log_at('debug'); try { my @result = shift; my @cmds = ('whoami','uname -a','df -h'); $s->connect({ username => $user, password => $pass, privileged_pas +sword => $su_pass}); $s->disable_paging(1); $s->begin_privileged({$su_pass}); ## I don't know how to call t +his ;) or Doesn't work ? push @result, $s->cmd("$cmds[0]"); #push @result, $s->cmd("$cmds[1]"); #push @result, $s->cmd("$cmds[2]"); $s->end_privileged; print "Host: $host \"@cmds\": @result \n"; } catch { warn "failed to execute command: $_"; } finally { $s->close; };
The custom Phasebook - FreeBSD411
prompt user match /[Uu]sername: $/ prompt pass match /[Pp]assword:\s+$/ prompt passsu match /[Pp]assword:$/ prompt generic03 match /\/]\$\s+$/ prompt generic01 match /\/\]\$\s+$/ prompt generic02 match /from$/ prompt generic match /\s+\/]\$$/ prompt privileged # match /^root@.+#\s+$/ match /^\[root@.+\~\]\#\s+$/ macro begin_privileged send su - match passsu or privileged macro end_privileged send exit match generic macro disconnect send logout macro paging send '' # send terminal length %s

My problems : I didn't succeed to disable paging (disable_paging). I didn't succeed to become root with "su -" and execute somme comands ...

Log

...... [ 0.589068] di queueing data for send: "" [ 0.589289] tr callback received for match [ 0.600092] du SEEN: [user1@freebsd411 /]$ [ 0.600355] tr output matched (?-xism:\s+\/]\$$), storing and retur +ning [ 0.600926] pr output matching prompt was "[user1@freebsd411 /]$" [ 0.601122] pr setting new prompt to (?-xism:\s+\/]\$$) [ 0.601267] pr prompt has been set to (?-xism:\s+\/]\$$) [ 0.601403] di trimmed command response: Odd number of elements in anonymous hash at ./send-cmds.pl line 58. [ 0.602153] en running macro begin_privileged [ 0.602440] en executing actions [ 0.602699] en dispatching to execute method [ 0.603191] tr callback received for send [ 0.603317] di queueing data for send: "su -" [ 0.603535] tr callback received for match [ 0.620060] du SEEN: Password: [ 0.620492] tr output matched (?-xism:[Pp]assword:$), storing and r +eturning [ 0.622734] pr output matching prompt was "Password:" [ 0.622947] pr setting new prompt to (?-xism:[Pp]assword:$) [ 0.623088] pr prompt has been set to (?-xism:[Pp]assword:$) [ 0.623219] di trimmed command response: failed to execute command: should be in privileged mode but prompt doe +s not match at /Volumes/lala/Ports/lib/perl5/site_perl/5.12.4/Net/App +liance/Session/Engine.pm line 110. [ 0.624138] en running macro paging [ 0.624271] en macro params are: 24 [ 0.624580] en executing actions [ 0.624828] en dispatching to execute method [ 0.625317] tr callback received for send [ 0.625444] di queueing data for send: "" [ 0.625665] tr callback received for match [ 0.640885] du SEEN: Sorry [user1@freebsd411 /]$ ..............................
at line 58 :
$s->begin_privileged({$su_pass}); ## I don't know how to call/use + this ;) or Doesn't work ?
and into the module Engine.pm we've function begin_privileged:
110 $self->prompt_looks_like('privileged') 111 or die 'should be in privileged mode but prompt does not ma +tch'; 112 $self->in_privileged_mode(1);

Replies are listed 'Best First'.
Re: Net::Appliance::Session, Disable paging & send commands after "su" ?
by kschwab (Vicar) on Nov 18, 2013 at 18:02 UTC

    I've never used the module, but the docs say this:

    To see a log of all the processes within this module, and a copy of all data sent to and received from the device, call the following method: $s->set_global_log_at('notice');

    I'm guessing that would show what the other end actually sent, then you would have better insight into why it didn't match.

      Just noticed you already have logging at the 'debug' level, so it should show the data...but I don't see it. Perhaps calling the $s->last_reponse() method would show it?
        Hello monks,

        I found the solutions for one of my problems :

        1. disable paging -- just put ....new->({ ..do_paging => 0.. })

        https://rt.cpan.org/Public/Bug/Display.html?id=69139

        2. $s->begin_privileged({password => 'pass'});

        The above suggestion.. Doesn't work for me. Workaround is to put "send pass " in the macro begin_privileged at the Phrasebook file i.e.

        ...... macro begin_privileged send su - match passsu send pass match privileged ......