[Perl Other] 使用expect后的窗口改变问题

Aug 11th, 2009

转载本站文章请注明,转载自:扶凯[http://www.php-oa.com]

本文链接: http://www.php-oa.com/2009/08/11/expect-window-size-changed.html

使用expect做自动login,一直有个麻烦的问题.就是当本地的终端窗口大小发生改变时,因为使用了expect,没法传送改变窗口的信号到远程机器上面,所以看起来很奇怪,严重影响工作的效率. 以前在perl上解决过,perl上对这个问题的详解如下 I set the terminal size as explained above, but if I resize the window, the application does not notice this. You have to catch the signal WINCH ("window size changed"), change the terminal size and propagate the signal to the spawned application:

my $exp = new Expect;
$exp->slave->clone_winsize_from(\*STDIN);
$exp->spawn("ssh somehost);
$SIG{WINCH} = \&winch;

sub winch {
  $exp->slave->clone_winsize_from(\*STDIN);
  kill WINCH => $exp->pid if $exp->pid;
  $SIG{WINCH} = \&winch;
}

$exp->interact();

There is an example file ssh.pl in the examples/ subdir that shows how this works with ssh. Please note that I do strongly object against using Expect to automate ssh login, as there are better way to do that (see ssh-keygen). 现在我们使用expect写和程序,先让我们来看看问题的现象. 普通的自动登陆的程序

#!/usr/bin/env expect
set server xxx.xxx.xxx.xxx
set user root
set passwd *******
spawn ssh $user@$server
expect -re "password:"
send "${passwd}\r"

expect -re "$"

# 给操作权还回给用户
interact

测试 #expect test.exp 工作的很好,只是当窗口发生改变时,会出问题,如下

现在我们根据上面perl中讲到的修改这个,让窗口改变的信号也能传送到远程服务器 修复后的expect.

#!/usr/bin/env expect
#trap sigwinch spawned
trap {
 set rows [stty rows]
 set cols [stty columns]
 stty rows $rows columns $cols < $spawn_out(slave,name)
} WINCH

set server xxx.xxx.xxx.xxx
set user root
set passwd *******
spawn ssh $user@$server
expect -re "password:"
send "${passwd}\r"

expect -re "$"
# 给操作权还回给用户
interact
 
Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪 ViVi 365Key 网摘 天极网摘 和讯网摘 博拉网 POCO 网摘 饭否 QQ 书签 Digbuzz 我挖网 Mister Wong
Tags:
  1. 斯文
    Nov 4th, 2009 at 12:09
    Reply | Quote | #1

    回帖加分!! 非常好用