ollyg/Net-Appliance-Session — Issue #13: in_privileged_mode should verify actual state on the switch
Author: obilodeau
Created: 2012-01-25 17:26:56+00:00
State: open

Hi,

We are using your module on our open source project PacketFence (www.packetfence.org).

We are often connecting on devices that require escalating to privileged mode and others that don't require it. This is all user-controlled and configured. Not something we can control.

Reading [Cookbook Recipe 5](http://search.cpan.org/~oliver/Net-Appliance-Session-1.36/lib/Net/Appliance/Session/Cookbook/Recipe05.pod) I realized that there is no way to connect to a switch using the same code if it's privileged or not.. Am I wrong about this?

I was thinking that since the yaml syntax knows what the privileged and unprivileged prompts look like that a call to in_privileged_mode would actually verify the state on the device connected to.

Right now with 1.36 I get an undef when calling in_privileged_mode after a connect. I was wondering if, instead of returning undef, you could check the prompt and return the actual mode?

Anyways, for now I'll probably tell users to forget about logging in unprivileged but in the long term I would really like to handle both gracefully with the same code.

```perl
my $session;
eval {
$session = Net::Appliance::Session->new(
Host => $this->{_ip},
Timeout => 5,
Transport => $this->{_cliTransport}
);
$session->connect(
Name => $this->{_cliUser},
Password => $this->{_cliPwd}
);
};

if ($@) {
$logger->error("Error connecting to " . $this->{'_ip'} . " using ".$this->{_cliTransport} . ". Error: $!");
}

# are we in enabled mode?
if (!$session->in_privileged_mode()) {

# let's try to enable
if (!$session->enable($this->{_cliEnablePwd})) {
$logger->error("Cannot get into privileged mode on ".$this->{'ip'}.
". Are you sure you provided enable password in configuration?");
$session->close();
return 0;
}
}

eval {
$session->cmd(String => "conf t", Timeout => '10');
$session->cmd(String => "int $ifName", Timeout => '10');
$session->cmd(String => "switchport port-security maximum $maxSecureMac vlan access", Timeout => '10');
$session->cmd(String => "end", Timeout => '10');
};

```
Please ignore the lack of proper error-handling and the fact that I don't use begin_configure in that code... ;) I'll get to these eventually.

Thanks for such a great module!