Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Net::SSH::Perl -- can I capture the command prompt?

Reply
Thread Tools

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

 
 
Keith
Guest
Posts: n/a
 
      11-15-2006
I've been using Net::SSH:erl 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!

 
Reply With Quote
 
 
 
 
Deepika
Guest
Posts: n/a
 
      11-15-2006
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:erl;
my $test = "xx.xx.xx.xx";
%params = ("debug",true);
my $sshtest = Net::SSH:erl->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:erl 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]#

Keith wrote:
> I've been using Net::SSH:erl 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!


 
Reply With Quote
 
 
 
 
koneruarjun@gmail.com
Guest
Posts: n/a
 
      11-15-2006
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

Keith wrote:
> I've been using Net::SSH:erl 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!


 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      11-15-2006
Keith <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> I've been using Net::SSH:erl 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
 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      11-15-2006
Keith wrote:
> I've been using Net::SSH:erl 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.
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      11-26-2006
On 2006-11-15 08:11, http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de <(E-Mail Removed)-berlin.de> wrote:
> Keith <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>> I've been using Net::SSH:erl 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:erl.


> 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


--
_ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
|_|_) | Sysadmin WSR | > ist?
| | | http://www.velocityreviews.com/forums/(E-Mail Removed) | Was sonst wäre der Sinn des Erfindens?
__/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 8.25 How can I capture STDERR from an external command? PerlFAQ Server Perl Misc 0 03-12-2011 05:00 AM
Screen Capture With Mouse , Mouse Position Capture Max Java 7 08-08-2009 11:51 PM
SuperVideoCap work as a broadcast capture and screen capture and record tool. hely0123 Media 0 10-30-2007 08:59 AM
Kindly help :::How can I send the output of the system() command to a file and capture a string in one of the lines in that file prashant_ec2003@yahoo.com Perl Misc 8 06-21-2005 04:02 PM
How can I send the output of the system() command to a file and capture a string in that file prashant_ec2003@yahoo.com Perl Misc 5 06-19-2005 11:40 PM



Advertisments