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

Discussion in 'Perl Misc' started by Keith, Nov 15, 2006.

  1. Keith

    Keith Guest

    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!
    Keith, Nov 15, 2006
    #1
    1. Advertising

  2. Keith

    Deepika Guest

    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]#

    Keith wrote:
    > 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!
    Deepika, Nov 15, 2006
    #2
    1. Advertising

  3. Keith

    Guest

    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::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!
    , Nov 15, 2006
    #3
  4. Keith

    -berlin.de Guest

    Keith <> wrote in comp.lang.perl.misc:
    > 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
    -berlin.de, Nov 15, 2006
    #4
  5. Keith

    J. Gleixner Guest

    Keith wrote:
    > 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.
    J. Gleixner, Nov 15, 2006
    #5
  6. On 2006-11-15 08:11, -berlin.de <-berlin.de> wrote:
    > Keith <> wrote in comp.lang.perl.misc:
    >> 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


    --
    _ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
    |_|_) | Sysadmin WSR | > ist?
    | | | | Was sonst wäre der Sinn des Erfindens?
    __/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd
    Peter J. Holzer, Nov 26, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chrisdude911
    Replies:
    10
    Views:
    135,333
    srk289
    Mar 10, 2009
  2. Slawek Zachcial
    Replies:
    0
    Views:
    113
    Slawek Zachcial
    Jul 10, 2008
  3. Tench Johnson
    Replies:
    1
    Views:
    278
    Biff Tannen
    Apr 23, 2010
  4. salamond

    net/ssh in ruby. ssh.exec fails

    salamond, Feb 17, 2011, in forum: Ruby
    Replies:
    0
    Views:
    225
    salamond
    Feb 17, 2011
  5. Replies:
    0
    Views:
    80
Loading...

Share This Page