#! /usr/bin/perl
use Expect; #前提是你的服务器上得有Expect包,没有请安装,否无法使用交互
my $PROMPT = ‘[\]\$\>\#]\s*$‘; # 远程系统的命令提示符模式
@a = qw#100.100.100.200 root#; #假设你要登陆的主机ip是100.100.100.200用户是root
$obj = Expect->spawn( "ssh -l $a[1] $a[0]" ) or die "Couldn‘t spawn telnet, $!";
$obj->log_stdout(0);#关闭目标程序的回显
$obj->expect( 10,
[ qr/yes\/no:\s*$/i,
sub{ my $self = shift; $self->send( "yes\r" ); exp_continue;}
],
[ qr/password:\s*$/i,
sub{ my $self = shift; $self->send( "123456\r" ); exp_continue;}
],
[ qr/$PROMPT/,
sub{my $self=shift; $self->send( "cd\rtouch 1 2 3\rexit\r" ); exp_continue_timeout;}
],
);
#$obj->soft_close();#关闭目标程序
时间: 2024-10-02 11:19:49