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

Hello I am trying to write a script using Net::Telnet that will allow me to telnet from W2K and interact with a unix box after logging in automatically, run a few commands at the command line (change shell, setenv etc)and then continue working on the unix box. How do I break out of the script on the remote machine? thanks in advance! I am using the script below;
use Net::Telnet; use strict; $Telnet::Exp_Internal = 0; $Telnet::Log_Stdout = 1; my $host = 'x.x.x.x'; my $user = 'root'; my $pwd = 'pswd'; my $prompt = '[login]'; my $telnet; my $output; $telnet = Net::Telnet->new(Host=>$host,Prompt=>"/$prompt/",input_log=> +"debug.txt"); $telnet->login(Name=>$user,Password=>$pwd); print "Connecting ......"; sleep 2;

Replies are listed 'Best First'.
Re: telnet login script from W2K to unix box
by dfaure (Chaplain) on Jul 28, 2004 at 09:37 UTC
    ...run a few commands at the command line (change shell, setenv etc)and then continue working on the unix box. How do I break out of the script on the remote machine?

    Your Net::Telnet script is seen by your host as a single telnet access to it. When the script ends, the network connection is close and the host session is lost (login shell terminated).

    • The first solution I see is to drive a real telnet client, feeding it with your commands from a script.
    • Depending on the Perl distrib you have, you may also use Expect.
    • You may even write your own terminal emulator, replacing the sleep 2; pause with a (quasi-)infinite loop waiting for an input, sending it and displaying the output back to you.

    You might also have other solutions involving a specific configuration of the pseudo-terminals of your destination host, but they are beyond my knowledge of the Unix system.

    ____
    HTH, Dominique
    My two favorites:
    If the only tool you have is a hammer, you will see every problem as a nail. --Abraham Maslow
    Bien faire, et le faire savoir...

      I didn't think there was an Expect Module for windows?????

        According to pod, it "runs" on the perl's cygwin distribution which provides terminal emulation à la Unix.

        Anyway, using PuTTY and Win32::GuiTest you may be able to drive your telnet session as you like.

        ____
        HTH, Dominique
        My two favorites:
        If the only tool you have is a hammer, you will see every problem as a nail. --Abraham Maslow
        Bien faire, et le faire savoir...

Re: telnet login script from W2K to unix box
by Anonymous Monk on Jul 28, 2004 at 13:22 UTC
    I'm not really sure what you are trying to do, but ssh (openssh anyway) has the functionality to run commands remotely, and doesn't have the security headaches of sending your root password unencrypted. If you typically do the same stuff, it might be a better idea to write a shell script on the remote server to do the work, and use ssh to start that script.
Re: telnet login script from W2K to unix box
by gman (Friar) on Jul 28, 2004 at 13:45 UTC
    if you have a x window emulation on your windoz box
    like exceed or cygwin xfree86 and an x window libraries
    on your unix box, you could set you display. see example below
    #!/usr/local/bin/perl -w use strict; use Net::Telnet; my $username = "xxxx"; my $passwd = "xxxx"; my $hostname = "xxxx"; #use Net::Telnet (); my $t = new Net::Telnet (Timeout => 10, Prompt => '/}/'); $t->input_log('Tempature.out'); $t->errmode('return'); $t->open($hostname); $t->login($username, $passwd); $t->print("ksh"); $t->print("export DISPLAY=10.231.14.57:0.0"); $t->print("nohup /usr/openwin/bin/xterm &"); sleep 2; $t->close;
      I would also like to add, as someone else has stated this is insecure and SSH should be used if possible.
      gman