Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Perl's Telnet vs. Expect Module

by TASdvlper (Monk)
on Dec 02, 2003 at 23:07 UTC ( #311776=perlquestion: print w/ replies, xml ) Need Help??
TASdvlper has asked for the wisdom of the Perl Monks concerning the following question:


I'm going to work on a script that will execute commands on a remote system, and I'm curious as to what the better mechanism is, Perl's expect or Telnet module ? Are there any pros or cons to each ? I'm not doing anything that complicated and I'm not worried about security (isolated LAN).

Any thoughts ??

Any help is much appreciated.

Replies are listed 'Best First'.
Re: Perl's Telnet vs. Expect Module
by pg (Canon) on Dec 03, 2003 at 02:31 UTC

    Expect supports your decision making much more smoothly than telnet.

    From a purpose point of view, telnet is not made to matching output for you, although Perl's telnet module does cover this a little bit, for example waitfor(), but very basic. From the perception of the author of telnet module, that is not what he should focus on, and spent time on. So even in the future, you don't expect the module to extend on this.

    Expect is made for the purpose of making decisions base on output.

    Also it makes sense to unplug the underneath communication stuff from your decision making process. Expect does not require telnet, in fact the output can come from any program. In case the source of your output is changed (the data under analysis is now coming from a different source), the majority of your script would still be useful, if it is based on Expect.

Re: Perl's Telnet vs. Expect Module
by sth (Priest) on Dec 02, 2003 at 23:41 UTC

    What are you doing on the remote server? If you are just running a non interactive program, then I would use Net::Telnet. If they are programs that need interactive responses, then Expect is probably what you want. I usually ssh to a server and run a command with a system() call.

    I probably should clarify that, I create a command like,

    my $cmd = q[ssh hostname -l foo 'ls bar']; die $! if system($cmd);


      Which also requires knowledge of setting up ssh with passwordless keys. Read up on .authorized_keys (authorized_keys2) and ssh-genkey for more information. That is, if it is to be unattended, and probably less secure.

      Play that funky music white boy..
      Basically, I was to "push" a set of scripts to a remote system, execute them, collect logs and move then back to my server, then clean up the remote server. So, script executed remotely is not interactive, but I need to do other actions pre/post executing the script.
Re: Perl's Telnet vs. Expect Module
by Elijah (Hermit) on Dec 03, 2003 at 00:44 UTC
    Well Perls telnet module has a function called "waitfor" that acts exactly like expect would would you issue a command and "waitfor" a certain response and then continue. If you are telneting to a piece of cisco gear then I would use the Net::Telnet::Cisco module and the waitfor function. I would do this because the Net::Telnet:Cisco function is going to be more catered to the interaction you will see on a cisco box. Also if your target machine uses any sort of special telnet formatting then the telnet module also might be a better choice. I have never used the expect module so I am not sure how configurable it is but I have program in expect itself which is a derivitive of TCL and the expect language itself is very versatile so if the module takes after the language either should be a good choice.
Re: Perl's Telnet vs. Expect Module
by logan (Curate) on Dec 03, 2003 at 05:26 UTC
    "I'm not worried about security" is one of the scariest phrases in the lexicon. Run your plan past your admin and see what he says about it. "I've considered the situation and security concerns have been addressed" is a far better response. Keep in mind that you'll be sending your password unencrypted, and it will have to be listed as plain text in your program, accessible to anyone who cares to look. Imagine what the weird intern might do with that information, especially when his internship is over.

    Think about what your program will be used for and what servers it will run on. Can you use rsh or rcp instead of telnet? Also, you're not doing whatever you're doing as root are you? Don't if you can avoid it. If possible, require that the password be passed in as an argument at invocation. It's far better than putting the root password to a cluster of servers as plain text in a script that may get passed around like a fattie at Snoop Dogg's house.

    Even if you're working on an isolated LAN, you have to consider what happens if someone gains access to the LAN. It's far better to have a second (or third) line of defense than to have one password give access to the whole system.

    "What do I want? I'm an American. I want more."

      I would take a closer look at I use it frequently and it saves you a lot of time not fiddling around with Net::Telnet. Additionally, has a lot more functionality you will find very usefull.

      cat /dev/world | perl -e "(/(^.*? \?) 42\!/) && (print $1))"
        It sounds like Expect is the way to go. Below is what I want to do, does Expect still make sense ? I was to "push" a set of scripts to a remote system, execute them, collect logs and move then back to my server, then clean up the remote server. So, script executed remotely is not interactive, but I need to do other actions pre/post executing the script. btw - what is /dev/world ? I don't see it on my Linux/Unix boxes.
        Can you elaborate on some of the functionality that you find useful ??? I still trying to get a grasp on what's better, Telnet or Expect.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://311776]
Approved by DaWolf
Front-paged by DaWolf
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2016-06-26 04:24 GMT
Find Nodes?
    Voting Booth?
    My preferred method of making French fries (chips) is in a ...

    Results (327 votes). Check out past polls.