Net::SSH::Perl -- can I capture the command prompt?

K

Keith

I've been using Net::SSH::perl to log in to Cisco routers and perform
tasks, but there's one thing I can't seem to do. Without opening an
actual interactive shell, I just want to capture (in a string) the
actual command prompt upon logging in. For example, when you log into
a Cisco router via a telnet or SSH shell, the prompt is normally
something like "Router>".

The reason I need to do this is to tell the level of access; i.e. the
prompt "Router>" tells you that you're in EXEC mode, the prompt
"Router#" tells you that you're in privileged mode, and
"Router(config)#" tells you that you're in configuration mode. This
isn't specific to Cisco either -- logging into another UNIX machine
obviously gives you a specific prompt that reveals the host, user, and
shell.

The method $ssh->cmd() works fine, but it just returns the output
resulting from a specific command. i.e., if you give the command:

my ($stdout, $stderr, $exit_status) = $ssh->cmd('show ip route');

The $stdout string will contain the output from the command (in this
case the routing table), but it won't give the contents of the actual
command prompt. I'd appreciate any advice if anyone has had to do this
before. Thanks!
 
D

Deepika

Hi Keith,

Reading your post was such a relief(to know that NET::SSH works with
Cisco routers).
I have been trying to do the same task as you mentioned above. I am
writing a perl Script to log into a CLI Box which behaves just like a
cisco router having the same prompts "> # (config)" and has the same
commands, but for me after logging into the CLI none of my commands
actually give an output back. I will really really appreciate if you
can just look at my straightfoward script and kindly tell me what I am
doing wrong. I have my output capture as well, as you can see "stdout"
has no value.
If you don't have time to look at my code, can you share your script
with me.

I will research on your task as well to capture the command prompt on
logging in. I will let you know if I find anything.
Here is my script for your reference:
*********************************************
use Net::SSH::perl;
my $test = "xx.xx.xx.xx";
%params = ("debug",true);
my $sshtest = Net::SSH::perl->new($test,%params);
my ($stdout, $stderr, $exit) = $sshtest->login("username","password");

print "\n value of stdout = $stdout \n";
print "\n value of stderr = $stderr \n";
print "\n value of exitt = $exit \n";
($stdout, $stderr, $exit) = $sshtest->cmd("?");
print "\n value of stdout = $stdout \n";
print "value of stderr = $stderr \n";
print "value of exitt = $exit \n";
*******************************************
HERE IS DEBUG OUTPUT:
[root@deepika-linux rnc]# perl ss.pl
deepika-linux: Reading configuration data /root/.ssh/config
deepika-linux: Reading configuration data /etc/ssh_config
deepika-linux: Allocated local port 1023.
deepika-linux: Connecting to xx.xx.xx.xx, port 22.
deepika-linux: Remote version string: SSH-2.0-OpenSSH_3.5p1


deepika-linux: Remote protocol version 2.0, remote software version
OpenSSH_3.5p1
deepika-linux: Net::SSH::perl Version 1.30, protocol version 2.0.
deepika-linux: No compat match: OpenSSH_3.5p1.
deepika-linux: Connection established.
deepika-linux: Sent key-exchange init (KEXINIT), wait response.
deepika-linux: Algorithms, c->s: 3des-cbc hmac-sha1 none
deepika-linux: Algorithms, s->c: 3des-cbc hmac-sha1 none
deepika-linux: Entering Diffie-Hellman Group 1 key exchange.
deepika-linux: Sent DH public key, waiting for reply.
deepika-linux: Received host key, type 'ssh-dss'.
deepika-linux: Host 'xx.xx.xx.xx' is known and matches the host key.
deepika-linux: Computing shared secret key.
deepika-linux: Verifying server signature.
deepika-linux: Waiting for NEWKEYS message.
deepika-linux: Enabling incoming encryption/MAC/compression.
deepika-linux: Send NEWKEYS, enable outgoing
encryption/MAC/compression.
deepika-linux: Sending request for user-authentication service.
deepika-linux: Service accepted: ssh-userauth.
deepika-linux: Trying empty user-authentication request.
deepika-linux: Authentication methods that can continue: password.
deepika-linux: Next method to try is password.
deepika-linux: Trying password authentication.


value of stdout = 1


value of stderr =


value of exitt =
deepika-linux: channel 0: new [client-session]
deepika-linux: Requesting channel_open for channel 0.
deepika-linux: Entering interactive session.
deepika-linux: Sending command: ?
deepika-linux: Requesting service exec on channel 0.
deepika-linux: channel 0: open confirm rwindow 0 rmax 32768


value of stdout =
value of stderr =
value of exitt =
[root@deepika-linux rnc]#
 
K

koneruarjun

In case of bourne/bash shell (and maybe most other shells) , You could
check $PS1 to access the contents of the prompt.
Not sure if such an environment variable is defined in your case - Also
If you just want to get the privileges of the user ..may be theres is
another method to obtain that as well.(something like `whoami` in most
*nix flavors)
HTH
-Arjun
 
A

anno4000

Keith said:
I've been using Net::SSH::perl to log in to Cisco routers and perform
tasks, but there's one thing I can't seem to do. Without opening an
actual interactive shell, I just want to capture (in a string) the
actual command prompt upon logging in. For example, when you log into
a Cisco router via a telnet or SSH shell, the prompt is normally
something like "Router>".

The reason I need to do this is to tell the level of access; i.e. the
prompt "Router>" tells you that you're in EXEC mode, the prompt
"Router#" tells you that you're in privileged mode, and
"Router(config)#" tells you that you're in configuration mode. This
isn't specific to Cisco either -- logging into another UNIX machine
obviously gives you a specific prompt that reveals the host, user, and
shell.

The connection between the prompt and properties of the process is
tenuous at best. It is shell-dependent and user configurable, so it's
nothing to rely on.

Find how the prompt is set to its various forms (study the startup
script(s) for the shell) and use the methods that are used there.

Anno
 
J

J. Gleixner

Keith said:
I've been using Net::SSH::perl to log in to Cisco routers and perform
tasks, but there's one thing I can't seem to do. Without opening an
actual interactive shell, I just want to capture (in a string) the
actual command prompt upon logging in. For example, when you log into
a Cisco router via a telnet or SSH shell, the prompt is normally
something like "Router>".
[...]

Take a look at the Expect module.
 
P

Peter J. Holzer

Keith said:
I've been using Net::SSH::perl to log in to Cisco routers and perform
tasks, but there's one thing I can't seem to do. Without opening an
actual interactive shell, I just want to capture (in a string) the
actual command prompt upon logging in. [...]
The reason I need to do this is to tell the level of access; i.e. the
prompt "Router>" tells you that you're in EXEC mode, the prompt
"Router#" tells you that you're in privileged mode, and
"Router(config)#" tells you that you're in configuration mode. This
isn't specific to Cisco either -- logging into another UNIX machine
obviously gives you a specific prompt that reveals the host, user, and
shell.

SSH on Unix can be used to invoke "interactive" and "non-interactive"
shells. Only interactive shells send a command prompt. Maybe IOS makes
the same distinction. So you need to figure out how to invoke an
interactive shell from Net::SSH::perl.

The connection between the prompt and properties of the process is
tenuous at best. It is shell-dependent and user configurable, so it's
nothing to rely on.

Find how the prompt is set to its various forms (study the startup
script(s) for the shell) and use the methods that are used there.

On a CISCO router?

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top