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

Hi All,

I was wondering what people's experiences have been with using perl kick off and automate telnet/ssh sessions that they wish to have live access to later.

For example, say I want to have a script that will ssh my current console into another system, run a few commands, and then drop control of the console back to me, as if I had sshed in myself.

I am currently using the expect module and telnet for this, but find it somewhat awkward and am looking to the monks for a possibly more elegant solution. I would also prefer to use ssh, which I haven't had much luck with using expect.

Thanks in advance!

Replies are listed 'Best First'.
Re: Automate remote console sessions.
by almut (Canon) on Jun 10, 2010 at 18:23 UTC
    and then drop control of the console back to me, as if I had sshed in myself.

    Not really sure I understand what exactly you intend to do... but maybe the screen utility might help for switching between sessions.  It allows you to detach from and reattach to terminal sessions.

Re: Automate remote console sessions.
by Yary (Pilgrim) on Jun 10, 2010 at 21:54 UTC
    One of my first perl projects involved a "screen scraper"- a telnet session to a mainframe application that was meant to be used interactively. Our program had to look for field names, see what if any text was highlighted, tab to the right place, enter data, get results from pages of tables, etc. And it was written in a perl4 variant- curseperl?- that was a long time ago. I wouldn't want to write it again without expect and heavy usage of CPAN.

    I think expect is pretty good since it hides the tty details. But if you are only executing a few commands and they aren't text menu/field monsters, try the Net::SSH (uses your system's ssh) or Net::SSH::Perl (pure-perl SSH, no sys executable needed) modules. Haven't tried it myself but it looks like it's designed with that usage in mind.

      I just re-read your question and realize I misunderstood it. Net::SSH probably won't help you much, and I can't think of anything better than the expect module offhand. Let's see what the monks have to say.