Sigh. It's Friday afternoon and the code doesn't work. I am trying to automate the remote configuration of a slew of Netscreen routers. They allow an SSH connection, and I've been playing with btrott's Net::SSH::Perl module. Right now I'm a bit stuck.
If I open a shell, I can type in a command and get the output back, but if I try to send it a command, it just hangs there. I must be doing something wrong but I can't see what. Stripped to the essentials, the code looks like this:
#! /usr/bin/perl -w use strict; use Net::SSH::Perl; use Term::ReadKey; my $host = '10.0.0.254'; my $user = 'foo'; my $pw = 'bar'; my $ssh = Net::SSH::Perl->new( $host, ( protocol => '1', debug => 1, )) or die "Could not create Net::SSH::Perl object: $!\n"; $ssh->login($user, $pw); if( @ARGV ) { ReadMode('raw'); $ssh->shell; ReadMode('restore'); } else { my($stdout, $stderr, $exit) = $ssh->cmd("get config\n"); print <<"SSH_END"; STDOUT BEGIN $stdout STDOUT END STDERR BEGIN $stderr STDERR END EXIT=$exit SSH_END }
So if I run this code with some stuff on the command line, it'll do the shell-based approach. This produces the following result:
localhost: Reading configuration data /home/foo/.ssh/config localhost: Reading configuration data /etc/ssh_config localhost: Connecting to 10.0.0.254, port 22. localhost: Remote protocol version 1.5, remote software version SSH Pr +otocol Compatible Server SCS 2.0 localhost: Net::SSH::Perl Version 1.23, protocol version 1.5. localhost: No compat match: SSH Protocol Compatible Server SCS 2.0. localhost: Connection established. localhost: Waiting for server public key. localhost: Received server public key (768 bits) and host key (1024 bi +ts). localhost: Host '10.0.0.254' is known and matches the host key. localhost: Encryption type: DES3 localhost: Sent encrypted session key. localhost: Received encryption confirmation. localhost: RSA authentication using agent refused. localhost: RSA authentication failed: Can't load public key. localhost: Trying password authentication. localhost: Requesting pty. localhost: Requesting shell. localhost: Entering interactive session. Remote Management Console foo-> get config Total Config size 6527: set auth type 0 set auth timeout 20 ... foo-> exit
Thus I can see that I can contact the router and get what I want out of it. On the other hand, if I run the script with a blank command line, it runs the other part of the conditional, and produces the following output:
ocalhost: Reading configuration data /home/dlandgre/.ssh/config localhost: Reading configuration data /etc/ssh_config localhost: Connecting to 10.0.0.254, port 22. localhost: Remote protocol version 1.5, remote software version SSH Pr +otocol Compatible Server SCS 2.0 localhost: Net::SSH::Perl Version 1.23, protocol version 1.5. localhost: No compat match: SSH Protocol Compatible Server SCS 2.0. localhost: Connection established. localhost: Waiting for server public key. localhost: Received server public key (768 bits) and host key (1024 bi +ts). localhost: Host '10.0.0.254' is known and matches the host key. localhost: Encryption type: DES3 localhost: Sent encrypted session key. localhost: Received encryption confirmation. localhost: RSA authentication using agent refused. localhost: RSA authentication failed: Can't load public key. localhost: Trying password authentication. localhost: Sending command: get config localhost: Entering interactive session. localhost: Warning: ignoring packet of type 15
... and then it just hangs there at that point.
I must admit to cut'n'paste programming. I just pinched this stuff out of the documentation, so I don't have much idea of what I'm doing :) I'm puzzled by the "packet of type 15", by the fact that I appear to send a command before entering an interactive session, but I'm leaning towards suffering from buffering to sweep that one under the carpet.
I registered a packet handler for those odd packets with:
$ssh->register_handler( 15, sub { my($s, $pkt) = @_; print "\ngot: ", $pkt->get_str, "\n"; });
...but that just produces:
Use of uninitialized value in addition (+) at /usr/local/share/perl/5. +6.1/Net/SSH/Perl/Buffer.pm line 125. substr outside of string at /usr/local/share/perl/5.6.1/Net/SSH/Perl/B +uffer.pm line 48. Use of uninitialized value in print at ./getcfg line 27. got:
And at this point I'm starting to run out of ideas. Any clues I can use will be warmly welcomed.
In reply to differences between Net::Perl::SSH shell and cmd methods by grinder
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |