Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Using Expect and interact with vi

by saunite (Initiate)
on Sep 13, 2012 at 18:49 UTC ( #993559=perlquestion: print w/replies, xml ) Need Help??
saunite has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I am doing a simple perl script to connect to a remote machine via ssh and open an interactive terminal

The connection works fine, the problem is that, after I am connected to a Solaris box, if I try to open a file in vi, it shows the whole screen with the ~ at the beginning of each line, even though the last line correctly shows the file name and number of characters in the file, but the text inside the file is not shown, and also, if I try to edit it, the ~ continues there as if they were part of the text.

I tried the same on a Linux box, and what happened was that I could edit the file, but the file was inside a small "box" in the left upper corner of my terminal, instead of being the whole size of my terminal.

I am using the code below:

#!/usr/bin/perl use Expect; my $ip = ""; my $user = "user"; my $passwd = 'user'; my $timeout = 30; my $command_prompt = '[#%\$>]|\:\/\s*$|TERM\ \=\ \([a-z]*\)'; my $passwd_prompt = '([p|P]ass|[p|P]ass[w|W]or[d|t]|[c|C]ontrase.a|Ent +er passphrase for key )\s*:\s*$'; my $user_prompt = '([l|L]ogin|[u|U]suario|[u|U]ser-?[n|N]ame|[u|U]ser) +\s*:\s*$|[n|N]ame\s*(.*:.*):\s*'; my $conn_command = "ssh -l $user -p 22 $ip"; my $ct = Expect->spawn($conn_command) or die "Error $conn_command: $!\ +n"; $ct -> expect ( $timeout, [ $user_prompt, sub { $ct -> send ( "$user\n" ); exp_continue; } ], [ $passwd_prompt, sub { $ct -> send ( "$passwd\n" ); exp_continue; } ], [ $command_prompt, sub { $ct->interact( \*STDIN, __TESTING__TESTING__ ); } ], ); $ct->close();

Does anyone have an idea of why is it behaving like that?

Thanks very much in advance!!!

Replies are listed 'Best First'.
Re: Using Expect and interact with vi
by kcott (Chancellor) on Sep 14, 2012 at 10:21 UTC

    G'day saunite,

    Welcome to the monastery.

    What you're describing is the normal behaviour for vi (vim, etc.).

    There's lots of vi tutorials online. Searching for vi tutorial, the first one I found ( has:

    $ vi myfile.txt

    Your file will be loaded into vi, and the cursor will be placed at the beginning of the first line. Note that an empty line is shown as a tilde (~).

    In the past, I have set up vi (and a number of other commands) on *nix boxes to start in a new window. I suspect this is what's happening on your Linux machine. There's a number of ways to do this: check with your system administrator for details of how that's achieved on your system.

    -- Ken


      I solved my issue by changing:

      my $conn_command = "ssh -l $user -p 22 $ip"; my $ct = Expect->spawn($conn_command) or die "Cannot spawn $conn_comma +nd: $!\n";
      my $ct = new Expect; $ct->slave->clone_winsize_from(\*STDIN); $ct->spawn($conn_command) or die "Cannot spawn $conn_command: $!\n";

      Thanks all for the help.

      André Saunite

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://993559]
Approved by Paladin
and the voices are still...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2018-06-25 15:51 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (127 votes). Check out past polls.