cosmicsoup has asked for the wisdom of the Perl Monks concerning the following question:

I'm getting pretty frustrated with this Net::Telnet command prompt criteria. I will put my code below in a pre statement. The problem I am having is that the unix servers I'm connecting to each have a different command prompt and multiple users. The SA recently changed the command prompt to a two-tiered prompt and now it's caused my code to fail. Is there anyway to pre-determine a machine's prompt during the telnet session or is there anyway around this?

This is a subroutine that I call. It reads in a password file which currently contains (user|password|prompt).
sub create_telnet { my( $host, $pass, ) = @_; my ( $user, $passwd, $prompt ) = read_password_file( $host, $pass +); my $telnet = new Net::Telnet (Timeout => 20, Binmode => 0, Prompt => ("/").(quotemeta $prompt). +("/"), Output_log => $mydump, Errmode => 'die', ); $telnet->open($host); $telnet->login( $user, $passwd ); return $telnet; }

Many Thanks,

Louis

Replies are listed 'Best First'.
Re: Telnet Command Prompt Help
by castaway (Parson) on Jan 29, 2003 at 15:26 UTC
    Theres no way to find out the prompt before you login, as you need to login to do that :)
    Net::Telnets default Prompt is /[\$%#>] $/ which is pretty generic, just wants any line which ends in $, %, k, > and then a single space. Maybe you can try an even more generic regexp (I wonder what .* would do?)

    Failing that (and as I'm not particularly enamoured of Net::Telnet for this very reason), you could try either Net::SSH / Net::SSH::Perl if you have ssh on the machines, or just a plain old IO::Socket::INET

    C.

      Thanks C. Do you know of a good web resource for Net::SSH?

      Louis
        try here ran into this same problem Net-SSH is the way to go
Re: Telnet Command Prompt Help
by Gilimanjaro (Hermit) on Jan 29, 2003 at 16:15 UTC
    Using telnet is usually a bad idea, unless you yourself are sitting behind the keyboard; that's what it was designed for.

    Update: Actually, even when behind the keyboard ssh is a much better idea because of encryption and such...

    If you need to execute commands ssh (Net::SSH) is the way to go. Depending on what it is you exactly need to do, using other protocols might also be an option...

    If you really are stuck with telnet a small trick that might be helpful is to blindly send the command 'echo $PS1' to the session. That will return the sequence that is used to build the prompt... But it's not identical to the prompt you'll be getting. Problem is that the prompt can change, if there are special sequences in the PS1 variable.

    I should point out that even this only works for the most commen *nix shells... I can telnet to my printer if I want, and that can give me another total unpredictable prompt.

    I'd try to find another way to access your server if I were you...

Re: Telnet Command Prompt Help
by SysApe9000 (Acolyte) on Jan 29, 2003 at 17:27 UTC

    Maybe you could consider using something other than telnet? RSH isn't pretty and there are security issues, but telnet has security issues that are just as bad if not worse. At least with rsh you wouldn't be sending the password cleartext.

    If your stuck with telnet for some reason, you might want to consider the Expect module for Perl. I've gotten some serious mileage out of that module and even though it has some limitations it's quite nice for connecting to devices that don't have another mechanism for interacting. I've used it to talk to terminal servers, Nortel DMS switches, and I'll probably have to use it talk to an Alteon load-balancer sometime soon too.

    With the Expect module you can log in, and look at what you're getting back and adjust your expect statements accordingly. Or since Expect allows you to match using regexes, you can probably devise a regex that covers all the cases you need.

    The Expect module is available on CPAN, btw and here are some docs. Good luck!