Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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 = "10.0.0.1"; 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!!!

Comment on Using Expect and interact with vi
Download Code
Re: Using Expect and interact with vi
by kcott (Abbot) 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 (http://www.unix-manuals.com/tutorials/vi/vi-in-10-1.html) 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

      Thanks!

      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";
      to:
      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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://993559]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (11)
As of 2014-09-23 09:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (216 votes), past polls