在水一万 发表于 2018-8-31 06:29:50

Expect模块安装 Perl脚本

  一、Expect模块安装
  通过安装Perl的Expect模块可以方便的实现远程主机自动登入和执行命令的功能。由于Expect模块依赖于IO::Tty模块,所以要使用Expect模块需要安装这2个模块:
  (1)IO-Tty-1.10 (2)Expect-1.21
  具体的安装模块很简单,和以前一样:perl Makefile.PL/make/make test/make install。
  二、Expect模块说明
  Expect模块的主要功能也通过 spawn,expect,send三个方法实现:
  1、spawn:启动目标程序
  $exp = Expect->spawn( $command, @parameters );
  spawn是Expect类的主要方法之一,通过fork和exec启动目标程序,若成功则返回Expect对象,否则返回undef。参数$command指定目标程序,@parameters为可选参数。
  2、expect:等待特定输出
  $exp->expect( $timeout, @match_patterns );
  使用Expect对象的expect方法等待目标程序的特定输出。参数列表中$timeout设定超时(以秒为 单位),@match_patterns提供一个或多个匹配模式,如果在设定时间内目标程序输出结果和@match_patterns中某元素匹配则成功 返回。缺省情况下expect使用精确匹配,若想使用正则表达式,可以在该模式元素前加’-re’前缀。
  3、send:发送数据
  $exp->send( @strings );
  当交互式程序等待用户输入时,可以使用send方法向其提供输入数据。
  【参考】:
   和
  三、例子
  安装完成,就可以在Perl脚本中使用Expect模块来实现ssh,下面记录一个例子:
  #!/bin/env perl
  use strict;
  use warnings;
  use Expect;
  $ENV{TERM} = “xterm”;
  # 设置进行ssh的相关参数
  my $ssh_host = “10.254.5.151″;
  my $ssh_user = “mysql”;
  my $ssh_pwd = “mysql”;
  my $ssh_port = “22″;
  # 用于记录ssh的log
  my $ssh_logfile = “/tmp/tmp_zx_output.log”;
  #my @params = qw/10.254.5.151 -lmysql -p22/;
  my @params = ($ssh_host,’-l’.$ssh_user,’-p’.$ssh_port);
  my $cmd = “ssh”;
  my $timeout = 5;
  my $exp = Expect->spawn($cmd,@params) or die “Cant’t spawn $cmd : $!\n”;
  # 执行结果不输出屏幕
  $exp->log_stdout(0);
  # 自动登入
  $exp->expect($timeout,-re=>’assword:’);
  $exp->send(“$ssh_pwd\n”);
  # 在远程主机执行命令
  $exp->send(“uptime\n”) if ($exp->expect(undef,’$'));
  $exp->send(“/home/mysql/orzdba -lazy -C 3\n”) if ($exp->expect(undef,’$'));
  # 发送上面那条命令后,进行记录输出到logfile中。
  # 执行将ssh日志输出到perl执行的本地文件:
  #$exp->log_file($ssh_logfile);         # Append
  #$exp->log_file($ssh_logfile, “w”);# Replace
  $exp->log_file($ssh_logfile, “w”);
  # 退出
  $exp->send(“exit\n”) if ($exp->expect(undef,’$'));
  # 结束ssh日志的记录
  $exp->log_file(undef);
  $exp->soft_close();
  ==================================================================================
  FEI:我自己写的一个简单的ssh
  #!/usr/bin/perl
  use strict;
  use warnings;
  use Expect;
  $ENV{TERM}="xterm";
  my $ssh_host="127.0.0.1";
  my $ssh_user="test";
  my $ssh_pwd="xxxxxx";
  my $ssh_port="22";
  ###############################
  my $ssh_logfile="/tmp/ssh.log";
  my @params=($ssh_host,'-l'.$ssh_user,'-p'.$ssh_port);
  my $cmd="ssh";
  my $timeout=5;
  my $exp=Expect->spawn($cmd,@params) or die "can not spawn $cmd\n"; ##此条指令相当于ssh 127.0.0.1 -ltest -p22
  ###############################
  $exp->log_stdout(0);
  $exp->expect($timeout,'-re'=>'assword:');
  $exp->send("$ssh_pwd\n");
  $exp->send("touch abc\n")if($exp->expect(undef,'$'));
  $exp->log_file($ssh_logfile,"w");
  $exp->send("exit\n")if($exp->expect(undef,'$'));
  $exp->log_file(undef);
  $exp->soft_close();
  注:一开始我怎么也不能成功,实在想不出哪里出错,后来是发现第一次SSH时,会提示是否接受服务器的RSA,而perl脚本里并没有对这一情况进行判断,所以老是卡在了那里。手动选择yes,在~/.ssh/known_hosts文件中记录下来之后,往后就不会有这样的突发情况了。正常情况是,ssh 127.0.0.1 -ltest -p22 这条指令执行后,会提示....password:,所以perl脚本中通过正则-re来判断等待出现passwod字样后,再进行sned操作
  /tmp/ssh_logfile的内容是touch abc
  $exp->log_stdout(0);是关闭stdout输出
  如果注释掉$exp->log_stdout(0);
  那么自己屏幕上会输出如下信息(一瞬间的):

页: [1]
查看完整版本: Expect模块安装 Perl脚本